C# 全球资源下的本地化噩梦
我在C# 全球资源下的本地化噩梦,c#,asp.net,localization,app-globalresources,C#,Asp.net,Localization,App Globalresources,我在App\u GlobalResources MyApp.resx MyApp.sv.resx 对于那些不知道的人:除了瑞典UICulture将使用MyApp.sv.resx 我有一个简单的页面,显示了3个,其中Text属性的调用方式不同,如下所示: <i>using Resource.Write:</i><br /> <asp:Label ID="Label1" runat="server" /> <hr /&g
App\u GlobalResources
MyApp.resx
MyApp.sv.resx
对于那些不知道的人:除了瑞典UICulture将使用MyApp.sv.resx
我有一个简单的页面,显示了3个
,其中Text
属性的调用方式不同,如下所示:
<i>using Resource.Write:</i><br />
<asp:Label ID="Label1" runat="server" />
<hr />
<i>using HttpContext.GetGlobalResourceObject:</i><br />
<asp:Label ID="Label2" runat="server" />
<hr />
<i>using Text Resources:</i><br />
<asp:Label ID="Label3" runat="server"
Text="<%$ Resources:MyApp, btnRemoveMonitoring %>" />
<p style="margin-top:50px;">
<i>Current UI Culture:</i><br />
<asp:Literal ID="litCulture" runat="server" />
</p>
如果我使用浏览器语言一切正常,但我希望覆盖该设置并基于其他输入加载正确的翻译,因此我需要覆盖UICulture
,为此我使用:
protected void Page_Init(object sender, EventArgs e)
{
Page.Culture = "en-US";
Page.UICulture = "en-US";
}
女巫与:
protected void Page_Init(object sender, EventArgs e)
{
System.Globalization.CultureInfo cinfo = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
System.Threading.Thread.CurrentThread.CurrentCulture = cinfo;
System.Threading.Thread.CurrentThread.CurrentUICulture = cinfo;
}
有了这些,我得到的是:
换句话说只有当我使用代码隐藏
设置正确的文本时,我才能获得正确的本地化,所有内联
本地化只使用浏览器语言
我错过了什么?噩梦已经结束
Page_Init
不会更改对全局资源的访问,我们需要覆盖对文化的初始化
protected override void InitializeCulture()
{
//*** make sure to call base class implementation
base.InitializeCulture();
//*** pull language preference from profile
string LanguagePreference = "en-US"; // get from whatever property you want
//*** set the cultures
if (LanguagePreference != null)
{
this.UICulture = LanguagePreference;
this.Culture = LanguagePreference;
}
}
现在一切正常
如果不想更改每个页面,可以在Global.asax中设置区域性
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim lang As String = "en-us"
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang)
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang)
End Sub
我从单个类继承我的页面。。。补充到这里,但是谢谢你的提醒。
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim lang As String = "en-us"
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang)
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang)
End Sub