ASP. NET websites support multiple languages and ASP. NET Website Languages

Source: Internet
Author: User

ASP. NET websites support multiple languages and ASP. NET Website Languages

ASP. NETWebsite Supports multiple languages

(Call of local and global resource files and summary of notes)

 

I,Local resource file(App_LocalResources):

①,Generate and call local resources

1. Local resourcesVSAutomaticGeneration Method:

Adjust the page to the design view, and then in the VS2008 menu bar

[Tools] = "[generate local resources] to generate the resource files on this page, which can be viewed in the App_LocalResources folder. Eg: Default2.aspx. Resx

2. Local resourcesManualGeneration Method:

Select website = add new project = and select resource file to create a resource file. However, you can see the resource file you just created. resx is stored in the App_GlobalResources folder, which is currently a global resource file. You need to create a folder named App_LocalResources (local resource folder) on the website) then, drag the created global resource file to the current folder, and add meta: resourcekey = "to the control when calling"Resource Key"Code, detailed explanation in the call.

3. Local Resource calls:

(1) server controls

1.1,Front-endImplicit usage of local resources, VS2008 automatically generates local resources. The control automatically contains meta: resourcekey ="Resource Key"Code. If a control is not scanned, it will not have this code identifier. You need to manually add this code

Eg:

<Asp: Label ruant = "server" ID = "lbl_msg" meta: resourcekey ="Lbl_msg"> </Asp: Label>

 

1.2,Front-endShow use of localization Resources

Eg:

<Asp: Label ruant = "server" ID = "lbl_msg"

Text ="<% $ Resources: lbl_msg. Text %>"

ToolTip ="<% $ Resources: lbl_msg. ToolTip %>"

> </Asp: Label>

 

The blue icon and the red icon indicate the data binding format (display/hide), as shown in:

 

1.3,BackgroundUse local resources for encoding

 

Method 1:

// Obtain the resource value through the resource key in the background and assign the value to the Page Object

This.txt box1.Text = (string) GetLocalResourceObject ("Resource Key ");

 Or

This.txt box1.Text = (string) HttpContext. GetLocalResourceObject ("resource file name", "Resource Key ");

 

(2) Non-server controls (HTML controls, input ...) :

1.1 neither the html control nor the input control can automatically generate resource files. You need to manually add meta: resourcekey ="Resource KeyAnd add it to the resource file. Note that the server identity attribute must be added to the control, that is:Runat = "server"

Eg:

<Input id = "txt_msg" type = "text" meta: resourcekey ="Txt_msg"/>

Test result: the resource cannot be displayed.Txt_msgModify the value as follows:

<Input id = "txt_msg" type = "text" meta: resourcekey = "txt_msg"Runat = "server"/>

Test result: resources are displayed normally.Txt_msgValue

 

②. Notes:

 

1. Each page must contain a default local resource file. (the difference between the resource file name and the page file name is that the resource file name adds a. resx suffix to the page file name (including the file suffix)

Eg: index. aspx. Resx.

The role of the default local resource file: when other local resource files, eg: index. aspx. en-US.resx or... The resource file does not containKeyThe default local resource file containsKeyFrom the default local resource file.

2. If the page needs to support multiple languages, you can copy the default local resource file and modify its file name.

Eg: index. aspx.En-US. Resx (English)

Then index. aspx.En-USKeys in. resxChange the value to EnglishYou can.

3. 1) if you need to use the same language for all pages, you can configure the web. config file, so that the culture = "auto" uiCulture = "auto" command in the Page command is deleted. Otherwise, the language format in the Page is used first.

Eg:

<System. web>

<! -En-US (English), zh-CN (Chinese )...

UICulture-display controls such as buttons on the user interface
Culture-determines how data types are organized, such as numbers, dates, and currencies.
-->

<Globalization culture = "en-US" uiCulture = "en-US"/>

</System. web>

2) if a Page is different from other pages in different languages, you can set culture and uiCulture in a Page command.,These two attributes are used to enable the page to automatically load the resource files of the corresponding language based on the browser's language settings.

Eg:

<% @ Page culture = "zh-CN" uiCulture = "zh-CN" %>

4. if you wantBackground(That is, the. cs file) by passing ParametersSet the language used, Then

Eg:

Namespace reference:

Using System. Threading;

Using System. Globalization;

 

Protected override void InitializeCulture ()

