Globalization Based on ASP. NET

Source: Internet
Author: User

Globalization Based on ASP. NET

Due to the project relationship, ASP. NET has been studying globalization issues over the past two days. I know that there is a dedicated I18N Processing Solution in the JAVA System, which is not possible in. NET. Therefore, I checked the information on the Internet. After some hard work, it was slightly reduced. I feel that there are no complete solutions on the Internet (or I may not find them), so I spent some time summing up my lessons and hope to share them with you. If you have any shortcomings, please kindly advise.
This article describes the basic steps and notes for implementing globalization in ASP. NET by creating an actual project in VS. Net2002.
Step 1: create a Web Application named TestRM. This project will include
-AssemblyInfo. cs
-TestRM. csproj
-TestRM. csproj. webinfo
-TestRM. sln
-TestRM. suo
-TestRM. vsdisco
-WebForm1.aspx (including aspx. cs and aspx. resx)
-Global. asax (including asax. cs and asax. resx)
-Web. Config
Step 2: Create resource files in different languages for the project. In.. Net, the resource file extension is. resx. This file is basically XML,. NET provides a very convenient resource file editing tool (I personally feel like XmlSpy. Here, we use simplified Chinese (zh-cn) and American English (en-us) as examples. Use "Project-> Add new item-> resource file" to add strings to the project. en-us.resx and string. the resource file for the zh-cn.resx, and in the string. add a record in the en-us.resx with the content name: String001; value: Welcome, in string. add a Record name: String001; value: Welcome to the zh-cn.resx. The XML file structure is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Root>
...
<Data name = "String001">
<Value> welcome </value>
</Data>
...
</Root>
If you do not want to change the language dynamically by modifying the configuration file, but want to change the language by setting the "region Settings" function of Windows, skip step 3. (Is there such a demand ??? -_-)
In this way, after the project is compiled, two subdirectories, en-us and zh-cn, are displayed under the bin directory, with a dll file respectively. The name is TestRM. resources. dll.
Step 3: Modify the Global. asax and Web. Config files to dynamically switch languages.
First, add the following code to the Application_BeginRequest event of Global. asax:
Protected void Application_BeginRequest (Object sender, EventArgs e)
{
Thread. CurrentThread. CurrentCulture = CultureInfo. CreateSpecificCulture (ConfigurationSettings. deleettings ["DefaultCulture"]);
}
This is the simplest application. Of course, you can store some configuration items in cookies.
Then, add the following code in Web. Config:
<Deleetask>
<Add key = "DefaultCulture" value = "zh-cn"/>
</AppSettings>
Note that the deleettings element and system. web belong to the same level.
Finally, add the following code to the Page_Load event of WebForm1:
Public class WebForm1: System. Web. UI. Page
{
Private void Page_Load (object sender, System. EventArgs e)
{
WebForm1 form1 = new WebForm1 ();
ResourceManager resManager = new ResourceManager (form1.GetType (). Namespace + ". string", form1.GetType (). Assembly);
System. Globalization. CultureInfo ci = System. Threading. Thread. CurrentThread. CurrentCulture;

Response. Write (resManager. GetString ("String001", ci ));
}

...
}
Please note that the above Code contains the line of underlined code, in. NET 2002, if you use this. getType (). namespace and this. getType (). assembly. when FullName is obtained, it turns out to be ASP and 6fqpc_hh, Version = 0.0.0.0, Culture = neutral, PublicKeyToken = null. The obtained Assembly is obviously randomly generated. But it is normal in c # class files and WinForm, and it is unclear why. Needless to say, the results certainly cannot find the corresponding resource file.
In addition, it should be emphasized that form1.GetType () is used here (). namespace + ". string ". If the resource file is not placed in the project root directory but in a subdirectory, modify the default namespace of the project. This is because after the resource file is compiled, it is stored in the project's resource file TestRM. resources. the following line appears in the dll Manifest (viewed using ILDASM ,. mresource public 'testrm. string. zh-CN.resources 'or. mresource public 'testrm. string. en-US.resources '. Its basic composition rules are:Project default namespace+Directory Name of the resource file+Resource file name[+Language], If the resource file of a project is not in any language, for example, we define a resource file named string. resx resource file. After the project is compiled, subdirectories such as zh-CN and en-US will no longer be generated in the bin directory, the resource file will be directly embedded in the Assembly of the project. At the same time, the Manifest of the Project Assembly contains the following line. mresource public 'testrm. string. resources '. Therefore, if the resource file is not placed in the root directory of the project and the default namespace is not changed, you are trying to use resManager. when GetString is used, the system will be prompted that "resource files in any language cannot be accessed" because it is described above.
Basically, the above are some of my experiences in implementing globalization based on ASP. NET, hoping to help some friends.

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.