Localization/Globalization in ASP.Net 2.0

爷,独闯天下 提交于 2020-03-25 05:11:24

There are many tutorials that explain you about localization/globalization concept. In this article I just tryed to explain every single point from the developer/programmer point of view.

 

Let me brief you about Resource files.

 

"aspx.resx" (resource)files:  Resource files are xml files, which stores string, file paths (image), which are required to translate in to other languages.

 

"App_GlobalResources": You can keep files, which need to be accessed throughout the application. So in our application I have kept "TextDirect" & "EmailFormat" which required to access in maximum pages. In short common strings.

 

"App_LocalResources": Local resources are associated with the single web pages, In this folder I have kept lables and strings which are specific to particualt aspx page it self.

Note: Since I am showing this in English and Arabic. So every aspx page will have two different resource files.

For Example:

I have "Registration.aspx" page, for this page I had made files as follows:

  1. Registration.aspx.ar.resx (for arabic content to be stored).

  2. Registration.aspx.resx (for english content to be stored).

The above files are local, now let us consider files which can be accessed globally as.

  1. MulResource.resx

  2. MulResource.ar.resx

These files are used for storing "EmailFormat" and "TextDirection" which is common for most of the pages.

To create a resource file manually:

Make sure that your Web site has a folder in which to store the resource file by doing one of the following:

If you are creating a global resource file, you must have a folder named App_GlobalResources. To create the folder, in Solution Explorer, right-click the name of your Web site, click Add Folder, and then click App_GlobalResources Folder. There can only be one of these folders in an application, and it must be located at the root of the application.

If you are creating a local resource file, you must have a folder named App_LocalResources. To create the folder, in Solution Explorer, right-click the name of your Web site, click Add Folder, and then click App_LocalResources Folder. There can be many of these folders in an application, and they can be located at any level in the application.

To create a resource file, right-click the App_GlobalResources or App_LocalResources folder, and then click Add New Item.

Global resource files must be in the App_GlobalResources folder. If you try to create a .resx file outside of this folder, Visual Web Developer prompts you to create it in the folder.

Globalization:

It is process by which you can develop application/program so that it can be used by the various regions/cultures. Let us consider that you have made a product called as "Shopping Cart" and you want to sell it to different regions, since it is online say Arabic people should able to read the info in Arabic & English people should read it in English. To achieve this we have to use something called as localization

Localization:

It is process of using specific regional/cultural info so that your program uses local language.

Culture:

Every region has different language and language depends on different geographical location.

For example: "ar-EG" ar is the code for arabic language & EG means this language is spoken in Egypt country/region so "ar-EG" sets for Arabic-Egyption. Similarly "en-US" stands for English-United State.

Note: In IE the user can change the culture by going to Internet Options->General->Languages. For this to work, we need to set both the Culture and the UICulture to auto and enableClientBasedCulture = true

Setting Page Culture:

You can set the page culture by two ways:

  1. <globalization enableClientBasedCulture="true" culture="ar-EG" uiCulture="ar-EG"/>

  1. Setting culture by programmatically.

Protected Overrides Sub InitializeCulture()

          'string culture = Request.Form["ddSelLanguage"];

          Dim culture As String = Session("Language").ToString()

          If String.IsNullOrEmpty(culture) Then

                   culture = "Auto"

          End If

          'Use this

          UICulture = culture

          Culture = culture

          'OR This

          If culture <> "Auto" Then

                   Dim MyCltr As System.Globalization.CultureInfo = New System.Globalization.CultureInfo(culture)

                   System.Threading.Thread.CurrentThread.CurrentCulture = MyCltr

                   System.Threading.Thread.CurrentThread.CurrentUICulture = MyCltr

          End If

          MyBase.InitializeCulture()

End Sub

 

Code:

In "Default.aspx" file I have a drop-down by which user can select specific language, I have Arabic and English languages and by default "Auto" is set. I have used Auto-postback property of the drop-down & even you can make use of submit button for the same.

Once you got the selected language you can hold that value in the session as explained in the above code.

Session["Language"] = Request.Form["ddSelLanguage"];

While overriding the InitializeCulture() method you can assign this to the culture variable.

In this application I have tryed to explain with two different forms:

  1. User registration form.

  2. Send Email (In which I have also explained that how can we make use of built-in asp.net validation controls).

And on both the pages I have shown as we can switch on different pages by making use of <asp:HyperLink>.

Now let us have a look at "Registration.aspx" page code.

<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Registration.aspx.vb" Inherits="Registration" meta:resourcekey="RegResource" %>

This code is preety much similar with the code which get generated normally for any aspx page, only difference is that:

meta
:resourcekey="RegResource"

Here I want to explain in little depth, the concept of storing values like key-value pair and accessing values in the aspx page. Forexample if you refer to "Registration.aspx.resx" & "Registration.aspx.ar.resx" pages you will notice that a resourceKey "RegResource.Title" has value "Registration" similarly in the other language. "RegResource.Title" has value, which is directly accessed as shown above.

<
html id="Html1" runat="server" dir='<%$ Resources:MulResource, TextDirection %>' xmlns="http://www.w3.org/1999/xhtml">

in the above <html> tag it should have runat="server" attribute or it will give you error use literal runat="server".

As I have already explained you about the global & local resources, in the above <html> we have used dir='<%$ Resources:MulResource, TextDirection %>'  attribute, now let us go little in dept for this, many of you must be aware of "DIR" attribute, it is nothing but the direction or the text in the page by default it is LTR (left to write) to have arabic type of appearance of the page we have to use it as "RTL". In short this act as common for all the pages either LTR or RTL so I have two global resource files as:

  1. MulResource.resx

  2. MulResource.ar.resx

In which you will find "TextDirection" ResourceKey and it's value associated with it, which is used in the above code.

I think so far so clear?

Now let us see how we can access the value of different labels in our project.

For example: On the registration page I have label called as "lblFirstName" and its values are stored in the corresponding resource files viz.

  1. Registration.aspx.resx

  2. Registration.aspx.ar.resx

Here is code to make use of the same.

<asp:Label ID="lblFirstName" runat="server" meta:resourcekey="lblFirstName"></asp:Label>

 

Note: None of the labels have assigned any default value, label get values assigned at runtime depend on the selected language on the default page

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!