ASP.NET:编程更改网站的文化 [英] ASP.NET: Changing a site's culture programmatically

查看:103
本文介绍了ASP.NET:编程更改网站的文化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以编程方式设置我的网站的文化,所以当用户点击一个按钮就可以在网页上的文字改变,从英语翻译成西班牙语。这里是我的code:

i'm trying to set my website's culture programmatically, so when a user clicks a button they can change the text on the page from english to spanish. here's my code:

protected void btnChangeLanguage(object sender, EventArgs e)
{
    Thread.CurrentThread.CurrentCulture = new CultureInfo("es");
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("es);
}

<asp:Label ID="lblDisplay" runat="server" meta:ResourceKey="lblDisplay" />

<asp:Button ID="btnChangeLanguage" runat="server" Text="Change Language"
        OnClick="btnChangeLanguage_Click" />

我有一个键/值为Default.aspx.resx文件:lblDisplay.text /英文
和Default.aspx.es.resx用密钥文件/值:lblDisplay.text /西班牙语

i have a Default.aspx.resx file with a key/value of: lblDisplay.text/English and a Default.aspx.es.resx file with a key/value of: lblDisplay.text/Espanol

我不能让我的标签的文本,从英语到西班牙的转变。任何人看到我在做什么错了?

i can't get my Label's text to change from "English" to "Spanish". anyone see what i'm doing wrong?

推荐答案

ASP.Net线程用于一个请求,而不是用户的整个会话的生存期。更糟的是,有时框架将回收同一个线程来处理其他请求,而不是将其返回到池中,并得到一个新的(这不是什么大不了的事,因为下一个请求将重新初始化的文化,但仍然)。

ASP.Net threads are used for the lifetime of one request, not a user's entire session. Worse, sometimes the framework will recycle the same thread to handle additional requests rather than return it to the pool and get a new one (it's not that big a deal because the next request will initialize the culture again, but still).

相反,你需要重写InitializeCulture()方法,为您的网页。请参阅此链接更多详细信息:结果
<一href=\"http://msdn.microsoft.com/en-us/library/bz9tc508.aspx\">http://msdn.microsoft.com/en-us/library/bz9tc508.aspx

Instead, you need to override the InitializeCulture() method for your page. See this link for more detail:
http://msdn.microsoft.com/en-us/library/bz9tc508.aspx

这篇关于ASP.NET:编程更改网站的文化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