{

String culture = Request. QueryString ["culture"]. ToString ();

If (culture! = "")

{

Thread. CurrentThread. CurrentCulture = CultureInfo. CreateSpecificCulture (culture );

Thread. CurrentThread. CurrentUICulture = new CultureInfo (culture );

}

Else

{

// Default Chinese

Thread. CurrentThread. CurrentCulture = CultureInfo. CreateSpecificCulture ("zh-CN ");

Thread. CurrentThread. CurrentUICulture = new CultureInfo ("zh-CN ");

}

Base. InitializeCulture ();

}

InitializeCulture (initialization language method) is rewritten by passing the encoding parameter to change the page language display mode.

5. The local resource file can only be called on this Page and cannot be called by other pages. If there are two pages Index1.aspx and Page 2. aspx, the following resource file

Eg: Index1.aspx. resx

Page2.aspx. resx

The Index1.aspx Page cannot call the resource key value in Page 2. aspx. resx;

Page 2. The aspx Page cannot call the resource key value in Index1.aspx. resx;

The Index1.aspx page can only call the resource key value in Index1.aspx. resx;

Page 2. The aspx Page can only call the resource key value in Page 2. aspx. resx;

 

II,Global Resource file(App_GlobalResources):

Global resource generation and calling

1,Global ResourceManualGeneration Method:

Select website = add new project = and select resource file to create a resource file. However, you can see the resource file you just created. resx is stored in the App_GlobalResources folder and is currently a global resource file.

 

2Front-endCall of global resource files:

1),Add<% $ Resources:Resource file name, Resource Key%>

Eg:

<Asp: Label ID = "lbl_global" runat = "server" Text ="<% $ Resources: lang,Lbl_global %>">

</Asp: Label>

 

2),Front-endPage-levelJavaScriptMediumCallGlobal Resource file method:

<% = Resources.Resource file name.Resource Key%>

Eg:

<Script>

Var msg ="<% =Resources.Lang. lbl_global %>"

</Script>

 

3,BackgroundCall of global resource files:

1),Use directly in the backgroundResources.Resource file name.Resource KeyTo obtain the resource value.

Eg:

This. lbl_global.Text =Resources. lang. lbl_global;

// Obtain the image type object in the background

System. Drawing. Bitmap imgJpg = Resources. lang. img_jpg

2),

// Obtain the resource value through the resource key in the background and assign the value to the Page Object

This. lbl_global.Text = (string) GetGlobalResourceObject ("Resource Key ");

Or

This. lbl_global.Text = (string) HttpContext. GetGlobalResourceObject ("resource file name", "Resource Key ");

 

3),The backend uses the ResourceManager class to obtain resources.

ResourceManager rm = new ResourceManager (Resource1.ResourceManager. BaseName,

Assembly. GetExecutingAssembly ());

String str = rm. GetString ("Resource Key ");

 

Note:

1,Global Resource files are stored in the App_GlobalResources folder with the. resx suffix. You must first add a default global resource file.

Eg: lang. Resx.

The role of the default global resource file: ① when other global resource files, such as: lang.En-US. Resx or lang.Zh-CN. Resx and other resource files do not containKeyThe default global resource file containsKeyThe default global resource file is used. ② When a resource is called, it can only be called by the default resource file name, that is

Resources. lang.Resource Key(Correct √)

But cannot use

Resources. lang. en-US.Resource Key; (Error ×)

Lang.En-US. Resx or lang.Zh-CN. Resx is automatically called only in different languages.

 

2,Global Resource files can be called on all pages of the website.

 

Other things that need attention are similar to local resources, so we will not repeat them here.

 

The above is my understanding of local and global resources,If there are any errors, please give them some guidance. ^. ^

 

Unsolved problems:

 

Unable to implement externalJavascriptIf you have a good solution to the resource calling problem, please share it. Thank you!

 

Related Articles on website support for multiple languages:

Http://www.cnblogs.com/FredTang/archive/2013/01/23/2873231.html

Http://www.cnblogs.com/wenjl520/archive/2010/10/17/1853367.html

Http://www.cnblogs.com/lansetiankong/archive/2010/07/30/1788790.html

Http://www.cnblogs.com/winsonet/archive/2010/09/08/1821107.html

Multilingual resource file production tools:

 

Http://www.cnblogs.com/LifelongLearning/archive/2010/07/04/1771019.html

 

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.