CMSC 498B: Developing User Interfaces - Spring 2005Internationalization | |
Internationalization1. Globalization => Start with resources"Assembly" - collection of types, code, resources (i.e., an .exe or .dll) 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. 2. LocalizationUsing 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* Forms have special support for localization Use VS to create a form with some controls, and for each Form's
language, set different properties NOTE: 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 Remember that the resource manager will look for the most detailed
resource available Non-Programmers can localize applications by using the winres.exe
utility to create new .resx files | |