Monday 29 June 2009

How to do ASP.NET Localization manually

In the "<%@ Page" declaration at the top of the aspx page add UICulture="auto" and Culture="auto".

Add a "meta:resourcekey" attribute to any element that needs localizing

eg...
<asp:label id="MyLabel" runat="server" text="Default Label Text" meta:resourcekey="MyLabelResource" />

Add an App_LocalResources folder to the solution. There seems to be some disagreement over whether and when to use App_LocalResources or App_GlobalResources. I think local unless it's something that'll be repeated on a lot of pages.

Add a new resource file to App_LocalResources folder named like "nameofmypage.aspx.resx" - this is the default language file.

Entries are then things like MyLabelResource.Text etc...

Other language resource files are created with names like "nameofmypage.aspx.fr.resx" for French or if specific to a particular region of a language "nameofmypage.aspx.en-US.resx" for US English and "nameofmypage.aspx.en-GB.resx" for Proper English etc.

That's all for aspx files. To retrieve resources programatically in C# is as simple as:

Label1.Text = GetLocalResourceObject("Label1Resource.Text").ToString();

Testing in IE, go to Tools > Options > Languages and add the appropriate entry and then move it to the top and refresh.


Globals

Globals work a little bit differently. In App_GlobalResources add a resource file eg globals.resx then to retrieve value use:

Label1.Text = GetGlobalResourceObject("globals", "myGlobalResourceString").ToString();

or in the page:

<asp:label id="MyLabel" runat="server" text="<%$ Resources:globals, myGlobalResourceString %>" />

Note the two arguments where the first is the name of the resource file (globals).


From .cs files

To get a resource programatically from within a .cs code file you just need to append HttpContext to the front of the above methods

eg HttpContext.GetGlobalResourceObject("globals", "myGlobalResourceString").ToString()


To use the current culture to format a date into the current language use:
date.ToString(System.Globalization.CultureInfo.CurrentCulture)