Globalization
Setting the culture
- CurrentCulture is date/numbers (ddmmyy or mmddyy; 1.5 or 1,5)
- CurrentUICulture is for looking up resources
On all string/number/date operations use the optional formatting argument (FXCop will complain if you don't). Either System.Globalization.CultureInfo.CurrentCulture or .InvariantCulture (if applicable).
For the web, the HTTP header contains user languages (set within IE-Tools-Internet Options-Languages) which you can read in Request.UserLanguages[0] (for the first one). ASP 1: Read it (or a cookie from a user form) in Global.asax Session_Start. In ASP 2 it's automatic.
Resource Files
Use assembly linker for satellite assemblies: al /t:lib /culture:en-UK /embed:strings.en-UK.resources /out:myapp.resources.dll
Asp 2+
App_GlobalResources (Resources namespace, with intellisense) or App_LocalResources (page specific) .No more ResourceManager as in .Net 1!
<p>Implicit Local:
<asp:Label ID="Label0" runat="server" Text="Hello world"
meta:resourcekey="LabelResource1" /></p>
<p>Explicit Local:
<asp:Label ID="Label1" runat="server"
Text='<%$ Resources:HelloWorld %>' /></p>
<p>Global:
<asp:Label ID="Label2" runat="server"
Text='<%$ Resources:Resource1, GlobalKey %>' /></p>
- App_GlobalResources folder (normally just one .resx). Each directory has App_LocalResources with .resx for for each page (key is page name with extension- default.aspx- not the class name).
- In asp markup: implicit (Tools-Localize -adds meta:resourcekey to controls) vs explicit (<asp:Label ID="lab" runat="server" Text='<%$ Resources:lab %>' / > (globals have $Resources:classname, id format)
- In code:
string txt1 = (string)HttpContext.GetGlobalResourceObject("GlobalResources", "MyText");
//or (strong typed but less efficient)
string txt2 = Resources.GlobalResources.MyText;
//or (local)
string txt3 = (string)HttpContext.GetLocalResourceObject("~/WebForm1.aspx", "HelloWorld"); - Use asp:Localize (like asp:Literal with a meta:resourcekey)
- Web.config globalization (and Page) culture="auto" uiCulture="auto".
Override Page.InitializeCulture event (to read from cookie/db setting)protected override void InitializeCulture()
{
if (Request.Form["ListBox1"] != null) //controls not available
{
string selectedLanguage = Request.Form["ListBox1"];
//set the page properties
UICulture = selectedLanguage;
Culture = selectedLanguage;
//set the thread using CultureInfo - needs 2 different ways :(
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
}
base.InitializeCulture();
} - Sitemaps must be done manually: title="$Resources: SiteMap, Home"