How to set culture as globally in mvc5

21,745

Solution 1

In web.config I commented the following line inside and it worked fine for me.

<configuration>
   <system.web>
    <globalization culture="en-US" uiCulture="en-US" />  <!-- this only -->
   </system.web>
</configuration>

Solution 2

for globally setting I suggest you to add the following lines to the global.asax.cs file: (in this example the culture sets to Israel hebrew )

        protected void Application_Start()
    {
        //The culture value determines the results of culture-dependent functions, such as the date, number, and currency (NIS symbol)
        System.Globalization.CultureInfo.DefaultThreadCurrentCulture = new System.Globalization.CultureInfo("he-il");
        //System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = new System.Globalization.CultureInfo("he-il");


    }

Solution 3

You have to persist the information about current culture somewhere (I recommend cookie) and set thread culture to this cookie value (if present) - preferably in Application_BeginRequest of your Global.asax.

public ActionResult ChangeCulture(string value) {
  Response.Cookies.Add(new HttpCookie("culture", value));  
  return View();
}

public class MvcApplication : HttpApplication {
  protected void Application_BeginRequest() {
    var cookie = Context.Request.Cookies["culture"];
    if (cookie != null && !string.IsNullOrEmpty(cookie.Value)) {
      var culture = new CultureInfo(cookie.Value);
      Thread.CurrentThread.CurrentCulture = culture;
      Thread.CurrentThread.CurrentUICulture = culture;
    }
  }
}
Share:
21,745
TechNo
Author by

TechNo

Updated on August 08, 2022

Comments

  • TechNo
    TechNo over 1 year

    I am using resource files to switch languages in my web application which is build in mvc5

    In index files its reading the culture value which i set.

    I am calling the set culture method from layout.cshtml and calling its value with the following code.

    @{
    Layout = "~/Views/Shared/_Layout.cshtml";
    
    if (!Request["dropdown"].IsEmpty())
    {
        Culture = UICulture = Request["dropdown"];
    }
    

    }

    in index page the language is loading correctly but when from there when i go to the next page its loading the default language German but the resources reading from English resource file only.

    Please help me on this..anybody

  • Admin
    Admin almost 10 years
    Uiculture and Thread culture are not for the same use. check this : stackoverflow.com/questions/9507582/…
  • Ondrej Svejdar
    Ondrej Svejdar almost 10 years
    @Zekth - I know :) but Sajna Ali is setting them to the same value in the first place; so I guess that is what he wants.
  • Admin
    Admin almost 10 years
    Puting the Test in the Application_BeginRequest() is a bit heavy. Each time you do a request you make the Test. :/
  • TechNo
    TechNo almost 10 years
    first i set the application like this only. The language is changing from home page. then its working properly. but when i moved into the second page or any other page am not geting the language with the following code var culture = System.Globalization.CultureInfo.DefaultThreadCurrentUICultu‌​re; but the code @Resources.FirstName is still loading as English instead of german
  • Ondrej Svejdar
    Ondrej Svejdar almost 10 years
    @Zeth - thats the only way where to make it as each request starts a new thread and you want to set correct culture for each requst ;) Sajna Ali - please elaborate more, this feels like different question.
  • TechNo
    TechNo almost 10 years
    @Zekth with the help of resource file am switching languages in my index page. The culture name is storing as above as Ondrej Svejdar mentioned. I had put a variabele in the second page like var culture = System.Globalization.CultureInfo.DefaultThreadCurrentUICultu‌​re; --- which is loading the default language German. and in side the same page am calling names from resources but its coming as english....
  • Ondrej Svejdar
    Ondrej Svejdar almost 10 years
    @SajnaAli - this smells like a problem with resource files; are all marked correctly (as resources), what is the full file name of english and german resources ?
  • TechNo
    TechNo almost 10 years
    @OndrejSvejdar Resources.resx Resources.de.resx
  • Ondrej Svejdar
    Ondrej Svejdar almost 10 years
    @SajnaAli - are Resources.de.resx marked as "Embedded Resource" ? (right-click - Properties)
  • TechNo
    TechNo almost 10 years
    Should i do anything in RouteConfig?
  • DLL_Whisperer
    DLL_Whisperer over 6 years
    that's what i was looking for. For multithread web apps CurrentThread solution does not work. It only changes the culture of main thread.