Implement Multilingual in ASP.NET Web Site using globalization.

Before going to implement we need to know the answer of few question:

What is globalization?

Globalization is the process of making an application that supports multiple cultures without mixing up the business logic and the culture related information of that application.

What is localization?
In Localization you customize the application for new locales. This consists of translating resources that you identified during the globalization phase.

What is Local Resource?
File which stores resources with local scope into App_LocalResource directory. We can not access one pages local resource from another page.
They can be generated by ASP.Net automatically. For it go to design view of aspx page and then Tools > Generate

What is Global Resource?
File used to store resources globally, means we can access same resource from different pages. It store in App_GlobalResource folder. we can't generate global resource automatically.

Difference between Local Resource and Global Resource:
  1. Performance: Global Resources are faster than Local Resource- because Global Resources are Strongly Typed.
  2. Consistency. Explanation: In your application you have Label Text as Category and ten pages. Now you want to change it to CATEGORY then if you using Local Resource concept then you have to change 10 resource files, where as in Global Resource you need to change only one.
  3. Difference as per Project Type:
    • Local Resource is treated as content based resource, so we can change it and it affects to website.
    • Global Resource is treated as embedded resource, So they are compiled into specific language dlls into bin folder.
Code to implement multilingual using global resource:

Add This in global.asax file:

private void Application_BeginRequest(Object source, EventArgs e)
{
string[] languages = HttpContext.Current.Request.UserLanguages;
if (languages[0].ToLower() != null && languages[0].ToLower()!="")
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(languages[0].ToLower());
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(languages[0].ToLower());

}
}

now we can use resource file on page like this:
suppose we have three resource file in app_globalresource folder:
TestResource.resx (Default)
TestResource.hi.resx (Hindi)
TestResource.zn-ch.resx (Chinese)
<:Label ID="lblText" runat="server" Text="<%$ Resources:TestResource, LabelText %&>">

now as we set the language from browser, the text for label will be read from respected files.

0 comments: