【问题标题】:Problem in accessing localized string in ASP.NET MVC application在 ASP.NET MVC 应用程序中访问本地化字符串时出现问题
【发布时间】:2009-07-18 08:37:45
【问题描述】:

我创建了一个 ASP.NET MVC 应用程序,并在项目中为 about.aspx 页面添加了 2 个资源文件。它看起来像这样:

然后我将 About.aspx 页面修改如下:

<asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%= GetLocalResourceObject ("About")%></h2>
    <p>
        <%= GetLocalResourceObject ("PutContentHere")%>
    </p>
</asp:Content>

我尝试在 changing the firefox locale 之后运行 about 页面到 hi-IN,但它仍然显示默认文本(英文)。请问你能发现问题吗?

【问题讨论】:

    标签: asp.net-mvc localization multilingual


    【解决方案1】:

    CurrentCulture 和 CurrentUICulture 不会根据浏览器报告的内容自动更改。您需要指定:

    protected override void OnInit(EventArgs e)
    {
        try
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = 
                              CultureInfo.GetCultureInfo(Request.UserLanguages[0]);
            System.Threading.Thread.CurrentThread.CurrentCulture = 
                              System.Threading.Thread.CurrentThread.CurrentUICulture;
        }
        catch (Exception ex)
        {
            // handle the exception
        }
        base.OnInit(e);
    }
    

    您应该注意,您可以选择的某些语言(例如“en”)在尝试将其分配给 Thread.CurrentCulture 时会导致异常,因为它不允许所谓的“中性”文化。简而言之,中性文化是一种只识别一种语言而不识别地理区域的文化。您可以在the documentation for the CultureInfo class 阅读更多相关信息。

    【讨论】:

    • 我的母版页中的资源字符串也很少。我必须在所有页面的资源文件中定义所有这些字符串吗?
    • @Hermant:您以与 aspx 页面相同的方式为母版页添加资源。与其调用资源文件 pagefilename.aspx.resx,不如调用它 masterfilename.master.resx。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    相关资源
    最近更新 更多