Sebastian
Sebastian

Reputation: 4811

Set default culture mvc 5 web app

I am working on an MVC-5 application which is expected to have users from Asia & Europe . I am struggling with handling double values now . In asia people use . as seperator and others use , as seperator. I use below code to handle this by setting Culture.

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(lang);

System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;

But my question is how to set nb-NO as default culture ? and also allow user to change the culture back to en-US if they want?

I tried

<globalization culture="nb-NO" uiCulture="nb-NO" />

it in web.config but no luck

Upvotes: 1

Views: 1144

Answers (1)

Davide Piras
Davide Piras

Reputation: 44605

in general it should work from the web.config except that in there you would have a static value so better do it in the code in a way that you can even allow users to specify which culture to use by either a drop down selection, user setting in the database, cookies or any other method.

you can try to add this code to the Initialize method of your MVC Controller class, it should work!

protected override void Initialize(RequestContext requestContext)
{
      base.Initialize(requestContext);

      const string culture = "nb-NO";
      var ci = CultureInfo.GetCultureInfo(culture);

      Thread.CurrentThread.CurrentCulture = ci;
      Thread.CurrentThread.CurrentUICulture = ci;
}

Upvotes: 1

Related Questions