ASP.NET 2.0: Resources )

來源:互聯網
上載者: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 按鈕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" %>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.