解决方案原理:
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事件代码
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!Page.IsPostBack){
4 if (Request.Cookies[Settings.Default.Cookies_Language] == null || Request.Cookies[Settings.Default.Cookies_Language].Value == "")
5 {
6 foreach (ListItem li in DropDownList1.Items)
7 {
8 if (li.Value == "")
9 li.Selected = true;
10 }
11 }
12 else
13 {
14 foreach (ListItem li in DropDownList1.Items)
15 {
16 if (li.Value.ToLower() == Request.Cookies[Settings.Default.Cookies_Language].Value.ToLower())
17 li.Selected = true;
18 }
19 }
20 }
21 }
22
23 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
24 {
25 Response.Redirect("~/ChangeLanguage.aspx?Language=" + DropDownList1.SelectedValue + "&url=" + Server.UrlEncode(Request.Url.PathAndQuery) ,true);
26 }ChangeLanguage中间页面代码:
27 public partial class ChangeLanguage : System.Web.UI.Page
28 {
29 protected void Page_Load(object sender, EventArgs e)
30 {
31 Response.Cookies["Language"].Value = Request["Language"];
32 Response.Cookies["Language"].Expires = DateTime.Now.AddDays(1000);
33
34 Response.Redirect(Server.UrlDecode(Request["url"]));
35 }
36 }
37
2 {
3 if (!Page.IsPostBack){
4 if (Request.Cookies[Settings.Default.Cookies_Language] == null || Request.Cookies[Settings.Default.Cookies_Language].Value == "")
5 {
6 foreach (ListItem li in DropDownList1.Items)
7 {
8 if (li.Value == "")
9 li.Selected = true;
10 }
11 }
12 else
13 {
14 foreach (ListItem li in DropDownList1.Items)
15 {
16 if (li.Value.ToLower() == Request.Cookies[Settings.Default.Cookies_Language].Value.ToLower())
17 li.Selected = true;
18 }
19 }
20 }
21 }
22
23 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
24 {
25 Response.Redirect("~/ChangeLanguage.aspx?Language=" + DropDownList1.SelectedValue + "&url=" + Server.UrlEncode(Request.Url.PathAndQuery) ,true);
26 }ChangeLanguage中间页面代码:
27 public partial class ChangeLanguage : System.Web.UI.Page
28 {
29 protected void Page_Load(object sender, EventArgs e)
30 {
31 Response.Cookies["Language"].Value = Request["Language"];
32 Response.Cookies["Language"].Expires = DateTime.Now.AddDays(1000);
33
34 Response.Redirect(Server.UrlDecode(Request["url"]));
35 }
36 }
37
2:在每页加载时会判断当前用户选择的区域,一般在页面的基类中实现。
当用户初次进入网站识别客户端浏览器,
当用户曾经选择了区域,加载选择区域
1 /**//// <summary>
2 /// 本地化
3 /// </summary>
4 protected override void InitializeCulture()
5 {
6 if (Request.Cookies[Settings.Default.Cookies_Language] != null && Request.Cookies[Settings.Default.Cookies_Language].Value != null && Request.Cookies[Settings.Default.Cookies_Language].Value != "")
7 {
8 setCulture();
9 }
10 else
11 {//设置Cookie
12 Response.Cookies[Settings.Default.Cookies_Language].Value =
13 (Request.Headers["accept-language"].Split(",".ToCharArray())[0]);
14 Response.Cookies[Settings.Default.Cookies_Language].Expires = DateTime.Now.AddDays(1000);
15 setCulture();
16 }
17 base.InitializeCulture();
18 }
19
20
21 private void setCulture()
22 {
23
24 try
25 {
26 String selectedLanguage = Request.Cookies[Settings.Default.Cookies_Language].Value;
27 UICulture = selectedLanguage;
28 Culture = selectedLanguage;
29
30 System.Threading.Thread.CurrentThread.CurrentCulture =
31 System.Globalization.CultureInfo.CreateSpecificCulture(selectedLanguage);
32 System.Threading.Thread.CurrentThread.CurrentUICulture = new
33 System.Globalization.CultureInfo(selectedLanguage);
34 }
35 catch //( Exception ex )
36 {
37 Response.Cookies.Clear();// [Settings.Default.Cookies_Language].d
38 //throw;
39 }
40 }
41
2 /// 本地化
3 /// </summary>
4 protected override void InitializeCulture()
5 {
6 if (Request.Cookies[Settings.Default.Cookies_Language] != null && Request.Cookies[Settings.Default.Cookies_Language].Value != null && Request.Cookies[Settings.Default.Cookies_Language].Value != "")
7 {
8 setCulture();
9 }
10 else
11 {//设置Cookie
12 Response.Cookies[Settings.Default.Cookies_Language].Value =
13 (Request.Headers["accept-language"].Split(",".ToCharArray())[0]);
14 Response.Cookies[Settings.Default.Cookies_Language].Expires = DateTime.Now.AddDays(1000);
15 setCulture();
16 }
17 base.InitializeCulture();
18 }
19
20
21 private void setCulture()
22 {
23
24 try
25 {
26 String selectedLanguage = Request.Cookies[Settings.Default.Cookies_Language].Value;
27 UICulture = selectedLanguage;
28 Culture = selectedLanguage;
29
30 System.Threading.Thread.CurrentThread.CurrentCulture =
31 System.Globalization.CultureInfo.CreateSpecificCulture(selectedLanguage);
32 System.Threading.Thread.CurrentThread.CurrentUICulture = new
33 System.Globalization.CultureInfo(selectedLanguage);
34 }
35 catch //( Exception ex )
36 {
37 Response.Cookies.Clear();// [Settings.Default.Cookies_Language].d
38 //throw;
39 }
40 }
41
3:当然是为每个页面准备资源文件。
这个是个体力活,
ASPNET.ascx.en.resx 表示英文的
ASPNET.ascx.zh.resx 这个表示中文的,当然还可以分为更加细的区域,比如zh-CN/zh-HK/zh-TW
参考地址:http://hi.baidu.com/qing%5Fspace/blog/item/9edd4344eea21087b2b7dcf8.html