CMSC 498B: Developing User Interfaces - Spring 2005

Internationalization

Internationalization

1. Globalization => Start with resources

"Assembly" - collection of types, code, resources (i.e., an .exe or .dll)
A VS project encapsulates an assembly
Resources can be added manually
=> Add item, change Build Action property to Embedded Resource
Gets added to assembly, and name gets added to "manifest"
Use ildasm.exe to look at an assembly and its manifest

Loading Manifest Resources

using System.reflection;
Assembly assem = this.getType().Assembly;
Stream stream = assem.GetManifestResourceStream("namespace.filename");
Image image = new Bitmap(stream);
// shortcut on some APIs
// Uses default namespace from this type
Image = new Bitmap(this.GetType(), "namespace.filename");

Text-based resources in XML ".resx" file

using System.Resources;
ResXResourceReader reader = new ResXResourceReader("resource.resx");
foreach (DictionaryEntry entry in reader) {
    if (entry.Key.ToString() == "ben")
    string value = (string)entry.Value;
}
// shortcut using Resource Manager
ResourceManager resmgr = new ResourceManager(this.GetType());
string s = resmgr.GetString("ben");

Note that the forms designer uses .resx files to store properties, embedded images, etc.
This implies that the resource needs to be reassigned if it changes on disk

2. Localization

Using Resource Manager for LOCALIZATION

using System.Globalization;
foreach (CultureInfo info in CultureInfo.GetCultures(CultureTypes.AllCultures)) {
    Console.WriteLine(info.EnglishName);
    Console.WriteLine(amount.ToString("C", info.NumberFormat);
    Console.WriteLine(date.ToString("d", info.DateTimeFormat);
}
Application.CurrentCulture; // Get or set to sample - new CultureInfo("fr-CA");

Actually one culture per *thread*
That is a shortcut for System.Threading.Thread.CurrentThread.CurrentCulture

Forms have special support for localization
Form.Localization = true => .resx file adds 26 properties that are then localized
This means that all kinds of things - layout, image, text, scrolling, etc. are localized
In this situation, you can then set the Form.Language property

Use VS to create a form with some controls, and for each Form's language, set different properties
You will see that different .resx and different .dlls get generated
You can set UI based on culture with CurrentUICulture

NOTE:
CurrentCulture is general culture info
CurrentUICulture is culture info for resource manager

Example:

System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("ar-EG");
ResourceManager resmgr = new ResourceManager(this.GetType());
label1.Text = Application.CurrentCulture.Name + "\n"
    + DateTime.Now.ToString(Application.CurrentCulture.NumberFormat);
button1.Text = resmgr.GetString("button1.Text");
button2.Anchor = (AnchorStyles)resmgr.GetObject("button2.Anchor");

Can look at .resx files in VS to see what is going on
ildasm does not show .resx properties - only

Remember that the resource manager will look for the most detailed resource available
I.e. ar-EG, then ar, then default

Non-Programmers can localize applications by using the winres.exe utility to create new .resx files
The developer can then use resgen.exe to compile those .resx files into a .dll which can be used with the application
NOTE: Don't use VS and winres to localize .resx files