Problem in accessing localized string in ASP.NET MVC application

廉价感情. 提交于 2019-12-19 10:36:09

问题


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

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>

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?


回答1:


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

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.



来源:https://stackoverflow.com/questions/1147028/problem-in-accessing-localized-string-in-asp-net-mvc-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!