Static Text


Static text represents a fundamental problem for an internationalized application: It cannot easily be localized. Type any text directly onto a Web form, and this text is stored as static text in the aspx page. At compile time, the static text is converted to a LiteralControl in the page's control tree. The LiteralControl is not a server-side control and can be accessed only by walking through the HtmlForm's Controls. The best solution is to replace the static text with either a server-side Label control or a server-side Literal control (do not confuse a "Literal control" with a "LiteralControl control"). A Label control differs from a Literal control only in that a Label control can have a style and a Literal control cannot.

There is a performance penalty to pay for placing all text in Label or Literal controls as opposed to typing it directly onto a page. Unfortunately, this is a penalty that you will have to endure if you want your application to be localized.


If you are unable to convert all the static text in your application to Labels or Literals, a considerably poorer solution would be to search for known unlocalized strings and replace them with localized strings. For example, assume that a page contains "Here is some static text." The following call to ReplaceStaticText (included in the downloadable source code for this book) attempts to localize it:

 ReplaceStaticText(     "Here is some static text",     "Voici un certain texte statique"); 


Of course, in practice you would create a resource called, say, HereIsSome StaticText, and read it using a ResourceManager:

 ReplaceStaticText (     "Here is some static text",     resourceManager.GetString("HereIsSomeStaticText")); 


The ReplaceStaticText method is this:

 private void ReplaceStaticText (     string fallbackString, string localizedString) {     HtmlForm htmlForm = GetHtmlForm(this);     foreach(Control control in htmlForm.Controls)     {         if (control is LiteralControl)         {             LiteralControl literalControl =                 (LiteralControl) control;             literalControl.Text = literalControl.Text.Replace(                 fallbackString, localizedString);         }     } } 


The ReplaceStaticText method looks for every LiteralControl and searches for the original fallback text, replacing it with the translated text. You might prefer to break out of the loop having found a successful match, but this assumes that each string is unique to a page, and that is not necessarily true. Furthermore, the code assumes that no text is a substring of another text. So, for example, you don't have a string of "Go" and a second string of "Going, Going, Gone". The String.Replace method is used because the LiteralControl's Text value is almost certainly not exactly equal to "Here is some static text". In practice, it is more likely to be something like this:

 </P>\r\n\t\t\t<P>&nbsp;</P>\r\n\t\t\t<P>Here is some static text </P>\r\n\t\t\t<P></P>\r\n\t\t\t 


All the text and HTML immediately before and after the string is also added to the LiteralControl. Hopefully when you look at this "solution," you will decide that the effort involved in working around the problem (not to mention its inherent inaccuracy) is greater than solving the problem properly.




.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