Realizing globalization based on asp.net

Source: Internet
Author: User
Tags config resource thread
asp.net sharing, using this method is possible, the test through


As a first step, we create a Web application called TESTRM. This project will default to 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
In the second step, you need to create several different language resource files for your project. In. NET, the extension of the resource file is. resx, which is basic to XML, and Vs.net provides a very handy resource file Editing tool (personally feels like XMLSpy), which is not much to say. Here, for example, in Simplified Chinese (ZH-CN) and U.S. English (en-us). Add a new Item-> resource file from Project-> to the project by adding a resource file named String.en-us.resx and String.zh-cn.resx, and add a record to the String.en-us.resx with the name: String001;value:welcome, add a record to the String.zh-cn.resx Name:string001;value: welcome. Its XML file is structured as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<root>
...
<data name= "String001" >
<value> Welcome </value>
</data>
...
</root>
Here's one thing to say, if you don't want to change the language by changing the configuration file dynamically, but you want to switch languages by setting Windows regional Settings, skip step three. (Is there such a demand??? -_-)
This way, when this item is compiled, there will be two subdirectories of en-US and ZH-CN in the bin directory, each with a DLL file. Known as TestRM.resources.dll.
The third step is to modify the Global.asax and Web.config files to achieve dynamic switching of languages.
First, we need to add the following code to the Global.asax Application_BeginRequest event:
protected void Application_BeginRequest (Object sender, EventArgs e)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture (configurationsettings.appsettings[) Defaultculture "]);
}
This is the simplest application, of course, where you can store some configuration items in a cookie.
Then, add the following code to the web.config:
<appSettings>
<add key= "Defaultculture" value= "ZH-CN"/>
</appSettings>
Note that the appsettings element and the system.web belong to the same level.
Finally, add the following code to the WebForm1 Page_Load event:
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 this line of code underlined in the above code, in Vs.net 2002, if this is used. GetType (). Namespace and This.gettype (). Assembly.fullname get unexpectedly is ASP and 6fqpc_hh, version=0.0.0.0, Culture=neutral, Publickeytoken=null, obtained Assembly is apparently randomly generated. But it's normal in C # 's class files and WinForm, and it's not clear what the reason is. The result, of course, must not be found in the corresponding resource file.
In addition, the point to be emphasized is that the Form1 is used here. GetType (). Namespace + ". String", if the resource file is not placed under the project root, but is placed under a subdirectory, be careful to modify the project's default namespace. Because, after the resource file is compiled, there will be one line in the manifest (view with ILDASM) of the project's resource file TestRM.resources.dll. Mresource Public ' Testrm.string.zh-cn.resources ' or. Mresource public ' testrm.string.en-us.resources '. The basic composing rule is the project default namespace + resource file directory name + resource filename [+ language], if a project resource file is not in the language, For example, we define a resource file named String.resx, at which point a subdirectory, such as ZH-CN and en-us, will no longer be generated in the bin directory after the project is compiled, and the resource file will be directly embedded in the project's assembly. At the same time, the following line will be in the assembly manifest of the project. Mresource Public ' TestRM.string.resources '. Therefore, if the resource file is not placed at the root of the project and the default namespace is not changed, the attempt to use resmanager.getstring will be prompted with "No access to any language resource files", as described above.
Basically, these are some of the things I've learned in my research based on asp.net to achieve globalization, and I hope it will 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.