ASP. NET globalization and localization C # multi-language support
Http://blog.sina.com.cn/s/blog_45eaa01a0100robc.html
ASP. NET 2.0 and later development platforms provide tools for global localized applications and are easy to implement. The following describes how to use C # To implement C # in detail.
A few necessary concepts (I) support globalization as the Internet makes software easier to cross national boundaries, globalization is particularly important.
1. Globalization is the process of preparing applications for localization. This step involves identifying all the localized Resources in the application and separating them from executable code for easy modification.
2. localization capabilities during the localization capability phase of the preparation process, you need to check whether the conversion of the application really does not need to change the program code.
3. After localization, the application is customized for the new region in the localization phase of the preparation process.
(2) Regional
1. Understand that the Regional. NET Framework identifies the target users of localized content by specifying a certain culture. A region is a more accurate concept than a language. For example, American English and English are two different regions in the. NET Framework. To completely localize an application, you must rely on the region rather than the language.
2. The regional code. NET Framework uses a group of acronyms to represent a region. Each acronym consists of a regional code followed by one or more regional code. The regional code is a lowercase letter, while the secondary regional code is a capital letter. For example, · Ni-be identifies the Dutch (Belgium) region. Convenient for Dutch users living in Belgium. · Sr-SP-LATN this is an example of multiple sub-regions that identify the slogan in Serbia with a Latin letter. · Es identifies the Spanish region. This is a non-specific region, that is, the secondary region is no longer specified.
(3) Regional Attributes and Methods
1. The difference between currentuiculture and currentculture lies in the use of the. NET Framework. The currentuiculture attribute is used to retrieve regional-specific resources from an appropriate file (most importantly, user interface text ). The currentculture attribute is used to set the date, time, currency, and number formats, as well as other regional functions, including sorting order, string comparison rules, and packaging rules.
If you have not read the entire article, you can skip the following example to avoid confusion. 2. You can simply change the thread culture by using the cultureinfo class in the system. globallization namespace.
Change the regional using system; using system of a thread. globalization; using system. threading; Class test {public static void main () {thread. currentthread. currentculture = new cultureinfo ("de-de"); console. writeline (cultureinfo. currentuiculture. name );}}
2. Create a file system website open vs2005, choose "file"> "New Website" from the menu, and click "ASP. NET Website" under "templates installed in Visual Studio ". In the "location" box, enter the name of the folder to save the website page. For example, enter the folder name "C: \ websites ". In the language list, click the programming language C # You want to use #. Click OK ". The system (Visual Web Developer) creates this folder and a new page named default. aspx.
3. Implement implicit localization through ASP. NET
(1) automatically generate a resource file. Click "design" or "Source" under the default. aspx page to switch back and forth between the "design" view and "HTML source code" view.
1. Switch to the design view.
In the toolbox, drag a button control from the standard area to the page. Click the designer drawing or control. On the menu, choose "Tools"> "generate local resources". The system automatically creates a new app_localresources folder in the solution, and automatically creates a default folder for the corresponding page file name. aspx. resx new file. When implicit localization is used, the resource file name depends on the name of each page.
2. Switch to the "Source" view. View the changes to the control declaration. The system has added an attribute to the control to retrieve its value from the newly created resource file. For example, the tag of the button control has the new meta: resourcekey attribute. <Asp: button id = "button1" runat = "server" meta: resourcekey = "buttonresource1" text = "button"/>
3. the browser sends a regional request to the Web server. When the browser sends a request to the Web server, the request may contain information about the current language and culture. For example, a browser may send a string "En-us" to indicate that it has been set to use American English; another browser may send the string "ZH-CN" to indicate that it has been set to use Chinese.
4. roll back the regional resource file. This resource file is used as the default resource file for all requests. (It is a resource file for the rollback region .) If the browser does not specify any culture, or the browser request contains an unsupported language or culture, the resource value is extracted from the default file.
5. Use the resource editor to edit the resource file. Now you have created a resource file. You can use the resource editor to add localized text to the file. In solution Resource Manager, open default. aspx. resx. Under the "value" of the resource editor, the "text" attribute of each control placed on the page is listed. Changing the value here will change the value of the default culture. Set "buttonresource1.text" to local-auto-English. Save the file.
Test the page. Press Ctrl + F5 to run the page, and you will see the changes.
(2) Add other regions
A combination of each language and culture requires a unique resource file. To add other regions, you can use the default file as the start point. You can create resource files for different regions and regions by creating new resource files, the file name contains the ISO code (such as ZH-CN, en-US, fr-Ca, and en-GB ). These ISO codes are located between the Page name and. resx file extension, such as default. aspx. en-us.resx. To specify a language specific to a non-regional language, delete the Country Code. For example, if you specify a French language, use the file name default. aspx. Fr. resx.
1. In Solution Explorer, right-click the default. aspx. resx file and click Copy ". Right-click the app_localresources folder and click paste ". Right-click the copy default. aspx. resx file, click rename, and type default. aspx. zh-CN.resx ".
Create a default. aspx. en-US.resx file. Double-click the preceding two items and set "buttonresource1.text" of the button control to "localized"-"optional"-"Chinese" and "Local-Modify-English. And save the files separately.
The results are as follows: ------------------------------------------------ file default. aspx. zh-CN.resx content
NAME value buttonresource1.text localization-optional modification-Chinese -------------------------------------------------- file default. aspx. Content name value of the en-US.resx buttonresource1.textlocal-Modify-English example ----------------------------------------------------
Test the page and press Ctrl + F5 to run the page. The button is displayed as: local-Modify-Chinese
2. Use the alternate language settings to test whether ASP. NET is using this new resource file. You must first change the browser settings to the required culture for the request.
Change the language settings of IE. in Microsoft Internet Explorer, Click Tools> Internet Options ". Click language in the lower part ". In the language preferences dialog box, click Add ". In the "language" dialog box, click "English (US) [en-US]", and then click "OK ". Under "language" in the "language preferences" dialog box, click "English (US) [en-US]", click "Move Up" until the first place, and then click "OK ". Internet Explorer is now set to pass en-us as the language setting for all requests. When the default. aspx page sets the culture to auto, ASP. NET tries to locate the resource file and its corresponding values based on the language and regional preferences to generate the page.
Test the page and press Ctrl + F5 to run the page. The button is displayed as local-Modify-English.
3. in the file web. select <system. web> <globalization culture = "En-us" uiculture = "En-us"/> </system. web> here file web. the configuration in config overwrites the settings in IE browsing and is shared by all webpages.
4. select the language on the independent page and use the command <% @ page culture = "ZH-CN" uiculture = "ZH-CN" %> to overwrite the IE browser, and file web. config. For example: uiculture = "ZH-CN", regardless of the language of the file web. config and IE, Chinese information is displayed.
When the language in the Command <% @ page is set to uiculture = "Auto", the Web. config file is accepted first, and then the language setting sequence of IE is accepted.
5. Differences between culture and uiculture: culture is mainly used within languages such as date and currency, while uiculture is used to display controls such as buttons on the user interface.
4. Use explicit Localization
In the above section, you have used ASP. NET implicit localization to display localized text to the control. You have generated a resource file containing the property value, and added an attribute for each control during this process) indicates that the control fills in its property value (if any) from the resource file ). Implicit localization automatically works, that is, you do not need to specify the way each attribute reads information from the resource file one by one. However, you may need to set attributes more directly. To achieve this goal, implicit localization should not be used, but explicit localization should be used. Through explicit localization, you can use an expression pointing to a resource file to set the attribute value. When the page is running, the expression is calculated, the value is read from the specified resource file, and then the attribute is set using this value. Explicit localization is useful if a large amount of text or custom messages need to be localized in addition to controls and labels. For example, you can develop a series of localized welcome messages and thank you messages for e-commerce sites, and then place the text on the page using an explicit declarative expression. In addition, explicit localization allows you to maintain a localized resource file set, rather than maintaining a separate file set for each page.
1. create any resource name in this section, you will manually create resource files, and then use ASP. NET declarative expression syntax to reference these files. You will create a resource file for a simple thank-you message. Unlike the designer, you do not need to create a separate resource file for each ASP. NET page. In order to remember, write the base name of the resource file as my name wuchaotext. In the future, it will be the class name for distinguishing resources. For each language to be localized, an additional file is created and the file name contains the corresponding language code (sometimes there may be regional code ). For example, for Chinese characters, a file named wuchaotext. zh-CN.resx will be created. For us English, a file named wuchaotext. en-US.resx is created. Both files are stored in the resources directory of the Web application. Unlike the preceding implicit example, you do not need to maintain a resource file for each. ASPX page. Instead, you can maintain a series of files for each supported language or culture.
2. Create a global resource file. In Solution Explorer, right-click the root directory of the website, click Add ASP. NET folder, and then click app_globalresources ".
Right-click the "app_globalresources" folder and click "Add new project ". Under "templates installed in Visual Studio", click "resource files ". In the Name box, type wuchaotext. resx and click Add ". The wuchaotext. resx file will act as a resource for the rollback culture. Create two more. resx files and name them wuchaotext. zh-CN.resx and wuchaotext. en-US.resx. Double-click the preceding two. resx files. Type g_addstr in the first row of the "name" column. In the first row of the "value" column, enter globalization-you must manually add-Chinese and global-must-hand-add-English.
Results: ------------------------------------------------------ contents of the wuchaotext. zh-CN.resx File
NAME value g_addstr globalization-must be manually added-Chinese -------------------------------------------------- contents of the wuchaotext. en-US.resx File
NAME value g_AddStrglobal-must-hand-add-english ----------------------------------------------------
Save the two files and disable them.
Note: To insert a letter with an acute note (for example, í), use the keyboard to press Alt + 0237 when the number is locked. Now we have created two values for the resource named g_addstr. ASP. NET reads the value from the corresponding resource file based on the language set by the browser. Now you have created a resource file. You can return to the page and add controls that reference the resource.
3. Add a label control to the page
Switch to the default. aspx page and switch to the design view. Drag a label control from the toolbox to the page. Right-click the label control, click "properties", find the data (expressions), and click the ellipsis (…) in the box on the right (...) Button. The "expression" dialog box appears. Click text in the "binable attributes" list ". Select resources from the expression type list ". Under expression properties, set the class name key "classkey" to wuchaotext, and set the Resource Key "resourcekey" to g_addstr. Click OK ". Switch to the "Source" view. The text attribute of a tag now has an explicit expression, indicating that the base file of the resource and key to be selected will be retrieved from it.
<Asp: Label id = "label1" runat = "server" text = "<% $ resources: wuchaotext, g_addstr %>">
Note that the wuchaotext attribute does not have a language indicator, regional indicator, or. resx extension because it is not the actual file name. In fact, wuchaotext indicates the Base Resource class. ASP. net will select resources from a file with the corresponding language or regional code in the file name based on the Culture sent by the browser. The file name may be similar to wuchaotext. zh-CN.resx and wuchaotext. en-US.resx, Or, wuchaotext if no matching language is found. resx. Test the page and press Ctrl + F5 to run the page. During the test, the browser language is changed multiple times. Different Versions of text provided in the resource editor are displayed as the text of the label control.
5. webpage HTML source code
1. By default, the first line of the web page is created <% @ page Language = "C #" codefile = "default. aspx. cs" inherits = "_ default" %>
2. the first line of the webpage <% @ page Language = "C #" codefile = "default. aspx. CS "inherits =" _ default "Culture =" Auto "meta: resourcekey =" pageresource1 "uiculture =" Auto "%>
3. The final webpage source code is: the webpage is forcibly displayed in simplified Chinese
<% @ Page Language = "C #" codefile = "default. aspx. CS "inherits =" _ default "Culture =" ZH-CN "meta: resourcekey =" pageresource1 "uiculture =" ZH-CN "%>
<! 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">
Http://www.csharpwin.com/dotnetspace/483.shtml ASP. NET 2.0 localization features-localized web applications 3 http://www.pin5i.com/showtopic-7158.html ASP. NET 2.0 localization features: New Methods for localized web applications http://hi.baidu.com/heiru/blog/item/e885ea242381380c4c088db8.html Silverlight Localization