ASP. NET 2.0: Resources)

Source: Internet
Author: User

\ App_globalresources folder

Resource files are string tables that can serve as data dictionaries for your applications when these applications require changes to content based on things such as changes in culture. you can add Assembly resource files (. resx) to this folder, and they are dynamically compiled and made part of the solution for use by all your. aspx pages in the application. in addition to strings, you can also add images and other files to your resource files.

Webresource. resx

Name Value
Button1caption Caption of Button 1

Webresource. zh-CN.resx

name value
button1caption button 1 title
in order to see the Chinese text, change your preferred culture in the Microsoft Internet Explorer browser by choosing tools allow Internet Options . this pulls up the Internet Options dialog. from the first tab, General , you can click the languages button to pull up a dialog that enables you want to specify the Chinese language as your preferred language choice. after you have added the Chinese language to the list, be sure that it is the uppermost choice in the dialog.

\ App_localresources

You can add resource files that are page-specific to the \ app_localresources folder by constructing the name Of the. resx file in the following manner:

Using default. aspx. resx

Name Value

Label1.text

Text of label1

Using default. aspx. zh-CN.resx

Name Value
Label1.text Label 1 Text


Now, the resource declarations used on the default. ASPX page will be retrieved from the appropriate file found in the \ app_localresources folder. by default, the default. aspx. resx resource file will be used if another match is not found. if the client is using a culture specification of fi-fi (Finnish), however, the default. aspx. fi. resx file will be used instead.

To use implicit Localization

Make sure that you have local resource files (. resx files) that meet the following criteria:
1. They are inApp_localresourcesFolder.
2. The base name matches the Page name. For example, if you are working with the page named default. aspx, the resource files are namedDefault. aspx. resx(For the default resources ),Default. aspx. es. resx, Default. aspx. es-mx.resx, and so on.
3. The resources in the file use the naming conventionResourcekey."Property". For example, key nameButton1. "text".

In the control markup, add an implicit localization attribute.

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

To use explicit Localization

In the markup for a control, use a resource expression to set the value for each property that you want to replace with a resource. The syntax is as follows:

 
<% $ Resources: Class, resourcekey %>
    • ClassIs the resource file class, which is based on the. resx file name.

      A resource file named webresources. resx uses the class nameWebresources. All culture variant resource files use the same class name as the culture neutral resource file.If you want to obtain a resource from the local resource file that is associated with a page,ClassIs optional.

    • ResourcekeyIs the name of a resource in the specified class.

< ASP: button ID = " Button1 " Runat = " Server "  
Text = " <% $ Resources: webresources, button1caption %> "   />

< ASP: Label ID = " Label1 " Runat = " Server " Height = " 101px " Text = " <% $ Resources:, label1.text %> " Width = " 122px " > </ ASP: Label >

 To retrieve resource values programmatically

< Script runat = " Server " >
Protected   Void Button#click ( Object Sender, eventargs E)
{
Button1.text =  
Getlocalresourceobject ( " Button1.text " ). Tostring ();
Image1.imageurl =  
(String) getglobalresourceobject (
" Webresourcesglobal " , " Logourl " );
Image1.visible =   True ;
}
</ Script >

To retrieve global resources using strong typing

String welcome;
Welcome = Resources. webresources. welcometext;

How to: Set the culture and UI culture for ASP. NET web page globalization

In an ASP. NET web page, you can set to two culture values,CultureAndUicultureProperties. the culture value determines the results of culture-dependent functions, such as the date, number, and currency formatting, and so on. the uiculture value determines which resources are loaded for the page.

To set the culture and UI culture for an ASP. NET web page declaratively

< Configuration >
< System . Web >
< Globalization Culture = "Auto" Uiculture = "Auto" />
</ System. Web >
</ Configuration >

< % @ Page Uiculture = "En-us" Culture = "En-us" % > To set the culture and UI culture for an ASP. NET web page programmatically Protected   Override   Void Initializeculture ()
{
System. Threading. thread. currentthread. currentculture = System. Globalization. cultureinfo. createspecificculture ( " Zh-CN " );
System. Threading. thread. currentthread. currentuiculture =   New System. Globalization. cultureinfo ( " Zh-CN " );

// Or
// Page. Culture = "ZH-CN ";
// Page. uiculture = "ZH-CN ";

Base . Initializeculture ();
}

Select an encoding for ASP. NET web page globalization < Configuration >
< System . Web >
< Globalization
Fileencoding = "UTF-8"
Requestencoding = "UTF-8"
Responseencoding = "UTF-8"
Culture = "En-us"
Uiculture = "De-de"
/>
</ System. Web >
</ Configuration >

%@ page requestencoding =" UTF-8 " responseencoding =" UTF-8 " % >

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.