MVC应用程序中访问本地化字符串的问题

MVC应用程序中访问本地化字符串的问题

本文介绍了在ASP.NET MVC应用程序中访问本地化字符串的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created an ASP.NET MVC application and added 2 resource file for about.aspx page in the project. It looks like this:

然后,我对About.aspx页面进行了如下修改:

Then I modified the About.aspx page as following:

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

将firefox区域设置更改为后,我试图运行关于"页面hi-IN,但仍显示默认文本(英语).请问您发现问题了吗?

I tried to run the about page after changing the firefox locale to hi-IN but it still shows the default text (in English). Please can you spot the problem?

推荐答案

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

The CurrentCulture and CurrentUICulture does not automatically change according to what the browser reports. You will need to specify that:

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

您应该注意,当您尝试将某些语言分配给 Thread.CurrentCulture ,因为它不允许所谓的中性"文化.简而言之,中立文化是仅识别一种语言而不识别地理区域的文化.您可以在 CultureInfo文档类.

You should note that some of the languages that you can select ("en" for instance), will cause an exception when trying to assign it to Thread.CurrentCulture, since it does not allow what is called "neutral" cultures. In short, a neutral culture is one that identifies only a language, but not geographical region. You can read more about that in the documentation for the CultureInfo class.

这篇关于在ASP.NET MVC应用程序中访问本地化字符串的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:15