Globalization of asp.net2.0 application and localization of localization

Source: Internet
Author: User
Tags date format expression implement locale resource thread visual studio
asp.net

   Summary:This article will be a practical case to analyze how to achieve a ASP.net 2.0 Web site development process of globalization and localization issues.

   First, Introduction

Globalization and localization are two important concepts that every developer must know when creating a globalized product or application. Although there are many articles that explain the topic better, I have not seen a comprehensive and comprehensive discussion of all the important concepts of globalization/localization. This article is intended to provide a detailed analysis of the globalization issues involved in developing a Web application using asp.net2.0 through a specific case.

   Second, the background theory

Globalization is a phase of application development that is designed to enable programs to be available across multiple cultural areas without regard to language and regional differences. For example, you are developing a small inventory management program, and you live in an area where English is the main language, such as England. Now, if you want to sell your program to another country, say Germany, then you need to make sure that your program is displayed and implemented in German.

Localization is the process of creating content, entering and outputting data using a locale-specific culture and language. Culture will affect date display settings (if mm/dd/yyyy or dd/mm/yyyy), currency display format, and so on. Now, the process of ensuring that our programs can be localized is called internationalization or globalization. In simpler terms, globalization can be defined as a set of activities through which we can ensure that our programs can operate in regions that use different languages and cultures.

Thus, globalization is associated with changes in internal code to support such changes as the use of resource files, and localization is the process of using a specific culture and regional information so that the program can use local languages and cultures. This means translating the string into a specific local language, and to do so, place the language-specific strings in the resource file. Generally, globalization issues should be considered from the primary build and code development phases, and localization is usually implemented later.

   Iii. Realizing the globalization of ASP.net 2.0 website

Let's start with a simple example. To explain the localization and make things as simple as possible, I used ASP.net and C # to create a new Web site called Testsite (see the source code downloaded in this article); I added a masterpage and a default page. This default page contains a textbox and a Calendar control, which has a double-precision real number that describes the currency. In this case, we'll see the corresponding changes as the user chooses a different language currency format. When I run the application, this default page looks like this:


I have published this test Web application and you can see its functional version at the following URL: http://63.134.215.124/testsite/default.aspx

   Iv. culture and localization

First, let me explain culture and localization first.

In general, language also relies on geography. For example, French is both French and Canadian (in addition, many other countries speak the language). But in terms of language, Canadian French is different from French French. Similarly, there is a certain difference between American English and British English. Therefore, languages often need to be associated with specific regions that speak the language, and this is achieved by using localization (language + geography).

For example, FR is a French-language code, and FR-FR means French for French use. As a result, FR only specifies this language, and FR-FR is localized. Similarly, FR-CA defines another representative of Canadian French and cultural localization. If we only use FR, then it represents only a neutral culture (i.e., regional neutrality).

So how do we define or change the current culture?

There are two properties in the CultureInfo class of the. NET FCL (Framework class Library), which we can set by overloading the constructors of the class, and then use it to change the culture of the current thread of execution:

1.UICulture: Gets/Sets the user interface for the currently executing thread. This property helps the runtime load the resource string from a particular resource file (which we will see later). This attribute can be used either as a neutral culture or as a localized one. For example:

Thread.CurrentThread.CurrentUICulture = new CultureInfo ("fr");
Or:

Thread.CurrentThread.CurrentUICulture = new CultureInfo ("Fr-ca");
2.Culture: Get/Set the format of locale-specific culture and currency, date, etc. This attribute requires the location (localization) of the language as well.

Thread.CurrentThread.CurrentCulture = new CultureInfo ("fr-a"); Right because we've given a localized
Thread.CurrentThread.CurrentCulture = new CultureInfo ("fr"); Error, unable to work
Sometimes we need a culture that does not belong to any language or localization and is unchanged for any region/language. To do this, we can use the CultureInfo.InvariantCulture property. This property is used during internal system processing, at which point the requirement is culturally independent or the data stored that does not need to be displayed directly to the end user.

Both the UICulture and culture properties can be defined in the <GLOBALIZATION> property of the Web.config file. In addition, they can be specified at the page level. However, we do not want to hard-code these values but prefer to set them dynamically. From the above, we can also use the Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture properties to encode/set these values. Therefore, we will use these properties in this application.

   v. Localization switching

Now, back in our application, we need a way to implement localization switching. To do this, there are two methods:

1. Use browser settings: In IE, users can change the culture by using the Internet options->general->languages. To do this, we need to set both culture and uiculture to auto and set the Enableclientbasedculture to True, as follows:

Globalization culture= "Auto" uiculture= "Auto" enableclientbasedculture= "true" "

2. User-Specified settings: We are able to provide users with an option to specify and change culture and language at run time. This is the recommended approach, because sometimes the browser itself may not provide a user-specific set of languages (for example, a French tourist may be surfing the internet in India). Also, sometimes changing the language settings via a browser will be blocked.

