【发布时间】:2013-03-07 09:27:01
【问题描述】:
我有一个网站一直运行良好,直到我不得不删除一些不正确的资源文件。但是,这可能不是它不起作用的原因。基本上,与大多数多语言网站一样,用户可以通过单击标志来更改语言:
protected void imbEnglish_Click(object sender, ImageClickEventArgs e)
{
SetCultureStoreCookie("en-GB");
}
protected void imbFrench_Click(object sender, ImageClickEventArgs e)
{
SetCultureStoreCookie("fr-FR");
}
protected void imbGerman_Click(object sender, ImageClickEventArgs e)
{
SetCultureStoreCookie("de-DE");
}
protected void imbSpanish_Click(object sender, ImageClickEventArgs e)
{
SetCultureStoreCookie("es-ES");
}
protected void imbItalian_Click(object sender, ImageClickEventArgs e)
{
SetCultureStoreCookie("it-IT");
}
protected void imbPolish_Click(object sender, ImageClickEventArgs e)
{
SetCultureStoreCookie("fr-FR");
}
protected void SetCultureStoreCookie(string culture)
{
//Sets the cookie that is to be used by Global.asax
HttpCookie cookie = new HttpCookie("CultureInfo");
cookie.Value = culture;
Response.Cookies.Add(cookie);
//Set the culture and reload the page for immediate effect.
//Future effects are handled by Global.asax
Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
//Response.Redirect(Request.Path);
Server.Transfer(Request.Path);
}
正如它在 cmets 中所说,文化的后续设置正在 Global.asax 中处理:
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["CultureInfo"];
if (cookie != null && cookie.Value != null)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(cookie.Value);
Thread.CurrentThread.CurrentCulture = new CultureInfo(cookie.Value);
}
else
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB");
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
}
}
包括母版页在内的所有页面都有英文(默认)资源文件,我已经开始创建法文资源文件:
但是,当您单击法国国旗时,没有任何反应。我已经单步执行了代码,正在设置 cookie 并运行 Thread.CultureInfo 行。
关于在哪里检查的任何想法?
谢谢。
【问题讨论】:
-
您是否使用“支持的文化”编辑了 .csproj?右键单击您的项目,卸载项目,右键单击,编辑 .csproj,然后添加:
en,fr -
您好 Joffrey,这不是 Visual Studio 中的一个项目,它是一个简单的网站,但是正如我在介绍中所说的那样,翻译过去可以正常工作,而 .csproj 文件不存在。
-
好的,在我的项目(SL/WPF/Win8)中,我需要在 .csproj 中添加 SupportedCultures 才能实现全球化。
-
您好 Joffrey,我很欣赏您所说的,但它从来没有 .csproj 并且它曾经可以工作。也没有在网站中添加 .csproj 文件的选项。
标签: c# asp.net multilingual globalization global-asax