全球资源下的本地化噩梦
我在 App_GlobalResources
下有两个资源文件
MyApp.resx
MyApp.sv.resx
对于那些不知道的人: 所有语言都将回退到 MyApp.resx
例外 瑞典 UICulture 将使用 MyApp.sv.resx
,我有一个简单的页面,在 Text< 中显示 3
/code> 属性的调用方式不同,例如:
<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
是页面上唯一调用的属性,前 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
设置正确的文本,所有内联
本地化仅使用浏览器语言。
我错过了什么?
I have two Resources files under App_GlobalResources
MyApp.resx
MyApp.sv.resx
for those who don't know: All languages will fallback to MyApp.resx
except the Swedish UICulture will use the MyApp.sv.resx
and I have a simple page that shows 3 <asp:Label>
in witch the Text
property is called differently like:
<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
is the only one called on Page, the first 2 are set like:
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;
}
}
if I use the browser language all works fine, but I want to override that setting and load the correct translation based on other input, so I need to overwrite the UICulture
and for that I use:
protected void Page_Init(object sender, EventArgs e)
{
Page.Culture = "en-US";
Page.UICulture = "en-US";
}
witch is the same as:
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;
}
with all this, what I'm getting is this:
In other words I'm getting the correct localization only if I use code-behind
to set the correct text, all inline
localization simply uses the browser language.
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
噩梦结束了...
Page_Init
不会更改对全局资源的访问,我们需要覆盖
对文化的初始化现在一切正常
Nightmare is over ...
Page_Init
does not change the access to Global Resources, we need tooverride
the initialization to the culureNow all works correctly
如果不想更改每个页面,可以在 Global.asax 中设置文化
If don't want to change each page, you can set the culture in Global.asax