Hemant
Hemant

Reputation: 19826

Problem in accessing localized string in ASP.NET MVC application

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

alt folder structure

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?

Upvotes: 2

Views: 806

Answers (1)

Fredrik M&#246;rk
Fredrik M&#246;rk

Reputation: 158309

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.

Upvotes: 3

Related Questions