我有一个目标语言存储在Session["lang"]中,可以是"en"或者"it"。我已经将其添加到Site.master中:
<script runat="server">
void Page_Load(object sender, EventArgs e) {
string lang = Session["lang"].ToString();
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(lang);
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.CreateSpecificCulture(lang);
}
</script>
接着,我想像这样调用资源字符串:
<asp:Label ID="Label1" runat="server" Text="<%$ Resources:Global, test %>"></asp:Label>
在App_GlobalResources文件夹中,我有两个文件,分别命名为Global.resx和Global.en.resx。
问题在于,无论lang变量中输入什么,我总是从主Global.resx获取结果,而且从Global.en.resx中永远无法获取英文版本。
我完全错了吗?
我尝试将System.Threading...部分放在Global.asax.cs中的Application_PreRequestHandlerExecute方法中,但结果相同。
谢谢
PS:我正在寻求一种简单的方法使其工作。如果我要使用复杂的方法,我会选择这个:http://helios.ca/2009/05/27/aspnet-mvc-and-localization/
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
- Kostas Konstantinidis