现在的程序越来越要考虑国际化了,对于网站的开发也经常要考虑国际化的问题。
常见的国际化的需求主要就是两个方面:
1、不同的国家页面布局不同
2、不同的国家要显示当国的文字
在ASP.NET2.0中实现这两种需求的方法很多,我今天就总结其中一种方法的具体实现。
对应需求1我们可以使用母版页来实现,对于需求2可以使用资源文件的方式来实现。
在母版页中定义好页面布局和各种语言的转换链接,每种语言定义一个母版页,比如中文就叫Chinese.master,英文就叫English.master
Chinese.master 示例
其他语言的母版页也是类似
然后可以在每个aspx的Page_PreInit事件中调用共通的方法来进行语言的动态转换
protected void Page_PreInit(object sender, EventArgs e)
{
Util.ChangeLanguage(Request, this);
}
public static void ChangeLanguage(HttpRequest Request, Page page)
{
string strLanguage = Request.QueryString.Get("language");
if (strLanguage != null)
{
page.Session[Util.SESSION_LANGUAGE] = strLanguage;
}
strLanguage = page.Session[Util.SESSION_LANGUAGE] as string;
string strMasterPage = "~/Front/MasterPage/" + strLanguage + ".master";
page.MasterPageFile = strMasterPage;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string strUICulture = "";
switch (strLanguage)
{
case "English":
strUICulture = "en-us";
break;
case "Japanese":
strUICulture = "ja";
break;
}
page.UICulture = strUICulture;
}
主要就是在ChangeLanguage方法中首先改变页面的母版页,然后再改变页面的区域
然后可以在Global.asax全局的Session_Start事件中设置初始语言:
void Session_Start(object sender, EventArgs e)
{
Session[Util.SESSION_LANGUAGE] = "Chinese";
}
我这个例子中是直接设置初始为中文语言,当然也可以根据客户端的环境来动态设置的:
void Session_Start(object sender, EventArgs e)
{
switch (Session.LCID)
{
case 0x0009:
Session[Util.SESSION_LANGUAGE] = "English";
break;
case 0x0011:
Session[Util.SESSION_LANGUAGE] = "Japanese";
break;
default:
Session[Util.SESSION_LANGUAGE] = "Chinese";
break;
}
//Session[Util.SESSION_LANGUAGE] = "Chinese";
}
通过以上步骤我们已经可以动态改变页面布局(母版页)和页面的区域了。
下面新建三个全局的资源文件:Resource.resx,Resource.en-us.resx,Resource.ja.resx
在三个资源文件中都加入TestString这个键值,value值写上不同的语言
然后我们就可以在自己的Page页面中使用这个资源文件了:
<%@ Page Language="C#" MasterPageFile="~/Front/MasterPage/Chinese.master" AutoEventWireup="true"
CodeFile="index.aspx.cs" Inherits="index" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Top" Runat="Server">
<asp:Label ID="Label1" runat="server" Text="<%$ Resources:Resource,TestString %>" Width="76px">
</asp:Label><br />
<br />
</asp:Content>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
也就是说以后出现文字或和区域有关的图片的时候都要在不同的资源文件中定义好,然后在页面中使用<%$ Resources:Resource,TestString %>的方式取值
常见的国际化的需求主要就是两个方面:
1、不同的国家页面布局不同
2、不同的国家要显示当国的文字
在ASP.NET2.0中实现这两种需求的方法很多,我今天就总结其中一种方法的具体实现。
对应需求1我们可以使用母版页来实现,对于需求2可以使用资源文件的方式来实现。
在母版页中定义好页面布局和各种语言的转换链接,每种语言定义一个母版页,比如中文就叫Chinese.master,英文就叫English.master
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
然后可以在每个aspx的Page_PreInit事件中调用共通的方法来进行语言的动态转换
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
然后可以在Global.asax全局的Session_Start事件中设置初始语言:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
通过以上步骤我们已经可以动态改变页面布局(母版页)和页面的区域了。
下面新建三个全局的资源文件:Resource.resx,Resource.en-us.resx,Resource.ja.resx
在三个资源文件中都加入TestString这个键值,value值写上不同的语言
然后我们就可以在自己的Page页面中使用这个资源文件了:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
CodeFile="index.aspx.cs" Inherits="index" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</asp:Label><br />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)