全球资源下的本地化噩梦

发布于 2024-09-25 21:04:11 字数 2246 浏览 6 评论 0原文

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

有了这一切,我就是得到的是这样的:

alt text

换句话说 只有当我使用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:

alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木緿 2024-10-02 21:04:11

噩梦结束了...

Page_Init 不会更改对全局资源的访问,我们需要覆盖对文化的初始化

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

现在一切正常

替代文字

Nightmare is over ...

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

Now all works correctly

alt text

吾家有女初长成 2024-10-02 21:04:11

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

If don't want to change each page, you can set the culture in 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文