【发布时间】:2010-09-28 12:44:53
【问题描述】:
我在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 上唯一调用的,前 2 个设置如下:
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 本地化只使用浏览器语言。
我错过了什么?
【问题讨论】:
标签: c# asp.net localization app-globalresources