1. Introduction
For websites that support multiple languages, you can dynamically select the language displayed on the website. This article describes how to use globalresource in ASP 2.0 (C #) for multi-language support. If you are used to using VB, you can make a reference.
2. Access to basic resource files
Right-click a website project and choose "Add new project" from the shortcut menu, as shown in:
After you click "add", a prompt box will pop up, indicating that a resource file will be created and whether to put the resource file in the corresponding folder. Select "yes, then, you can view the new folder and new resource files in the resource manager, for example (in the figure, we use the default resource. resx name changed to lan. resx ):
2.2 access resource content
Different resource files can be placed under app_globalresources. Each resource file is used to store names, values, and annotations. The name is a string, and the value is the value corresponding to the string. It can be seen as the relationship between keys and values in map. A resource file can contain multiple strings with different names.
In, the Resource Name is lan. resx, which has a stringString1, The corresponding value isHi!, Use the following method to obtain the string1 value in C:
Using system; using system. configuration; using system. data; using system. LINQ; using system. web; using system. web. security; using system. web. ui; using system. web. UI. htmlcontrols; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. XML. LINQ; using system. globalization; // using system is required. globalization namespace public partial class _ default: system. web. UI. page {protected void page_load (Object sender, eventargs e) {string a = resources. lan. string1; // use system. resources class in the globalization namespace to obtain the string1 value in the LAN resource file }}
The method for obtaining resource content in C # is as follows:
1. The using system. Globalization namespace is required.
2. Use the resources class in the system. Globalization namespace to obtain the string1 value in the LAN resource file. If the resource file is the default resource. resx, the access method is:
Resources. Resource. string1
The method for accessing resource content in. aspx is as follows:
<% $ Resources. Lan, string1 %>
3. multi-language support
3.1 create a resource file
Now we create three resource files named respectively:
Lan. resx
Lan. zh-cn.resx
Lan. En. resx
The default languages, Chinese characters, and English strings are stored under these three files. The LAN is the identifier of the program when accessing the resource file. By default, lan. resx is accessed. If simplified Chinese is specified, the LAN. zh-cn.resx is accessed. If English is specified, lan. En. resx is accessed. The key to multi-language implementation is how to make programs dynamically select resource files.
Enter a string in the preceding three resource files.HelloAnd the corresponding values areHello,Hello,HelloThe string hello is Hello by default. It is hello in Chinese and hello in English.
3.2 Create a file named basepage. CS
After you click Add, a dialog box is displayed. Select Yes.
The basepage. CS code is as follows:
/*** @ File basepage. CS * @ brief all our pages are inherited from the page, and the basepage here is also inherited from the page. * after that, all the pages we create must be inherited from the basepage. The basepage implements language switching by rewriting the initializeculture method in the page. * This is the key to multilingual implementation. * ** @ author Don Hao * @ date 2011-09-22 22:48:39 * @ version * <PRE> <B> copyright: </B> </PRE> * <PRE> <B> Email: </B> hao.limin@gmail.com </PRE> * <PRE> <B> company: </B> http://blog.csdn.net/donhao </PRE> * <PRE> <B> All Rights Reserved. </B> </PRE> * <PRE> <B> modification: </B> </PRE> * <PRE> write modifications here. </PRE> */using system; using system. data; using system. configuration; using system. globalization; // usingusing system is required. threading; // usingusing system is required. web; using system. web. security; using system. web. ui; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. web. UI. htmlcontrols; // <summary> // custom base page used for all web forms. /// </Summary> public class basepage: Page // all the previous pages are inherited from the page, and the basepage here is also inherited from the page, after that, all pages created by ourselves must inherit from the basepage. The basepage implements language switching by overwriting the initializeculture method in the page {protected override void initializeculture () {try {If ("cn" = httpcontext. current. session ["Lan"]. tostring () // we use session to determine whether the current display is English or Chinese {thread. currentthread. currentculture = cultureinfo. createspecificculture ("ZH-CN"); thread. currentthread. currentuiculture = new cultureinfo ("ZH-CN");} else {thread. currentthread. currentculture = cultureinfo. createspecificculture ("en"); thread. currentthread. currentuiculture = new cultureinfo ("en") ;}} catch (system. exception ex) // if no LAN is set in the session, the English {thread. currentthread. currentculture = cultureinfo. createspecificculture ("en"); thread. currentthread. currentuiculture = new cultureinfo ("en");} base. initializeculture ();}}
In default, click the button to dynamically switch the Display language. The default. aspx code is as follows:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">
The default. aspx. CS code is as follows:
/*** @ File default. axps. CS * @ brief inherited from basepage *** @ author Don Hao * @ date 2011-09-22 22:48:39 * @ version * <PRE> <B> copyright: </B> </PRE> * <PRE> <B> Email: </B> hao.limin@gmail.com </PRE> * <PRE> <B> company: </B> http://blog.csdn.net/donhao </PRE> * <PRE> <B> All Rights Reserved. </B> </PRE> * <PRE> <B> modification: </B> </PRE> * <PRE> write modifications here. </PRE> */using system; using system. configuration; using system. data; using system. LINQ; using system. web; using system. web. security; using system. web. ui; using system. web. UI. htmlcontrols; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. XML. LINQ; public partial class _ default: basepage // note that this parameter is inherited from basepage {protected void page_load (Object sender, eventargs e) {} protected void button#click (Object sender, eventargs e) {session ["Lan"] = "cn"; response. redirect (request. URL. tostring (); // note that the basepage setting method is always executed first, therefore, you need to refresh the page after writing the session.} protected void button2_click (Object sender, eventargs e) {session ["Lan"] = "en"; response. redirect (request. URL. tostring ());}}
Press F5 to execute it.
4. Summary
The basic idea is: first place the values displayed in different countries into different resource files and mark them with a unique string. Assume that the resource name is lan:
A. the lan. resx file is used to store the default strings. You need to find this file during compilation, so this file is required.
The B. Lan. XYZ. resc file is used to store the string corresponding to country XYZ:
Thread. currentthread. currentculture = cultureinfo. createspecificculture ("XYZ ");
Thread. currentthread. currentuiculture = new cultureinfo ("XYZ ");
To change the resource file selected by the website to XYZ.
C. The method to change the language resource file is as follows:
First, inherit the page class by using a class (assuming basepage), and override the initialization method. In this method, determine the language to display based on the value in the session (or other methods ).
Secondly, every page is inherited from the page class, so now we need to inherit from the basepage class.
D. Use a string string1 to access the corresponding resource value:
In the. CS file: resources. Lan. string1
In. aspx: <% $ resources. Lan, string1 %>
E. For a page using masterpage, mastpage. CS does not need to change its inheritance relationship. You only need to change the inheritance relationship of the background files of other. CS pages outside masterpage.
If you have any questions, please feel free to leave a message...