ASP.NET 2.0: Resources (转)

\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 按钮1标题

In order to see the Chinese text, change your preferred culture in the Microsoft Internet Explorer browser by choosing Tools➪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 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:

  ❑ Default.aspx.resx

Name Value

Label1.Text

Text of Label1

  ❑ Default.aspx.zh-CN.resx
Name Value
Label1.Text 标签1文本

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 in an App_LocalResources folder.
   2. The base name matches the page name.  For example, if you are working with the page named Default.aspx, the resource files are named Default.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 convention resourcekey."property".  For example, key name Button1."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 %>
  • Class is the resource file class, which is based on the .resx file name.

    A resource file named WebResources.resx uses the class name WebResources. 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, Class is optional.

  • ResourceKey is 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 Button1_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, the Culture and UICulture properties. 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" %>
原文地址:https://www.cnblogs.com/RobotTech/p/859985.html