9

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

TechNo
  • 615
  • 2
  • 7
  • 24

3 Answers3

22

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


    }
Dudi
  • 3,069
  • 1
  • 27
  • 23
  • 1
    that's what i was looking for. For multithread web apps CurrentThread solution does not work. It only changes the culture of main thread. – DLL_Whisperer Sep 29 '17 at 06:54
16

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>
TechNo
  • 615
  • 2
  • 7
  • 24
4

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;
    }
  }
}
Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89
  • 1
    Uiculture and Thread culture are not for the same use. check this : http://stackoverflow.com/questions/9507582/what-is-the-difference-between-culture-and-uiculture –  Jun 05 '14 at 12:33
  • @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. – Ondrej Svejdar Jun 05 '14 at 12:37
  • 1
    Puting the Test in the Application_BeginRequest() is a bit heavy. Each time you do a request you make the Test. :/ –  Jun 05 '14 at 12:39
  • 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.DefaultThreadCurrentUICulture; but the code @Resources.FirstName is still loading as English instead of german – TechNo Jun 05 '14 at 12:43
  • 1
    @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. – Ondrej Svejdar Jun 05 '14 at 12:53
  • @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.DefaultThreadCurrentUICulture; --- which is loading the default language German. and in side the same page am calling names from resources but its coming as english.... – TechNo Jun 05 '14 at 12:58
  • @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 ? – Ondrej Svejdar Jun 05 '14 at 13:01
  • @OndrejSvejdar Resources.resx Resources.de.resx – TechNo Jun 05 '14 at 13:29
  • @SajnaAli - are Resources.de.resx marked as "Embedded Resource" ? (right-click - Properties) – Ondrej Svejdar Jun 05 '14 at 13:32
  • Should i do anything in RouteConfig? – TechNo Jun 05 '14 at 13:41