我有一个使用框架4的网站。我用全球资源改变了语言。在我使用这些代码后面的按钮点击代码。
protected void Button2_Click(object sender, EventArgs e)
{
dil = "en-US";
var ci = new CultureInfo(dil); //TO_DO Route culture
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = ci;
Session["culture"] = ci;
}
也是我的resx文件:
-PB.resx
-PB.en-US.resx
-PB.ru-RU.resx
默认语言工作正常,但我怎样才能改为英语和俄语?我的错在哪里?
答案 0 :(得分:0)
首先,您应该将语言数据存储在cookie中。要设置页面语言,请覆盖InitializeCulture方法。
protected override void InitializeCulture()
{
var currentLanguage= HttpContext.Current.Request.Cookies["dil"];
string defaultLanguage="tr";
if(currentLanguage==null)
{
//set cookie to defaultLanguage
}
else{
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(currentLanguage.Value);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
}
}
点击按钮
更改语言protected void Button2_Click(object sender, EventArgs e)
{
dil = "en-US";
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(dil);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
HttpCookie hc = new HttpCookie("dil");
hc.Expires=DateTime.Now.AddDays(30);
hc.Value=dil;
HttpContext.Current.Response.Cookies.Add(hc);
}
答案 1 :(得分:0)
经过长时间的搜索,我解决了这个问题。这是您需要的答案和所有代码。我为visual studio 2010中的母版页制作了这个。
您可以在页面加载中使用ispostback。
protected void Page_Load(object sender, EventArgs e)
{
//only does it on non-postback because otherwise the selected
//value will not reach event handler correctly
if (!Page.IsPostBack)
{
dil = Thread.CurrentThread.CurrentCulture.Name;
}
}
之后我们可以添加按钮点击和cookie
protected void Button2_Click(object sender, EventArgs e)
{
dil = "en-US";
//var ci = new CultureInfo(dil); //TO_DO Route culture
//Thread.CurrentThread.CurrentUICulture = ci;
//Thread.CurrentThread.CurrentCulture = ci;
//Session["culture"] = ci;
//Sets the cookie that is to be used by Global.asax
HttpCookie cookie = new HttpCookie("CultureInfo");
cookie.Value = dil;
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(dil);
Thread.CurrentThread.CurrentUICulture =
new CultureInfo(dil);
Server.Transfer(Request.Path);
}
和最后一个global.asax文件有助于解决这个问题。
//*
Public void Application_BeginRequest(Object sender, EventArgs e)
{
// Code that runs on application startup
HttpCookie cookie = HttpContext.Current.Request.Cookies["CultureInfo"];
if (cookie != null && cookie.Value != null)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new
System.Globalization.CultureInfo(cookie.Value);
System.Threading.Thread.CurrentThread.CurrentCulture = new
System.Globalization.CultureInfo(cookie.Value);
}
else
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new
System.Globalization.CultureInfo("tr-TR");
System.Threading.Thread.CurrentThread.CurrentCulture = new
System.Globalization.CultureInfo("tr-TR");
}
}
//*
如果您使用的是html标签而不是.net标签,则可以使用这些标签添加文本控件。
<a><asp:Literal ID="Literal1" runat="server" Text="<%$Resources: PB, Home %>" /></a>