Gradually explanation: Internationalization of Web Form Web

xiaoxiao2021-03-06  14

[Selected from: http://msdn.microsoft.com/library/cht/default.asp? URL = / library / cht / vbcon / html / vbwlkwalkthroughlocalizingwebforms.asp]

The Visual Studio system does not automatically generate resource files when you use the Web Form web page. You must manually establish and edit XML resource files. This topic will demonstrate how to join the resource file, then use XML editing, establish a resource in English, Chinese. This topic also discusses how to write procedures for accessing these resources.

You can also convert text content into resource files. For details, see the resource and resource file generator (ResGen.exe) text format.

A. How to create and edit resource files manually

1. Establish an ASP.NET web application called LocProject. For details, see Establishing a web project.

Note: Project name will be used in the program LocProject

2. In the project, join the new item (Add New Item ...)

3. Select "Assembly Resource File. The file name is Strings.resx (there is no "in the middle of the name.", Otherwise, it is easy to erroneously). The resource file Strings.resX will contain the original resources of English. These resources will be accessed whenever the app is more suitable for the UI cultural feature.

The file will be added to the project and open in the XML design tool.

4. In the "Data Tabls :) list, select" DATA ".

5. In the Data (Data :) page, add 2 lines of data TXTSEARCH and TXTWELCOME as follows:

Name ValuetXTSearch Search for the Following Text: Txtwelcome Welcome To Localization!

6. Repeat 1 time step 2 to 5, establish another data file named strings.en-chs.resx.resx, the data is as follows:

Name ValuetXTSearch Search String: TXTWELCOME Welcomes localization!

B. How to access resources

1. Add System.Resources and System.Globalization namespaces. The following code is as follows:

'Visual BasicImports System.ResourcesImports System.globalization

Public class Webform1 inherits system.web.ui.page

// c # using system.resources; using system.global;

Namespace LocProject

2. Declare the variable of the ResourceManager class LOCRM

'Visual Basic Public Class Webform1 Inherits System.Web.ui.page Protected Locrm As ResourceManager

// c # namespace LocProject_cs {public class Webform1: system.Web.ui.page {protected resourcemanager LOCRM

3. Add the following code to WebForm1 Method Page_Load to create an instance of the ResourceManager class. The ResourceManager constructor uses two parameters. The first is the root name of the resource file, that is, the resource file name without cultural characteristics and .resx suffix. The second parameter is the main component. 'Visual Basic LoCRM = New ResourceManager ("LocProject.strings", gettype (webform1) .assembly)

// c # LoCRM = New ResourceManager ("LocProject.strings", Typeof (WebForm1) .assembly);

Note: If you do not name the project is LocProject, you should modify the root name of the resource file.

C. How to display a static resource string in the page

Place the code between the

and tag (TAG):

<% = LOCRM.GETSTRING ("txtwelcome")%>

Note: Size in ResourceManager.

D. How to give the resource string to the properties of the control

1. Add code:

'Visual BasicLabel1.Text = LoCrm.getstring ("txtsearch") label2.text = LoCrm.getstring ("txtwelcome")

//C#Label1.text =LOCRM.GETSTRING ("tSearch ") ;Label2.text =LOCRM.GETSTRING ("twelcome");

2. Increase system.threading namespace.

'Visual Basic' Import this Namespace at the beginning of the code moduleimports system.threading

// C # // Import this namespace at the beginning of the code module.using system.threading;

3. Add the following code to the beginning of the page_load method, specifying the Web Form web page to display the resources and formats in accordance with the cultural characteristics specified in the browser's Accept-Language title. This code is quite important, because if not, your web application will follow the server's cultural specific settings, and this may not apply the remote user of the web form page.

'Visual Basic' Set the culture and UI culture to the browser's accept languageThread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture (Request.UserLanguages ​​(0)) Thread.CurrentThread.CurrentUICulture = New CultureInfo (Request.UserLanguages ​​(0))

// C # // Set the culture and UI culture to the browser's accept languageThread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture (Request.UserLanguages ​​[0]); Thread.CurrentThread.CurrentUICulture = new CultureInfo (Request.UserLanguages ​​[0]); Compile, execution can see the result. In the Control Panel / Area option, if you choose: Chinese (China), a Chinese web page is displayed; if you choose other, an English web page is displayed. [Summary of the steps:] 1) create a string resource file need to use 2) is provided Thread.CurrentThread.CurrentCulture acquisition area client = CultureInfo.CreateSpecificCulture (Request.UserLanguages ​​[0]); Thread.CurrentThread.CurrentUICulture = new CultureInfo.. (Request.Userlanguages ​​[0]); 3). Create an object to read the resource string (the code must be in 2).) 'Visual Basic LoCRM = New ResourceManager ("LocProject.Strings", gettype (WebForm1) .assembly ) // c # LoCRM = New ResourceManager ("LocProject.Strings", TypeOf (WebForm1) .assembly); 4). Read the resource string, complete the interface international label2.text = LoCrm.getstring ("txtwelcome";

转载请注明原文地址:https://www.9cbs.com/read-49435.html

New Post(0)