.NET     Console.WriteLine( "All Things .NET" );
.NET Nerd Blog Home
2.25.2002

 

The Saga of Globalization



After a talk by Michele Leroux Bustamante at the SD .NET user group meeting last month, I was inspired to try ou the G18N and I18N functionality. In .NET WinForms, globalization of controls on the form are as easy as setting Localizable property to TRUE, then setting the Language property for each set of controls. The IDE automagically creates the appropriate .resx files (containing xml elements of our properties such as color, location, text, etc) for us, and when building, creates the projectName.resources.dll in the appropriate sub directory.

I wanted to go one step further and play around with string resources. The easiest way to accomplish this is with a text file of name-value pairs of identifiers and strings. This works great as a stand-alone exercise, but now I can't get the string resources to load with the controls/layout resources created by the compiler.

Using the IDE, you can add an "embedded resource file", which gives you the xml-based .resx file, but it doesn't seem to be localizable. So I went about it the manual way --- remember, you always have the command line.


  • created (notepad) mystrings.es.txt
  • resgen mystrings.es.txt mystrings.es.resources
  • build the project with IDE. this creates the blah.xxx.reources files for each language selected when laying out the winform
  • al /out:.\bin\debug\es\globalize.resources.dll /c:es /embed: .\obj\debug\globalize.form1.es.resources /embed:mystrings.es.resources


TADA! The last step in list above MERGES the strings resources with the IDE-created form resources for a given language (es [spanish] in this case).

Finally, in the code, load the strings like this:

Thread.CurrentThread.CurrentUICulture = new CultureInfo( "es" );
ResourceManager rm = new ResourceManager( "mystrings", GetType().Module.Assembly );
MessageBox.Show( rm.GetString( "Greeting", new CultureInfo( "es" ) ) );


Since the IDE doesn't seem to offer this as a built-in feature, it's begging for a "custom build step". According to a quick search on discuss.develop.com however, custom build steps are not supported in VS7 for VB or C#. An MS source said that you have to write a "plug-in" and talk to the environment through automation to accomplish this. Others pointed out that you can add a VC++ utility project dependency to the solution to accomplish the custom steps.



I owe a BIG THANK YOU to Michele Leroux Bustamante for her help on this topic. Together with her talk at the San Diego .NET User Group Meeting, and putting up with my email questions, we were able to get somewhere on this issue.

p.s. Some notes about this topic in ASP.NET (from DevX .NET zone)
Browser sends user culture preferences in HTTP headers. User sets this up in Tools | Options, Languages.
Usually detect settings in Application_BeginRequest

String[] prefs = HttpContext.Current.Request.UserLanguages;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture( culturePref );
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

Dynamically display images from different cultures.
Store images in subdirs based on their culture (ex: appname/images/fr-FR/image.gif )
To dynamically create these at runtime:

  • create server control containing HTML <img> tag
  • Create public property (something like URL) that you can get/set at runtime
  • Override the CreateChildControls method and construct the HREF attribute dynamically


protected override void CreateChildControls()
{
CultureInfo culture =
Thread.CurrentThread.CurrentCulture;
Image flag = new Image();
flag.ImageUrl = "images/" + culture.Name +
"/" + imageName;
flag.Width = imageWidth;
flag.Height = imageHeight;
Controls.Add(flag);
}




Comments: Post a Comment

Powered by Blogger