1.设置当前文化
页面类有两个影响本地化的重要属性均在< % Page % >指令设置:
1).UICulture
此属性用于指定载入到页面的资源文件,资源文件可以包含页面文本内容的特定语言的翻译版本.其属性值可为中立文化也可为特定文化(下面有介绍)
2).Culture
此属性决定了诸如日期、数值、货币单位等的格式与排序。此属性值只能为特定文化.
以上UICulture和Culture属性都接受标准的文化名称作为它们的值。(可在http://www.IETF.org查看标准文化名称)
注:UICulture属性决定了为页面载入哪个资源文件,而Culture是决定了日期,数字,货币数量如何格式化
常用的文化名称如下:
zh-CN=Chinese(China)
en-US=English(United States)
en-GB=English(United Kingdom)
注:每个文化名称有两个部分组成。第一部分代表语言代码,第二部分代表国家/区域代码
即文化名称=语言代码-代表国家/区域代码
若只有语言代码,而无国家/区域代码的文化名称则为中立文化(neutral culture)
若即有语言代码,又有国家/区域代码的文化名称则为特定文化(specific culture)
1.1手动设置文化
实例手动在Page指令中设置:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Language="C#" UICulture="id-ID" Culture="id-ID" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<script runat="server">
void Page_Load()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
this.lblToday.Text = DateTime.Now.ToString("D");
this.lblPrice.Text = (512.3m).ToString("c");
}
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>印度尼西亚文化</title>
</head>
<body>
<form id="form1" runat="server">
<div>
今天的日期是:
<asp:Label ID="lblToday" runat="server" />
<hr />
商品的价格是:
<asp:Label ID="lblPrice" runat="server" />
</div>
</form>
</body>
</html>
实例以编程方式设置这些属性:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Language="C#" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<script runat="server">
void Page_Load()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void Page_PreRender()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
this.lblToday.Text = DateTime.Now.ToString("D");
this.lblPrice.Text = (512.3m).ToString("c");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected void btnSelect_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Page.Culture = ddlCulture.SelectedValue;
}
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>手动设置文化</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblCulture" Text="文化" AssociatedControlID="ddlCulture" runat="server" />
<asp:DropDownList ID="ddlCulture" runat="server"
DataTextField="DisplayName" DataValueField="Name" DataSourceID="srcCulture" />
<asp:Button ID="btnSelect" Text="选择" runat="server" OnClick="btnSelect_Click" />
<hr />
今天的日期是:
<asp:Label ID="lblToday" runat="server" />
<hr />
商品的价格是:
<asp:Label ID="lblPrice" runat="server" />
<asp:ObjectDataSource ID="srcCulture" runat="server"
TypeName="System.Globalization.CultureInfo" SelectMethod="GetCultures">
<SelectParameters>
<asp:Parameter Name="types" DefaultValue="SpecificCultures" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
实例用Profile记录用户的文化以便用户以后访问网站时自动进入记录的文化不必再设置(注意:有重写了InitializeCulture()此方法是为页的当前线程设置 Culture 和 UICulture):
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Language="C#" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<script runat="server">
//为页的当前线程设置 Culture 和 UICulture。
protected override void InitializeCulture()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Page.Culture = Profile.UserCulture;
Page.UICulture = Profile.UserUICulture;
}
void Page_Load()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//Page.Culture = Profile.UserCulture;
//Page.UICulture = Profile.UserUICulture;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void Page_PreRender()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
this.lblToday.Text = DateTime.Now.ToString("D");
this.lblPrice.Text = (512.3m).ToString("c");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected void btnSelect_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Profile.UserCulture = ddlCulture.SelectedValue;
Profile.UserUICulture = ddlCulture.SelectedValue;
Page.Culture = ddlCulture.SelectedValue;
}
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>手动设置文化</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblCulture" Text="文化" AssociatedControlID="ddlCulture" runat="server" />
<asp:DropDownList ID="ddlCulture" runat="server"
DataTextField="DisplayName" DataValueField="Name" DataSourceID="srcCulture" />
<asp:Button ID="btnSelect" Text="选择" runat="server" OnClick="btnSelect_Click" />
<hr />
今天的日期是:
<asp:Label ID="lblToday" runat="server" />
<hr />
商品的价格是:
<asp:Label ID="lblPrice" runat="server" />
<asp:ObjectDataSource ID="srcCulture" runat="server"
TypeName="System.Globalization.CultureInfo" SelectMethod="GetCultures">
<SelectParameters>
<asp:Parameter Name="types" DefaultValue="SpecificCultures" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
1.2自动检测文化
即利用浏览器请求一个页面时,它会发送一个Accept-Language首部,其包含了用户习惯的语言列表. 可用Request.UserLanguages属性返回Accept-Language首部的值.
实例显示Accept-Language:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Language="C#" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<script runat="server">
void Page_Load()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
bltAcceptLanguage.DataSource = Request.UserLanguages;
bltAcceptLanguage.DataBind();
}
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>显示Accept-Language</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:BulletedList ID="bltAcceptLanguage" runat="server" />
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
1.3在Web配置文件中设置文化
< system.web > < globalization culture="en-US" uiCulture="en-US" / > < / system.web >
1.4文化和ASP.NET控件
Culture属性的值会自动地影响诸如Calendar这样的ASP.NET控件的呈现行为。
实例:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Language="C#" Culture="zh-CN" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<script runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>文化和ASP.NET控件</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2.使用CultureInfo类
CultureInfo类包含了大约超过150个不同的文化的信息。可以在我们的代码中使用该类的方法返回特定的 区域。使用这些信息来格式化诸如日期、数字和货币数量的值。
2.1使用CultureInfo类格式化字符串值
即在ToString()方法中使用第二个参数。
实例:
3.创建本地资源
使用资源文件可根据用户的语言修改页面的文本或其他内容。每一个资源文件可以包含页面文本的特定语言的翻译版本.
ASP.NET Framework支持两种类型的资源文件:本地资源和全局资源.
ASP.NET Framewormk自动把本地资源编译成程序集,而全局资源就编译成一个类,这样全局资源就可以用强类型表达式在代码中处理全局资源!
当创建一个资源时,一个新类会自动添加到Resource命名空间.该类以属性的形式暴露了所有资源文件中的项目.
在开始本地化一个页面之前,一定要创建一个默认本地化文件.即如test.aspx.resx
3.1显式本地化表达式(即用显式的< %$ Resources:[**ResourceFileName,] **ResourceKeyName % >为控件属性设置一个特定的资源项目的值)
- 先在要本地化页面的文件路径同一级别上建立特殊文件夹App_LocalResources.
- 再在App_LocalResources文件夹内添加资源文件其命名如下:
pageName.[cultureName].resx
即例如:为页面test.aspx页面关联资源文件那么资源文件的命名将如下:
test.aspx.resx(默认,若没有其他资源文件符合用户的语言设置,则用此资源文件来应用)
test.aspx.zh.resx(中立文化,若没有符合用户特定文化语言设置,而有中立文化符合,则用此资源文件)
test.aspx.zh-CN.resx(特定文化,若符合用户特定文化语言设置,就应用此资源文件)
- 然后再完成页面部分.
- 在页面的< % Page % >指令中添加UICulture属性并设其值为auto.即< %@ Page Language="C#" UICulture="auto" % >
- 接着在相应的控件的属性上用上资源表达式< %$ Resources:[**ResourceFileName,] **ResourceKeyName % > (注:此资源表达式是一个显式的资源表达式,因属性被显式地设置为一个特定的资源项目的值)
MSDN版资源表达式说明:
该资源表达式采用以下形式,其中 Class 是可选的(除非资源是全局资源),而 ResourceID 是必需的:
< %$ Resources: Class , ResourceID % >
Class 值标识要在使用全局资源时使用的资源文件。在编译 .resx 文件时,将不带扩展名的基文件名显式用作所得程序集的类名。若要使用本地资源文件(与当前页名匹配的文件)中的资源,则不必提供类名,因为 ASP.NET 将该页类与资源类相匹配。
ResourceID 值是要读取的资源的标识符。在前面的示例中,从全局资源文件 WebResources.resx(或相应的本地化版本)读取按钮的 Text 属性。在该文件中,ASP.NET 将该值用于带有标识符 Button1Caption 的资源及页本身。若要设置页属性,可以在 @ Page 指令中使用资源表达式。
实例之正常无本地资源版(即页面内容不会随着用户的语言文化而改变):
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Language="C#" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<script runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected void btnSubmit_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
this.lblMsg.Visible = true;
}
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>简单页面</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Click Here!" /><br />
<br />
<asp:Label ID="lblMsg" runat="server" Text="Thank you!" Visible="False"></asp:Label>
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
实例之显式本地资源版(即页面内容会随着用户的语言文化而改变)
(相应的资源文件在此略了,也就先建立App_LocalResources文件夹,再在其中添加相应的资源文件及资源键/值等):
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Language="C#" UICulture="auto" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<script runat="server">
protected void btnSubmit_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
this.lblMsg.Visible = true;
}
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>本地化页面</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="<%$ Resources:ClickHere %>" /><br />
<br />
<asp:Label ID="lblMsg" runat="server" Text="<%$ Resources:ThankYou %>" Visible="False"/>
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3.2隐式本地化表达式
(即用一个资源键meta:resourceKey="ControlID"本地化控件的多个属性(这些属性在相应的资源文件中以Key(ControlID) . Property格式定义好了))
注:被隐式本地化的控件属性其被定义在资源文件中,则此控件属性在页面中可声明也可不用声明或只给出默认值
使用本地资源的隐式本地化
如果您已为特定页创建了本地资源文件,则可以使用隐式本地化从该资源文件中为控件填充属性值。使用隐式本地化时,ASP.NET 读取资源文件并将资源与属性值相匹配。
若要使用隐式本地化,必须对本地资源文件中的资源使用命名约定,命名约定采用以下模式:
Key . Property
例如,若要为名为 Button1 的 Button 控件创建资源,可以在本地资源文件中创建以下键/值对:
Button1.Text
Button1.BackColor
Label1.Text
可以对 Key 使用任意名称,但 Property 必须与要本地化的控件的属性相匹配。
在页上,对该控件的标记使用特殊的 meta 属性可指定隐式本地化。不必显式指定要本地化的属性。配置为使用隐式本地化的 Button 控件看起来可能类似于下面的形式:
resourcekey 值与相应资源文件中的键相匹配。在运行时,ASP.NET 通过将控件标签用作 resourcekey 将资源与控件属性相匹配。如果在资源文件中定义了某个属性值,则 ASP.NET 会用资源值替换该属性。
实例:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
LocalizablePageImplicit.aspx@ Page Language="C#" UICulture="auto" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<script runat="server">
protected void btnSubmit_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
this.lblMsg.Visible = true;
}
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>本地化页面</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" meta:resourceKey="btnSubmit" ToolTip="default value" /><br />
<br />
<asp:Label ID="lblMsg" runat="server" meta:resourceKey="lblMsg" Text="default value" Visible="False"/>
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
使用隐式的本地化表达式比显式资源表达式有两个好处.
- 隐式资源表达式可以使用一个资源键值重写控件的多个属性.即一个meta:resourceKey="Key"搞定,其他(键.属性)就交给相应的资源文件管理.
- 利用隐式本地化表达式,可以更简单地本地化一个已存在的网站,只需简单地添加一个meta:resourceKey属性到任意需要本地化的控件中.
3.3对页面属性使用本地化资源
可以使用资源表达设置诸如页面标题这样的的页面属性。
实例(显式本地化页面方式):
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
PageExplicit.aspx@ Page Language="C#" UICulture="auto" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<script runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title><asp:Literal ID="ltlTitle" Text="<%$ Resources:TitleText %>" runat="server" /></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>显式本地化页面</h1>
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
实例(隐式本地化页面方式):
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
PageImplicit.aspx @ Page Language="C#" UICulture="auto" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<script runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title><asp:Literal ID="ltlTitle" Text="SB SB SB" meta:resourceKey="ltlTitle" runat="server" /></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>隐式本地化页面</h1>
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3.3以编程方式获取本地化资源
在代码中访问本地资源,可以使用GetLocalResourceObject()由方法得到其值.
实例:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
ProgramLocalResource.aspx@ Page Language="C#" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<script runat="server">
void Page_Load()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string welcomeMsg = (string)GetLocalResourceObject("welcomeMsg");
lblMsg.Text = string.Format(welcomeMsg, "ithqm");
}
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>以编程方式获取本地资源</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblMsg" runat="server" />
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4.创建全局资源
本地资源用于特定的页面.而全局资源就用于应用程序的任意页面.任意需要被多个页面共享的本地化内容应该被放在一个全局资源文件中
可通过在根目录下添加特殊文件夹App_GlobalResources来实现全局资源文件夹,再创建相应的全局资源文件.
实例:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
ShowGlobalPage.aspx @ Page Language="C#" UICulture="auto" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<script runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title><asp:Literal ID="ltlTitle" Text="<%$ Resources:Site,Title %>" runat="server" /></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />页面内容
<br />页面内容
<br />页面内容
<br />页面内容<hr />
<asp:Literal ID="ltlCopyright" Text="<%$ Resources:Site,Copyright %>" runat="server" />
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4.1以编程方式获取全局资源
利用GetGlobalResourceObject()
实例:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
ProgramGlobalResource.aspx @ Page Language="C#" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<script runat="server">
void Page_Load()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string welcomeMsg = (string)GetGlobalResourceObject("Site","Title");
lblMsg.Text = welcomeMsg;
}
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>以编程方式获取全局资源</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblMsg" runat="server" />
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4.2使用强类型本地化表达式
ASP.NET Framewormk自动把本地资源编译成程序集,而全局资源就编译成一个类,这样全局资源就可以用强类型表达式在代码中处理全局资源!
当创建一个资源时,一个新类会自动添加到Resources命名空间.该类以属性的形式暴露了所有资源文件中的项目.
实例:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
ProgramGlobalTyped.aspx @ Page Language="C#" UICulture="auto" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<script runat="server">
void Page_Load()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//读取全局资源文件的属性.
lblMsg.Text = Resources.Site.Title;
}
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>强类型访问全局资源</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblMsg" runat="server" />
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5使用Localize控件
Localize控件与控件Literal控件非常相似(它继承自Literal控件),也就Localize控件可直接在源码视图状态下直接编写
实例:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
ShowLocalizeControl.aspx @ Page Language="C#" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<script runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>显示 Localize 控件</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Localize ID="locBodyText" meta:resourceKey="locBodyText" runat="server">
这里是些页面内容.
</asp:Localize>
<br /><br />
<asp:Literal ID="ltlBodyText" runat="server">这里是些literal文本!</asp:Literal>
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Don't dream it,Be it!