【问题标题】:Nightmare with Localization under Global Resources全球资源下的本地化噩梦
【发布时间】: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


    【解决方案1】:

    噩梦结束了……

    Page_Init不改变对全局资源的访问,我们需要override对文化进行初始化

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

    现在一切正常

    【讨论】:

      【解决方案2】:

      如果不想更改每个页面,可以在 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
      

      【讨论】:

      • 我从一个类继承了我的页面...添加到那里,但感谢您的提醒。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      • 2020-03-14
      相关资源
      最近更新 更多