噩梦在全球资源本地化 [英] Nightmare with Localization under Global Resources

查看:209
本文介绍了噩梦在全球资源本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个资源文件,在 App_GlobalResources文件

I have two Resources files under App_GlobalResources

MyApp.resx
MyApp.sv.resx

对于那些谁不知道:的所有语言将回退到 MyApp.resx 除了瑞典的UICulture将使用 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

和我有一个简单的页面,显示3 < ASP:标签> 在女巫的文本属性所谓的不同,如:

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 是唯一一个呼吁页,第2的设置类似于:

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;
    }
}

如果我使用浏览器语言所有工作正常,但我希望基于其他输入来覆盖该设置并加载正确的翻译,所以我需要覆盖的UICulture 和我使用:

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";
}

女巫是一样的:

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:

换句话说我只得到,如果我使用了正确的本地化 code-背后来设置正确的文本,所有在线本地化只需使用浏览器语言。

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.

我缺少什么?

推荐答案

噩梦结束了...

Page_Init 不改变访问全球资源,我们需要覆盖初始化到culure

Page_Init does not change the access to Global Resources, we need to override the initialization to the 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;
    }
}

现在所有的正常工作

这篇关于噩梦在全球资源本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