Embedded Control Characters


This issue represents a development decision that you will need to make: Should you include embedded control characters in strings or exclude them from strings? The following code snippet has a string that contains embedded control characters ("\n"):

 MessageBox.Show("There are 5 deliveries.\n\n" +     "3 deliveries are suspended.\n\n" +     "Continue with order ?",     "", MessageBoxButtons.YesNoCancel); 


(Ignore the fact that the string resource is hard-coded and not loaded from a resource manager, and also that the numbers are not represented by placeholders).

The "\n" control character is a new line control character. Equivalent code could be written like this:

 MessageBox.Show("There are 5 deliveries." +     System.Environment.NewLine + System.Environment.NewLine +     "3 deliveries are suspended." +     System.Environment.NewLine + System.Environment.NewLine +     "Continue with order ?",     "", MessageBoxButtons.YesNoCancel); 


The question is, which is better in terms of localization? There are pros and cons with both approaches. The former approach puts formatting control in the hands of the translator. The translator is at liberty to remove or add new lines and other control characters, according to the translator's desires/needs. Whether this is necessary is probably dependent upon the complexity of the translated phrases. A single phrase or sentence almost always translates into a single phrase or sentence in any other language. A complex description made up of many sentences or paragraphs will not necessarily contain the same number of sentences (or new lines) when the entire text is translated to another language because it is the meaning that will most likely be translated. In this scenario, a translator would be trapped by the preconceptions of new lines embedded in the source code instead of resource strings. Another issue to consider is how embedded control characters are handled in machine translation. Most machine-translation facilities do not understand .NET Framework control characters, and the results of such translations can be less than successful (although you can increase your chances of success by separating control characters from other text with spaces or full stops). If you choose to put control in the hands of the developer (and, therefore, exclude control characters from strings), you should take a look at the "Control characters embedded in resource string" rule in Chapter 13, "Testing Internationalization Using FxCop," which reports embedded control characters as errors.

A variation on the theme of embedding control characters in strings is to embed placeholders in strings to mark the location of new lines and other formatting characters:

 MessageBox.Show(String.Format("There are 5 deliveries.{0}{0}" +     "3 deliveries are suspended. {0}{0}" +     "Continue with order ?", System.Environment.NewLine),     "", MessageBoxButtons.YesNoCancel); 


In this example, the new line characters, System.Environment.NewLine, are passed as a string parameter to String.Format. The localized string can contain any number of placeholders (including zero) with the same numeric identifier (i.e., {0}), so the translator has complete freedom over the number of new lines in the text. Essentially, this is simply a variation of the first approach that embeds control characters in the localized string. The difference from a localization viewpoint is that the new lines are represented by placeholders instead of control characters. For some scenarios, this difference is beneficial. Some machine translators yield better results using placeholders than embedded control characters, especially when the control characters are not delimited with spaces or punctuation.




.NET Internationalization(c) The Developer's Guide to Building Global Windows and Web Applications
.NET Internationalization: The Developers Guide to Building Global Windows and Web Applications
ISBN: 0321341384
EAN: 2147483647
Year: 2006
Pages: 213

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net