我在App_GlobalResources
MyApp.resx
MyApp.sv.resx
对于那些不知道的人:所有语言都将回退到MyApp.resx
,但瑞典UICulture将使用MyApp.sv.resx
我有一个简单的页面显示3 <asp:Label>
,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>
Label3
是在Page上调用的唯一一个,前两个设置为:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Label1.Text = Resources.AdwizaPAR.btnRemoveMonitoring;
Label2.Text = HttpContext.GetGlobalResourceObject("MyApp", "btnRemoveMonitoring").ToString();
litCulture.Text = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
}
}
如果我使用浏览器语言一切正常,但我想覆盖该设置并根据其他输入加载正确的翻译,因此我需要覆盖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;
}
所有这些,我得到的是:
换句话说我只有在使用code-behind
设置正确的文字时才能获得正确的本地化,所有inline
本地化只使用浏览器语言。
我缺少什么?
答案 0 :(得分:3)
梦魇结束了......
Page_Init
不会更改对全局资源的访问权限,我们需要override
初始化到culure
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;
}
}
现在一切正常
答案 1 :(得分:0)
如果您不想更改每个页面,可以在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