GlobalizationResource in ASP.NET


Globalization & Localization:

It is that part of .NET that deals with "Multi Lingual" Applications i.e the web application which can generate content in different languages. The Dynamic content which comes from the database will already be in a specific language we have to explicitly take care of the static content we change from language to language

For every language which we wish to support we add a RESOURCE FILE having key value pairs ,the keys will be same but the values will change based on the language which our application supports.

To the project we can have GLOBAL and Local Resource Files

The Difference between Global and Local Resource Files are, GRF: It can share between many web forms, LRF: It has to share to a particular web form

*GRF(GlobalResource File)

*LRF(LOCALResource File)

  1. While dealing with multilingual If some dynamic (language independent) text has to be stored in the database table the column datatypes to be taken must be either nChar, nVarchar, nText.
  2. In Web.Config: <globalization requestEncoding="Unicode" responseEncoding="Unicode"/>

"WebConfig" will decide how the text going to be encoded

Three steps for building a Multi-Lingual WebApplication

Creation of Resource files

Designing the WebForm so that the controls in it are linked to the Keys of resource files

Set the culture settings with thread executing the request from the client

Creation of Resource Files

Local Resource File: These files must be present in the folder App_LocalResources and the filename is based on the name of WebForm with which the resource file is associated with.

Steps:

  1. Create a new Web Application with Default.aspx
  2. To the root folder - RightClick - Add ASP.NET Folder - App_LocalResources
  3. To App_LocalResources - RightClick - Add a new Item - Resource File (provide name and Value pairs)

\AppName\Default.aspx.
\AppName\App_LocalResources\Default.aspx.resx - Culture Neutral Resource File.
\AppName\App_LocalResources\Default.aspx.fr.resx - French Neutral Resource File.
\AppName\App_LocalResources\Default.aspx.fr-BE.resx - French Belgium Resource File.
\AppName\App_LocalResources\Default.aspx.de.resx- German Neutral Resource File.

Globalization Resource

Global Resourse File in the folder App_GlobalResources: This is Global to all the WebForms in a given web application. It can have any name and must be present in the Root directory of the web application.

Globalization Resource

Globalization

Design the WebForm so that the controls are linked to Keys in Resource Files

Steps for setting the Explicit Expression:

  • Select the control - RightClick - Properties - Goto Expressions
  • On the lefthand side select the property to be initialized, set ExpressionType to Resources
  • If the key has to be fetched from LocalResources File directly set ResourceKey. Otherwise, to fetch from GlobalResoucesFile first set ClassKey with the value of GlobalResource Filename and then set ResourceKey

--------------------Explicit Expression From Local Resource File------------------

<asp:Label Text="<%$Resources:lblRealNameTextKey %>"
ForeColor="<%$Resources:lblRealNameForeColorKey %>" ID="lblRealName" runat="server"/>


------------------Explicit Expression From Global Resource File---------------</strong>

<asp:Label Text="<%$ Resources:Settings, lblCompanyName %>" ID="lblCompanyName" runat="server"></asp:Label>


--------------------Implicit Expression-------------------

<asp:Label meta:ResourceKey="lblAddressKey" ID="lblAddress" runat="server"/>
Based on the key "lblAddressKey" the ForeColor and Text properties are automatically fetched from the resouce file.


Note: Implicit expression cannot fetch keys from Global Resource File.


-------------------Fetching Values Programatically-------

<asp:Label ID="lblRealName1" runat="server" Font-Bold="False"></asp:Label>


--------------Culture Demos-------------------

<asp:Label ID="lblDate" runat="server"></asp:Label>
<asp:Label ID="lblAmount" runat="server"></asp:Label>

Default.aspx.vb

        protected void Page_Load(object sender, EventArgs e)
        {
            lblRealName1.Text = GetLocalResourceObject("lblRealNameTextKey").ToString();
            lblRealName1.ForeColor =
            System.Drawing.Color.FromName(GetLocalResourceObject("lblRealNameForeColorKey").ToString());
            decimal amt = 100.02M;
            lblAmount.Text = amt.ToString("C");
            lblDate.Text = DateTime.Now.ToString();
        }


We can use GetGlobalResourceObject for reading the Key - Value pair from Global resource file.

Any one of the following alternatives can be used for associating the culture with the current thread / request in execution.

Alternative 1

We do it in the Web.Config file.
 
Note: If configured here the value would remain same for all the webforms of the project and is not based on clients choice of language.

Alternative 2: In ASPX Page.

<%@ Page UICulture="auto" Culture="auto" . . . %> or
<%@ Page UICulture="de-AT" Culture="de-AT" . . . %>

CULTURE: It is based on value of culture the data and currency symbol number formating is done

UICULTURE:The Appropate resourcefile is loaded

The Culture value can be set to specific cultures only, such as en-US or en-GB. This prevents the requirement to identify the correct currency symbol to use for en, where en-US and en-GB have different currency symbols

Users can set the UI culture and culture in their browsers. For example, in Microsoft Internet Explorer, on the Tools menu, users can click Internet Options, on the General tab, click Language, and then set their language preference

  • Request.UserLanguages is a collection and is based on the value (comma separated) of a request header "Accept-Language"
  • If the value of Culture or UICulture is set to "auto" then the value is taken from first value of request header "User-Languages". This request header is initialized in browser by going to Tools - Internet Options - Languages.
  • If configured here then it has to be done for every page and the value to be mentioned must be constant or "auto"

If the Culture settings of the thread has to be set programatically, it must done either in InitializeCulture method of the Page class or before it (Application_BeginRequest of Global.asax)

Alternative 3

<asp:dropdownlist autopostback="true" id="ddlCulture" runat="server" onselectedindexchanged="ddlCulture_SelectedIndexChanged">
<asp:ListItem Value="en-US" Text="English"></asp:ListItem>
<asp:ListItem Value="de-AT" Text="German"></asp:ListItem>
<asp:ListItem Value="fr-Be" Text="French"></asp:ListItem>
<asp:ListItem Value="hi-IN" Text="Hindi"></asp:ListItem>
</asp:dropdownlist>
 
    protected void ddlCulture_SelectedIndexChanged(. . .)
    {
          Response.Cookies ["culture"].Value
            = ddlCulture.SelectedValue; Response.Redirect(Request.Path);
    }
    protected override void InitializeCulture()
    {
         if (Request.Cookies["culture"] == null) return;
         Page.Culture = Request.Cookies["culture"].Value;
         Page.UICulture = Request.Cookies ["culture"].Value;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
         if (!IsPostBack)
         {
               if (Request.Cookies["culture"]
               != null) ddlCulture.SelectedValue = Request.Cookies["culture"].Value;
         }
    }


Alternative 4

//Following should be written in Global.asax and this global to all the webforms of the application.

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (Request.Cookies["culture"] == null) return;
            System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(Request.Cookies["culture"].Value);
            System.Threading.Thread.CurrentThread.CurrentCulture = ci;
            System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
        }

  1. Based on the Culture value of the CurrentThread the Formatting functions (like Date or Currency) render their output.
  2. Based on UICulture of the CurrentThread the framework loads the appropriate resource file for all the culture specific lookups.

Up Next
    Ebook Download
    View all
    Learn
    View all