Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set culture as globally in mvc5

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

like image 913
TechNo Avatar asked Jun 05 '14 12:06

TechNo


People also ask

How do you change the current UI culture?

To change the current UI culture, you assign the CultureInfo object that represents the new UI culture to the Thread. CurrentThread. CurrentUICulture property.

What is ASP Net culture setting?

In an ASP.NET Web page, you can set to two culture values, the Culture and UICulture properties. The Culture value determines the results of culture-dependent functions, such as the date, number, and currency formatting, and so on. The UICulture value determines which resources are loaded for the page.


3 Answers

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


    }
like image 154
Dudi Avatar answered Sep 29 '22 06:09

Dudi


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>
like image 27
TechNo Avatar answered Sep 29 '22 08:09

TechNo


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;
    }
  }
}
like image 24
Ondrej Svejdar Avatar answered Sep 29 '22 07:09

Ondrej Svejdar