• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NET 2.0: Resources (转)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

\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)

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()
    }

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" %>

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
【ASP.NET】存储过程分页实例发布时间:2022-07-10
下一篇:
ASP.NET MVC下的四种验证编程方式发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap