首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

关于ASP.NET国际化的有关问题,如何让用户选择语言类型

2012-02-02 
关于ASP.NET国际化的问题,怎么让用户选择语言类型请问一下,我给我的asp.net程序添加了两个资源文件,分别显

关于ASP.NET国际化的问题,怎么让用户选择语言类型
请问一下,
我给我的asp.net程序添加了两个资源文件,分别显示对应中文和英文版,
如果我给程序配置文件中设置默认显示中文或者英文,都能正常显示,
现在我在页面上加了一个dropdownList,想让用户从dropdownList下拉显示中文或者英文,
选择中文就匹配程序的默认资源文件,选择英文就匹配程序的默认英文资源,
这个应该怎么实现呢?

我的代码中,
重写了InitializeCulture()方法

protected override void InitializeCulture()
{
  //显式的指定区域为en
  //实际应用中可以根据用户选择来动态设置语言
  Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
  Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
}

以为这样的话只要将改方法调用就可以了,可是不行!
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
  if (this.DropDownList1.SelectedValue == "英语")
  {
  InitializeCulture();
  }
}

这样的话,因为我重写的InitializeCulture()方法是设置文化为英语,这个页面加载的时候,就默认显示英语了!
如果我不重载InitializeCulture()方法,
直接写
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
  if (this.DropDownList1.SelectedValue == "英语")
  {
  Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
  Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");  
  }
}
这样的话,页面加载的时候,是显示默认文化中文,然后我在dropdownList中选择“英语”以后,页面还是没有调用英语的资源文件,还是显示中文!!

请问高手们,这个根据用户的选择语言种类显示多语言版本的asp.net程序,应该怎么做呢????

PS:我的dropdownList的AutoPostback为true


[解决办法]
UP

[解决办法]
为了帮你顶~~

我重新进来的~~
[解决办法]
- -帮你顶了 给我50分
[解决办法]
对于用户请求,通常会有若干个线程进行处理的。

设置Thread.UICulture试试看
[解决办法]
另外,ref:
http://www.cnblogs.com/cleo/archive/2006/09/09/aspnet_internationalization.html
[解决办法]
支持下
[解决办法]
顶,JF!
[解决办法]

[解决办法]
很多书上都有介绍多语言和本地化的东东
自己做的时候感觉最麻烦的是css的调整,毕竟中文和英文长度不一样,定义不同的样式再改起来也麻烦
[解决办法]
关注,up
[解决办法]
我刚刚做了个中英文的 
我用了一个静态的变量做判断
给你看看代码
不懂的话Q我 283162221

-----------------
private static string m_lang = "en-US";//初始化语言(ajax)


protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(Forum_ForumBack_Main));




if (Request["ltp"] != null && Request["ltp"] != "")
{
loadType = int.Parse(Request["ltp"].ToString());
}

if (!IsPostBack)
{
InitializeCulture();

PageInit();
DistillateBind();
}
}
----------------------------------
//设置语言
protected override void InitializeCulture()//
{
if (HttpContext.Current.Request.Cookies["Language"] != null)
{
string language = Request.Cookies["Language"]["Language"].ToString(); 
if (!String.IsNullOrEmpty(language))
{
//UICulture - 决定了采用哪一种本地化资源,也就是使用哪种语言
//Culture - 决定各种数据类型是如何组织,如数字与日期
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(language);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
//设置页面语言显示
}
}
else
{
HttpCookie csEnt = new HttpCookie("Language");
csEnt.Values.Add("Language", "en-US");
System.Web.HttpContext.Current.Response.AppendCookie(csEnt);
}

}
//中文
protected void btnChina_Click(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Cookies["Language"] != null)
{
HttpContext.Current.Request.Cookies.Remove("Language");
HttpCookie csEnt = new HttpCookie("Language");
csEnt.Values.Add("Language", "zh-CN");
System.Web.HttpContext.Current.Response.AppendCookie(csEnt);
//修改Cookie的两种方法
//cok.Values["userid"]="alter-value";
//cok.Values.Set("userid","alter-value"); 
}
else
{
HttpCookie csEnt = new HttpCookie("Language");
csEnt.Values.Add("Language", "zh-CN");
System.Web.HttpContext.Current.Response.AppendCookie(csEnt);
}
InitializeCulture();

