1

I've got a website which needs localizing, I have successfully achieved that automatically, by setting the culture to auto in the Web.Config and using my resx files for translation, However I'd also like a customer to be able to change the website's translation manually as a backup.

I have a form on my textbox with multiple language choices, this choice is POSTed to a controller as per the following code

[AllowAnonymous]
        [HttpPost]
        public ActionResult SelectLanguage(LoginViewModel model)
        {
            switch (Request["lang"])
            {
                case "English":
                   CultureInfo.CurrentCulture=new CultureInfo("en-GB");
                    CultureInfo.CurrentUICulture = new CultureInfo("en-GB");
                    break;
                case "French":
                    CultureInfo.CurrentCulture = new CultureInfo("fr-FR");
                    CultureInfo.CurrentUICulture = new CultureInfo("fr-FR");
                    break;



            }
            var currentCulture = CultureInfo.CurrentCulture;
            return RedirectToAction("Index");

        }

the problem is that while my currentCulture and CurrentUICulture do change, the pages remain untranslated however,If I change this manually in the Web.config, my site translates perfectly!

Is there something I'm missing here?

Thanks in advance =]

UPDATE existing answers involving the use of InitializeCulture aren't valid AFAIK as the events in WebPages naturally aren't available in MVC

Captain_Custard
  • 1,308
  • 6
  • 21
  • 35
  • possible duplicate of [Setting culture for session](http://stackoverflow.com/questions/8487856/setting-culture-for-session) – Stephen Sep 30 '15 at 13:22
  • I believe that's for webforms, as there isn't an InitializeCulture event in MVC to my knowledge – Captain_Custard Sep 30 '15 at 13:36
  • true, I was too hasty. Anyway, you can store the culturestring in your session, and set the culture in a custom actionfilterattribute – Stephen Sep 30 '15 at 13:43
  • Would you have an example of this by any chance? – Captain_Custard Sep 30 '15 at 13:44
  • take a look here: http://stackoverflow.com/questions/1560796/set-culture-in-an-asp-net-mvc-app – Stephen Sep 30 '15 at 13:45
  • But isn't that just for when a user lands on the page? what about for selecting from a list of languages and translating on the fly? – Captain_Custard Sep 30 '15 at 13:48
  • 1
    That link uses a bit different approach, but when you store the culturestring in your session after picking it from the list, you can read it in the attribute, and set the culture for that request. Add the actionfilter to all requests in the filterConfig,cs – Stephen Sep 30 '15 at 13:49

0 Answers0