Understanding Globalization and Localization in .NET
Now days as the audience for web applications has bursted from computers to pda's, cell phones and many other gadgets, Globalization has become must for web application. Globalization allows your web application to be useful for different people in different parts of the world who speaks different languages. Thanks to Microsoft .Net Globalization feature. Using this feature one can easily build a web application available to dispersed audience throughout the world. In this article I will walkthrough the steps that are essential for creating a multi cultured application. This example will include the use of Global and Local resource files that are heart for multi cultured web application. ASP.Net supports two types of resources Local Resource and Global Resource. Both types of Resources are xml based resource files that can contain text translated into particular language
Local Resources: Local resource is specific to particular webpage. All the resource files related to particular webpage must be placed in a special .Net folder named App_LocalResources. This folder must be in the same folder of the webpage that you want to localize. So App_LocalResources can be subfolder for any folder of your website.
Global Resources: They are the resource files for the entire web application. They are not specific to any page. They can be accessed from the entire web application. Global resource files must be located in a special .Net folder named App_GlobalResources. App_GlobalResources must be at the root of the web application.
Steps to create local Resource file.
1) Switch to design view of the webpage for which you want to create the resource file.
2) Go to Tools menu and click Generate Local Resource.
3) As the result of step 2 .Net generates the .resx file. Naming conventions that Resource files follow is as explained below: <PageName>.aspx.<language>.resx Example myWebPage.aspx.resx: This is the default resource file of your page. If no culture matches than this resource file is used. myWebPage.aspx.es.resx: For Neutral Spanish culture it may look like this. myWebPage.aspx.es-MX.resx: For Specific Spanish Mexican culture it may look like this. What do you mean by neutral and specific cultures? I leave this small exercise to readers.
Steps to create global Resource file.
1) Their are no specific utilities in .Net to generate Global resource files automatically. But you can create App_GlobalResources folder at root level and add a resource file manually Or you may copy the local resource file and change it at your will.
Ways in which you can Globalize or localize your application. Implicit Localization
How to check this?
1) Open IE and go to Tools > internet Options
2) Click Languages tab.
3) This will open language Preference dialog box window.
4) Add the language by clicking the add button. Make sure to add the language for which you have configured the resource file of your web page.
5) Select the language that you have added and using Move Up button, bring it to the first topmost position in the language list.
6) Press Ok and again Ok to return. Open your webpage in the browser and enjoy the taste of globalized web application. I believe that few lines of code can speak thousand words and so I have prepared a small demo that includes both Globalization and Localization.
To download the entire source code Click Here.
Note: Globalizing the web application entirely, requires lots of other things like changing the Culture, formatting dates and currency and lot more. I will try to incorporate all this stuff in my next article.
Now days as the audience for web applications has bursted from computers to pda's, cell phones and many other gadgets, Globalization has become must for web application. Globalization allows your web application to be useful for different people in different parts of the world who speaks different languages. Thanks to Microsoft .Net Globalization feature. Using this feature one can easily build a web application available to dispersed audience throughout the world. In this article I will walkthrough the steps that are essential for creating a multi cultured application. This example will include the use of Global and Local resource files that are heart for multi cultured web application. ASP.Net supports two types of resources Local Resource and Global Resource. Both types of Resources are xml based resource files that can contain text translated into particular language
Local Resources: Local resource is specific to particular webpage. All the resource files related to particular webpage must be placed in a special .Net folder named App_LocalResources. This folder must be in the same folder of the webpage that you want to localize. So App_LocalResources can be subfolder for any folder of your website.
Global Resources: They are the resource files for the entire web application. They are not specific to any page. They can be accessed from the entire web application. Global resource files must be located in a special .Net folder named App_GlobalResources. App_GlobalResources must be at the root of the web application.
Steps to create local Resource file.
1) Switch to design view of the webpage for which you want to create the resource file.
2) Go to Tools menu and click Generate Local Resource.
3) As the result of step 2 .Net generates the .resx file. Naming conventions that Resource files follow is as explained below: <PageName>.aspx.<language>.resx Example myWebPage.aspx.resx: This is the default resource file of your page. If no culture matches than this resource file is used. myWebPage.aspx.es.resx: For Neutral Spanish culture it may look like this. myWebPage.aspx.es-MX.resx: For Specific Spanish Mexican culture it may look like this. What do you mean by neutral and specific cultures? I leave this small exercise to readers.
Steps to create global Resource file.
1) Their are no specific utilities in .Net to generate Global resource files automatically. But you can create App_GlobalResources folder at root level and add a resource file manually Or you may copy the local resource file and change it at your will.
Ways in which you can Globalize or localize your application. Implicit Localization
<asp:Label ID="lblLoadLocalResource" runat="server" Text="Local Resource" meta:resourcekey="lblLoadLocalResourceResource1"> </asp:Label>Implicit Globalization
<asp:Label ID="lblLoadGlobalResource" runat="server" Text="<%$ Resources:Resource, commonHeader %>" ToolTip="<%$ Resources:Resource, commonHeader %>"> </asp:Label>Programmatic Localization
lblLoadLocalResource.Text = GetLocalResourceObject ("lblLoadLocalResourceResource1.Text").ToString(); lblLoadLocalResource.ToolTip = GetLocalResourceObject ("lblLoadLocalResourceResource1.ToolTip").ToString();Programmatic Globalization
lblLoadGlobalResource.Text = GetGlobalResourceObject ("Resource", "commonHeader").ToString(); lblLoadGlobalResource.ToolTip = GetGlobalResourceObject ("Resource", "commonHeader").ToString();How to change the UI Culture? Steps to check and configure the UI Culture of your webpage 1) To change it programmatically you need to override the InitializeCulture() of your web page.
protected override void InitializeCulture() { UICulture = "en"; }2) To change it implicitly from .aspx page Set
<%@ Page Language="C#" UICulture="en" %>3)To change it globally from web.config file
<system.web> <globalization uiCulture="es"/> </system.web>
Note: If you set UICulture="auto" then .Net runtime will automatically detect the requesting browsers preferred language setting and will compile the related resource file for that culture. In this case you do not have to write a single piece of code for setting UICulture.
How to check this?
1) Open IE and go to Tools > internet Options
2) Click Languages tab.
3) This will open language Preference dialog box window.
4) Add the language by clicking the add button. Make sure to add the language for which you have configured the resource file of your web page.
5) Select the language that you have added and using Move Up button, bring it to the first topmost position in the language list.
6) Press Ok and again Ok to return. Open your webpage in the browser and enjoy the taste of globalized web application. I believe that few lines of code can speak thousand words and so I have prepared a small demo that includes both Globalization and Localization.
To download the entire source code Click Here.
Note: Globalizing the web application entirely, requires lots of other things like changing the Culture, formatting dates and currency and lot more. I will try to incorporate all this stuff in my next article.