Jun 20, 2013

.NET Culture, Localization - bits & pieces

Two settings rule culture settings:

The UICulture property is used to specify which resource files are loaded for the page. The
resource files can contain all the text content of your pages translated into a particular
language. You can set this property to any standard culture name. This property is
discussed in detail during the discussion of using local and global resources later in this
chapter.
The Culture property, on the other hand, determines how strings such as dates, numerals,
and currency amounts are formatted
. It also determines how values are compared and
sorted. For example, by modifying the Culture property, you can display dates with
language-specific month names such as January (English), Januar (German), or Enero
(Spanish).
Make distinction between neutral & specific culture:

Notice that each culture name consists of two parts. The first part represents the language
code and the second part represents the country/region code. If you specify a culture
name and do not provide a country/region code—for example, en—then you have specified
something called a neutral culture. If you provide both a language code and a
country/region code—for example, en-US—then you have specified something called a
specific culture.

Set explicitly culture on current thread (no matter Desktop or Website app).
Watch out if you run multithreaded app upon instancing of thread do this:

System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("de-DE");

Although above will work in most cases proper way in WebForms website is to override InitializeCulture() like this:

 protected override void InitializeCulture()
        {
            Culture = "en-US";
        }

Set culture for complete website in web.config:

<system.web>
     <globalization culture="de" uiCulture="DE"/> 
Alternatively use granular approach and assign explicitly to each value its own culture:

var deCulture = new CultureInfo("de-DE");
labCurrency.Text = (2323.23m).ToString("c",deCulture);
If you work on multi-language app you should take special care on comparing string values of different language. Use string.Compare :

if (String.Compare(“Hello”, “Hello”, true, new CultureInfo(“de-DE”)) == 0)
lblResult.Text = “The strings are the same!”; 

Resources file in website

Create App_LocalResources under root of ASPX web pages you want localized.
Place in it resource file named :
MyWebPage.aspx.resx
This is neutral culture default resource file.
To support different languages add Culture id in name like this:

MyWebPage.aspx.de-DE.resx

You can write it neutral culture also:

MyWebPage.aspx.de.resx

Then  in html markup explicitly address resource file member like this:

<asp:Literal ID="literal" runat="server"  Text="<%$ Resources:CURRENCY %>"></asp:Literal>
Above is explicit retrieving of resource value.

There is more intuitive implicit localization using meta tag in server controls:

<asp:Label meta:resourceKey="labCurrency2" id="labCurrency2" runat="server" ToolTip="Funny" ></asp:Label>

labCurrency2.Text de_currency2Text labCurrency2.Tooltip de_currency2Tooltip
If needed you can access content of resource file even outside context of web page.
But still you need to have access to HttpContext:

return (string)HttpContext.GetLocalResourceObject(“~/LocalizablePage.aspx”,
“ClickHere”);

Global resource

When localization is addressing frequent same text it is best to put it in global resource file.
Just create App_GlobalResources folder and place in resource file named what ever you like.
For example:
/_App_GlobalResources
                    site.resx
You obtain value on same ways as explained for localized resources. Here is meta tag example:

<%$ Resources:Site,Copyright %>

Alternative from code is:
(string)GetGlobalResourceObject(“Site”, “Title”);
Since global  resource files get compiled immediately upon change they can  be accessed directly through their namespace:
labName = Resources.Site.Copyright;

How to convert language code page (e.g. 1033) to .NET country ISO code (e.g. DE):


var twoLetterLangugageISO = (new CultureInfo(languageId)).TwoLetterISOLanguageName;

No comments:

Post a Comment