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

NameValue
Button1CaptionCaption of Button 1

WebResource.zh-CN.resx
NameValue
Button1Caption按钮1标题
None.gif 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

NameValue

Label1.Text

Text of Label1

  ❑ Default.aspx.zh-CN.resx
NameValue
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.

None.gif < asp:Button ID = " Button1 "  runat = " server "  Text = " DefaultText "  
None.gif    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.

None.gif < asp:Button ID = " Button1 "  runat = " server "  
None.gif    Text
= " <%$ Resources:WebResources, Button1Caption %> "   />

None.gif < asp:Label ID = " Label1 "  runat = " server "  Height = " 101px "  Text = " <%$ Resources:, Label1.Text %> "  Width = " 122px " ></ asp:Label >

 To retrieve resource values programmatically

None.gif < script runat = " server " >
None.gif    
protected   void  Button1_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        Button1.Text 
= 
InBlock.gif            GetLocalResourceObject(
"Button1.Text").ToString();
InBlock.gif        Image1.ImageUrl 
= 
InBlock.gif            (String)GetGlobalResourceObject(
InBlock.gif            
"WebResourcesGlobal""LogoUrl");
InBlock.gif        Image1.Visible 
= true;
ExpandedBlockEnd.gif    }

None.gif
</ script >
None.gif

To retrieve global resources using strong typing

None.gif String welcome;
None.gifwelcome 
=  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

None.gif < configuration >
None.gif        
< system .web >
None.gif            
< globalization  culture ="Auto"  uiCulture ="Auto" />
None.gif        
</ system.web >
None.gif    
</ configuration >

None.gif < %@ Page  UICulture ="en-US"  Culture ="en-US"  % >

To set the culture and UI culture for an ASP.NET Web page programmatically

None.gif      protected   override   void  InitializeCulture()
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        System.Threading.Thread.CurrentThread.CurrentCulture 
= System.Globalization.CultureInfo.CreateSpecificCulture("zh-CN");
InBlock.gif        System.Threading.Thread.CurrentThread.CurrentUICulture 
= new System.Globalization.CultureInfo("zh-CN");
InBlock.gif
InBlock.gif        
//or
InBlock.gif        
//Page.Culture = "zh-CN";
InBlock.gif        
//Page.UICulture = "zh-CN";
InBlock.gif

InBlock.gif        
base.InitializeCulture();
ExpandedBlockEnd.gif    }

Select an Encoding for ASP.NET Web Page Globalization 
None.gif < configuration >
None.gif  
< system .web >
None.gif    
< globalization
None.gif      
fileEncoding ="utf-8"
None.gif      requestEncoding
="utf-8"
None.gif      responseEncoding
="utf-8"
None.gif      culture
="en-US"
None.gif      uiCulture
="de-DE"
None.gif    
/>
None.gif  
</ system.web >
None.gif
</ configuration >

None.gif < %@ Page  RequestEncoding ="utf-8"  ResponseEncoding ="utf-8"  % >

转载于:https://www.cnblogs.com/RobotTech/archive/2007/08/17/859985.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值