Avoid Certain Coding Shortcuts

Developers are forever seeking ways to make their code smaller and faster. Some shortcuts can improve performance for a few language editions of a product, but they can also complicate matters, or simply not work, for others.

Clever Use of Strings

Well-intentioned programmers often try to save space through clever use of strings, such as concatenating string fragments to form a complete string rather than creating a number of very similar complete strings. For example, look at the next section of code, which could be used to form these English messages:

"Not enough memory to open the file FileName1."

"Not enough memory to save the file FileName1."

"Not enough memory to spell check the file FileName1."

char szString1[] = "Not enough memory to";
char szString2[] = "the file";

char szFinalString[cbMaxSz] =
ConcatenateFourStrings(szString1, szCommand, szFilename, ".");

Translators would have a difficult time understanding how to work with these sentence fragments without documentation explaining the context. For some languages, the code would work. The sample sentences would be translated into Swedish as follows:

"Det finns inte tillräckligt med minne för att öppna filen FileName1."

"Det finns inte tillräckligt med minne för att spara filen FileName1."

"Det finns inte tillräckligt med minne för att kontrollera stavning i filen FileName1."

For other languages, however, the sentences would need to be rearranged. In Finnish, for example, the filename would appear in the middle of the sentence:

"Liian vähän muistia tiedoston FileName1 avaamiseen."

"Liian vähän muistia tiedoston FileName1 tallentamiseen."

"Liian vähän muistia tiedoston FileName1 oikeinkirjoituksen tarkistamiseen."

Word order can vary significantly from one language to another. The best way to handle the coding problem above would be to put the strings in a message file and format them using FormatMessage:

... in the message file ...

MessageId=1 SymbolicName=IDS_ERR_01
Language=English
.
Not enough memory to %1 the file %2.
.
Language=Swedish
Det finns inte tillräckligt med minne för att %1 filen %2.
.
Language=Finnish
Liian vähän muistia tiedoston %2 %1.
.

The argument for building strings from other strings, whether it involves concatenating words to form a complete string, extracting words from complete strings, or adding an s to make words plural, is that doing so saves data space. The argument against building strings this way is that different languages require different algorithms. One solution would be to carry customized code for each language in a DLL, though the amount of space you would save would have to be worth the amount of time you would spend customizing the code.

Another potential shortcut involves declaring a single string, such as "none," "blue," or "first," and displaying it in a number of different contexts—on a menu, in a dialog, and perhaps in several messages. The problem with using "all-purpose" strings is that in European languages, adjectives (and some nouns) have anywhere from 4 to 14 different forms (for example, masculine, feminine, and neuter singular; and masculine, feminine, and neuter plural) that must match the nouns they modify. A single string displayed in different contexts will be correct in gender and number in some cases but incorrect in others. Users will consider such a translation amateurish.

Another situation in which noun agreement would be a problem involves strings such as the following:

"Cannot find the %1."

The program might fill in the blank with words such as file, word, network, spell-checker, and so on, but in European languages, articles such as the, a, and an change depending on the gender of the word they modify. Therefore, a better way to handle this message would be to keep the article with the word it modifies:

"Cannot find %1."

The program would then fill in this blank with the file, the word, the network, the spell-checker, and so on.