hlnkLogin.Text = (String)GetGlobalResourceObject("LocalizedText", "Login");
hlnkAdmin.Text = (String)GetGlobalResourceObject("LocalizedText", "Admin");
lbtnBack.Text = (String)GetGlobalResourceObject("LocalizedText", "mExit");
hlnkNote.Text = (String)GetGlobalResourceObject("LocalizedText", "uNote");
hlnkCorp.Text = (String)GetGlobalResourceObject("LocalizedText", "aModManage");
lblHello.Text = (String)GetGlobalResourceObject("LocalizedText", "mHello");

lbnPrevPage.Text = (String)GetGlobalResourceObject("LocalizedText", "sPreviousPage");
Localize3.Text = (String)GetGlobalResourceObject("LocalizedText", "sNo");
Localize1.Text = (String)GetGlobalResourceObject("LocalizedText", "sPage");
lbnNextPage.Text = (String)GetGlobalResourceObject("LocalizedText", "sDropPage");
Localize5.Text = (String)GetGlobalResourceObject("LocalizedText", "sAll");
Localize2.Text = (String)GetGlobalResourceObject("LocalizedText", "sPage");
Localize4.Text = (String)GetGlobalResourceObject("LocalizedText", "sRecord");

m_lang = "zh-CN";
lanageStates = 1;
DistillateBind();
}
// 英文
protected void btnEnglish_Click(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Cookies["Language"] != null)
{
HttpContext.Current.Request.Cookies.Remove("Language");
HttpCookie csEnt = new HttpCookie("Language");
csEnt.Values.Add("Language", "en-US");
System.Web.HttpContext.Current.Response.AppendCookie(csEnt);
}
else
{
HttpCookie csEnt = new HttpCookie("Language");
csEnt.Values.Add("Language", "en-US");
System.Web.HttpContext.Current.Response.AppendCookie(csEnt);
}

InitializeCulture();


hlnkLogin.Text = (String)GetGlobalResourceObject("LocalizedText", "Login");
hlnkAdmin.Text = (String)GetGlobalResourceObject("LocalizedText", "Admin");
lbtnBack.Text = (String)GetGlobalResourceObject("LocalizedText", "mExit");
hlnkNote.Text = (String)GetGlobalResourceObject("LocalizedText", "uNote");
hlnkCorp.Text = (String)GetGlobalResourceObject("LocalizedText", "aModManage");
lblHello.Text = (String)GetGlobalResourceObject("LocalizedText", "mHello");

lbnPrevPage.Text = (String)GetGlobalResourceObject("LocalizedText", "sPreviousPage");
Localize3.Text = (String)GetGlobalResourceObject("LocalizedText", "sNo");
Localize1.Text = (String)GetGlobalResourceObject("LocalizedText", "sPage");
lbnNextPage.Text = (String)GetGlobalResourceObject("LocalizedText", "sDropPage");
Localize5.Text = (String)GetGlobalResourceObject("LocalizedText", "sAll");
Localize2.Text = (String)GetGlobalResourceObject("LocalizedText", "sPage");
Localize4.Text = (String)GetGlobalResourceObject("LocalizedText", "sRecord");

m_lang = "en-US";
lanageStates = 0;
DistillateBind();
}
[解决办法]
在页面线程是无状态的,改了当然没用
你可以设置一个Session,用来存储用户选择的语言
如果是应用程序的话,也可以将选择保存到数据库中或文件中,这样所有的用户都使用同一种语言
[解决办法]
弄清楚什么是多國語言就好做了:)
[解决办法]
Settings是WebApplication和WinForm中出现的
[解决办法]
12楼的可行

热点排行