Now, using the second recommended method, I created a section at the top of the masterpage (inside a panel control), where I used a drop-down list box with these language options to allow the user to choose a specific localized expression.

In this example, I use only four language options to illustrate the problem:

Hindi,american english,british 中文版 and French.

To achieve globalization of my application, my goal is to: whenever a user chooses a specific localized language, the following should occur:

1. All content should be localized: This means that all strings and text should be displayed in the selected language and localization.

2. The title (/content) of each control should also display text in the local language.

3. Date and currency formatting should occur according to the selected localization.

4. All messages displayed to the user should be in the local language.

To do this, you first need to make sure that you take this content out of your code and put it in a separate resource file, in. NET, which is a simple XML file with a. resx extension.

Since the content will vary according to language, we have created corresponding resource files for each culture (language). Name and value two domains are available in each of these files. The following is an example entry in two resource files; here, suppose we have to enter a string "Welcome":

1. Add a new resource file and name it testsiteresource.resx and use the VS editor to open it. Enter "Banner" in the Name field and enter "Test Website for Localization" in the Value field. This resource file defaults to American English.

2. Add another resource file and name it "Testsiteresources.fr-fr.resx". This file corresponds to the French string. Add "Banner" to the Name field and add "examinez le site Web pour le comportement de localisation" in the Value field.

If you want to add Canadian French resources, then you need to create another resource file "Testsiteresources.fr-ca.resx". The middle part of the name defines localization, and it should be the same as specified by the UICulture property.

3. These files will be saved in the App_GlobalResources folder of ASP.net 2.0.

Tip If you only want to display localized strings on certain pages, you can restrict localization behavior throughout your application by putting the resource files in the App_localdirectory folder. This will make localization a page-specific, rather than a full application-wide. The names should look like this (assuming you only want to localize a page named mypage.aspx):

MyPage.aspx.resx: This is the default resource file for MyPage.aspx.

Mypage.aspx.fr-fr.resx: When culture changes to French, it is used, but only the mypage.aspx in the application is localized.

All the. resx files above will be compiled into the runtime assembly. These assemblies are called "small assemblies" and provide a strongly typed wrapper for the. resx file. Therefore, we do not need to worry about creating a resource assembly by ourselves in ASP.net 2.0. After you publish your site, these assemblies are placed in a separate folder under the/bin folder (named locale):

For non-ASP.NET applications, we need to use two tools:

1. resource file Builder (Resgen.exe);

2. Assembly Linker (Al.exe).

There are a lot of details about how to use these tools in MSDN. Interested readers can look for them.

Now that we've created resource files for different cultures and languages, next, when users change culture dynamically, we need a way to load them at run time. Fortunately, this is fairly easy to achieve in asp.net 2.0. See the following code:

String welcome = Resources.TestSiteResources.Welcome;
In this line of code, we use the resources namespace, which is created automatically by ASP.net, which compiles the resource file into a "small" assembly and uses the same testsiteresources class as the resource file name we created. We then access the Welcome property, which provides the actual text from the resource file based on the current culture. If we want the text of the label control Lblwelcome, we can use the two methods provided in ASP.net 2.0 to make the same setting:

1. Implicit localization: Here, we specify a new meta tag in the control definition and let ASP.net get the value from the resource file based on the ResourceKey property:

<asp:label id=lblwelcome meta:resourcekey= "lblwelcome" Text = "Welcome"
runat= "Server" > </asp:Label>
To enable this to work, we need to put the page-specific resource files under the/app_localdirectory folder. Implicit localization helps reduce the size of global resource files and helps improve global resource management. You can use this method when you have most of the page-specific content.

You don't have to do anything by hand to set these implicit localization properties. Just open your Web page in the design mode and go to "tools->generate local resources". This will automatically create a resource file for your page. In the Visual Studio 2005 resource file Editor, you only need to set different field values (Control.property) for each control.

2. Explicit localization: Use this technique when we have global resource files. Here, we use an expression to set the value from the resource file, as follows:

<asp:label id=lblwelcome Text = "<%$ resources:testsiteresources, Welcome%>" runat= "Server" >
</asp:Label>
We can use the VS IDE to implement this setting. Select the Label control, go to its Properties window, and select Expressions->text. Then, select "Resources" from the Drop-down box and enter the class name (in this case, testsiteresources) and the resource key (Banner). This is the recommended way to localize the UI controls in a page.


3. Programmatically access a strongly typed resource class as follows:

Lblwelcome.text = Resources.TestSiteResources.Welcome;
This method works, but it needs to be coded for each control in the page. Therefore, you can use the second method for all controls, and use this method to access resource strings that correspond to other content, if required. It should also be noted that controls such as the Calendar control implement built-in localized functionality. Once the uiculture and culture of the front line change, it automatically displays localized content, thanks to asp.net!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.