2

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I have few resource string in my master page as well. Do I have to define all those strings in resource file of all pages?
@Hermant: you add resources for master pages in the same way, as for aspx pages. Instead of calling the resource file pagefilename.aspx.resx you call it masterfilename.master.resx.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.