ASP.Net本地化/国际化解决方案原理和代码示例
解决方案原理:
ASP.NET本地化支持
通过 CultureInfo 类可以访问区域设置的属性。
此外,ASP.NET 基于每个线程和请求跟踪默认区域性的两个属性:用于区域设置相关函数默认值的 CurrentCulture,
和用于区域设置特定的资源数据查找的 CurrentUICulture。
解决方案主要包含几个部分组成:
1:客户设置语言的控件,一般会包含在每个页面,使得用户可以随时切换区域
控件ascx代码
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="English Version" Value="en-US"></asp:ListItem>
<asp:ListItem Text="中 文 版" Value="zh-CN"></asp:ListItem>
</asp:DropDownList>
<asp:ListItem Text="English Version" Value="en-US"></asp:ListItem>
<asp:ListItem Text="中 文 版" Value="zh-CN"></asp:ListItem>
</asp:DropDownList>
控件CS事件代码
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack){
if (Request.Cookies[Settings.Default.Cookies_Language] == null || Request.Cookies[Settings.Default.Cookies_Language].Value == "")
{
foreach (ListItem li in DropDownList1.Items)
{
if (li.Value == "")
li.Selected = true;
}
}
else
{
foreach (ListItem li in DropDownList1.Items)
{
if (li.Value.ToLower() == Request.Cookies[Settings.Default.Cookies_Language].Value.ToLower())
li.Selected = true;
}
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("~/ChangeLanguage.aspx?Language=" + DropDownList1.SelectedValue + "&url=" + Server.UrlEncode(Request.Url.PathAndQuery) ,true);
}
{
if (!Page.IsPostBack){
if (Request.Cookies[Settings.Default.Cookies_Language] == null || Request.Cookies[Settings.Default.Cookies_Language].Value == "")
{
foreach (ListItem li in DropDownList1.Items)
{
if (li.Value == "")
li.Selected = true;
}
}
else
{
foreach (ListItem li in DropDownList1.Items)
{
if (li.Value.ToLower() == Request.Cookies[Settings.Default.Cookies_Language].Value.ToLower())
li.Selected = true;
}
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("~/ChangeLanguage.aspx?Language=" + DropDownList1.SelectedValue + "&url=" + Server.UrlEncode(Request.Url.PathAndQuery) ,true);
}
ChangeLanguage中间页面代码:
public partial class ChangeLanguage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Cookies["Language"].Value = Request["Language"];
Response.Cookies["Language"].Expires = DateTime.Now.AddDays(1000);
Response.Redirect(Server.UrlDecode(Request["url"]));
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Cookies["Language"].Value = Request["Language"];
Response.Cookies["Language"].Expires = DateTime.Now.AddDays(1000);
Response.Redirect(Server.UrlDecode(Request["url"]));
}
}
2:在每页加载时会判断当前用户选择的区域,一般在页面的基类中实现比如类名:BasePage。
当用户初次进入网站识别客户端浏览器,
当用户曾经选择了区域,加载选择区域
/// <summary>
/// 本地化
/// </summary>
protected override void InitializeCulture()
{
if (Request.Cookies[Settings.Default.Cookies_Language] != null && Request.Cookies[Settings.Default.Cookies_Language].Value != null && Request.Cookies[Settings.Default.Cookies_Language].Value != "")
{
setCulture();
}
else
{//设置Cookie
Response.Cookies[Settings.Default.Cookies_Language].Value =
(Request.Headers["accept-language"].Split(",".ToCharArray())[0]);
Response.Cookies[Settings.Default.Cookies_Language].Expires = DateTime.Now.AddDays(1000);
setCulture();
}
base.InitializeCulture();
}
private void setCulture()
{
try
{
String selectedLanguage = Request.Cookies[Settings.Default.Cookies_Language].Value;
UICulture = selectedLanguage;
Culture = selectedLanguage;
System.Threading.Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.CreateSpecificCulture(selectedLanguage);
System.Threading.Thread.CurrentThread.CurrentUICulture = new
System.Globalization.CultureInfo(selectedLanguage);
}
catch //( Exception ex )
{
Response.Cookies.Clear();// [Settings.Default.Cookies_Language].d
//throw;
}
}
/// 本地化
/// </summary>
protected override void InitializeCulture()
{
if (Request.Cookies[Settings.Default.Cookies_Language] != null && Request.Cookies[Settings.Default.Cookies_Language].Value != null && Request.Cookies[Settings.Default.Cookies_Language].Value != "")
{
setCulture();
}
else
{//设置Cookie
Response.Cookies[Settings.Default.Cookies_Language].Value =
(Request.Headers["accept-language"].Split(",".ToCharArray())[0]);
Response.Cookies[Settings.Default.Cookies_Language].Expires = DateTime.Now.AddDays(1000);
setCulture();
}
base.InitializeCulture();
}
private void setCulture()
{
try
{
String selectedLanguage = Request.Cookies[Settings.Default.Cookies_Language].Value;
UICulture = selectedLanguage;
Culture = selectedLanguage;
System.Threading.Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.CreateSpecificCulture(selectedLanguage);
System.Threading.Thread.CurrentThread.CurrentUICulture = new
System.Globalization.CultureInfo(selectedLanguage);
}
catch //( Exception ex )
{
Response.Cookies.Clear();// [Settings.Default.Cookies_Language].d
//throw;
}
}
3:当然是为每个页面准备资源文件。
这个是个体力活,
ASPNET.ascx.en.resx 表示英文的
ASPNET.ascx.zh.resx 这个表示中文的,当然还可以分为更加细的区域,比如zh-CN/zh-HK/zh-TW
4:当然要记得,把你的页面都要继承你的基类
public partial class Index : PageBase