Super simple: ASP. NET localization (localization, multi-language)

Source: Internet
Author: User

There are many discussions about ASP. NET localization (localization, multi-language ).ArticleThis article will not discuss ASP. NET localization (localization, multi-language) in depth ). On the contrary, it will give you a quick reference on localization of the content commonly used in Asp.net pages, including ASP. NET Server controls, HTML content, sitemap, and some other resources.

This article includes the following content:

1. How to localize ASP. NET Server controls?

2. How to localize HTML content?

3. How to localize site maps?

4. How to dynamically change the cultural environment?

How to localize ASP. NET Server controls?

Localization of ASP. NET Server controls is the simplest of all types. Once you add a server control to your page, you can simply switch the page to "design", and then go to the menu "Tools"-> "generate local resource ".

It generates a string resource for each Asp.net Server Control on the page. In this example, createDefault. aspx. resx. It containsDefault. aspxName/value pairs of all resources on the page.

Switch back to "Source", you will see the following added in your HTMLCode:

< ASP: button ID = "Button1" Runat = "Server" Text = "Hello"  
Meta: resourcekey = "Button1resource1"   />

Then, you can copy/paste it to create another cultural resource file. For example, you can create a default. aspx. Fr. resx for a French user.

 

The following is when the language of Internet Explorer is set to English.

 

Use Internet Explorer> Tools> Internet Options> languages to change the language to French.

 

The following is a page for the French version:

 

How to localize HTML content?

To localize common HTML content, you can use the <asp: localize> control. Let's use an example.

On Your webpage, you have a title and paragraph.

< H1 > Localization page header </ H1 >
< P > This is a demo page to show you how to do localization in ASP. NET </ P >

For localization, you need to add <asp: localize> to them.

Code

< H1 > < ASP: localize ID = "Header" Runat = "Server" > Localization page header </ ASP: localize > </ H1 >
< P > < ASP: localize ID = "Localize1" Runat = "Server" > This is a demo
Page to show you how to do localization in ASP. NET </ ASP: localize > </ P >

 

Then, you can manually create a resource file and add meta: resourcekey = "headerresource1" to your webpage. You can use Visual Studio, which is automatically generated. Switch to "design" and choose "Tools"> "generate local resource ".

It generates the following code and resource files (for example, Yourfile. aspx. resx) for you ).

The following code is changed:

Code

< H1 > < ASP: localize ID = "Header" Runat = "Server"  
Meta: resourcekey = "Headerresource1"  
Text = "Localization page header" > </ ASP: localize > </ H1 >
< P > < ASP: localize ID = "Localize1" Runat = "Server"  
Meta: resourcekey = "Localize1resource1" Text = "This is a demo page to show you
How to do localization in ASP. NET" > </ ASP: localize > </ P >

The remaining steps are the same as how to localize ASP. NET Server controls.

How to localize sitemap?

This article will give you more details about sitemap localization.

1. Add enablelocalization = "true" to enable localization of website map files. For example, in the web. sitemap file.

< Sitemap Xmlns = "Http://schemas.microsoft.com/AspNet/SiteMap-File-1.0"
Enablelocalization = "True" >

2. Use the format "$ resources: classname, keyname, defaultvalue" in sitemapnode to change the attribute value of the resource string to be localized.

< Sitemapnode URL = "Default. aspx"  
Title = "$ Resources: sitemaplocalizations, homepagetitle"  
Description = "$ Resources: sitemaplocalizations, homepagedescription" >

3. Add global resource folders and files. Right-click the web site in Solution Explorer. Click "add ASP. NET folder ". Click "add app_globalresources file" from the sub-menu ". This will add a "app_globalresources file" folder to the root directory.

Add a resource file, for example, sitemaplocalizations. resx. In the file, you will have the name/value pair of the resource string. For example:

NAME value
Homepagetitle home

4. Add resource files for each culture you need. For example, you may have a file named sitemaplocalizations. Fr. resx in French. The following is a menu screen in English and French:

 

How to program local strings?

Sometimes you may need to display a string, for example, an error message at runtime. You need to program to localize it. The following describes how to do this:

1.App_globalresourcesAdd a resource file under the folder. For example, we can add aCommonresource. resx"File and a French version"Commonresource. Fr. resx".

2. There are many other good methods to obtain resource strings.

You can~ \ App_globalresources \ mymessages. resxThe obtained resource passes:

1. generated encapsulation code:

String Message = Resources. mymessages. Hello;

2. Resource expression

< ASP: Label Text = "<% $ Resources: mymessages, hello %>"   />

3,Getglobalresourceobject Method

String Message = Getglobalresourceobject ( " Mymessages " , " Hello " );

You can~ \ App_localresources \ default. aspx. resxThe obtained resource passes:

1. Resource expression:

< ASP: Label Text = "<% $ Resources: Hello %>"   />

2. Meta: resourcekey:

< ASP: Label Meta: resourcekey = "Labelresourcekey"   />

 3. getlocalresourceobject method:

String Message = Getlocalresourceobject ( " Hello " ); "

 This includes:

How to change culture dynamically?

In the sample code, we added two linkbuttons controls. When you click "English", the webpage will switch to the English culture. Click "français" to switch to the French culture. To check which linkbuttons is clicked, we use request. Form ["_ eventtarget"]. It can be implemented, but it may not be the best way.

We overwrite the initializeculture method on the page, so that the correct culture is displayed for the choices we make.

< ASP: linkbutton ID = "Languageenglish" Text = "English" Runat = "Server" > </ ASP: linkbutton >
< ASP: linkbutton ID = "Languagefrench" Text = "Français" Runat = "Server" > </ ASP: linkbutton >

 

Code

Protected   Override   Void Initializeculture ()
{
String Language = Request. Form [ " _ Eventtarget " ];
String Languageid =   "" ;

If ( ! String . Isnullorempty (language ))
{
If (Language. endswith ( " French " ))
Languageid =   " Fr-fr " ;
Else Languageid =   " En-US " ;
Thread. currentthread. currentculture =  
Cultureinfo. createspecificculture (languageid );

Thread. currentthread. currentuiculture= NewCultureinfo (languageid );
}
Base. Initializeculture ();
}

 

All controls except menu are valid. If you have a menu using sitemap on the webpage, you may need to call menu. databind () or set enableviewstate to false in the page_load event. Otherwise, the other content is changed to a new culture, and your menu will still display the previous cultural string.

When we dynamically change the culture, the most likely thing is that we need to store it somewhere and when we jump from the page to the page, it will not be lost. Application in the exampleProgramWe use session.

Source code:

/Files/zhuqil/localization.zip

Original article: http://www.codeproject.com/KB/aspnet/AspNetLocalization.aspx

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.