Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Andreas Kristensen 58 posts 258 karma points c-trib
    Feb 05, 2018 @ 14:13
    Andreas Kristensen
    0

    Changing current culture on the fly

    Hello!

    I have been searching high and low, for a solution to my problem. Most of the solutions are the same, but none of them seems to work.

    I want to change the culture of my website, on the fly, but I just can't get it to work.

    I have mainly tried two different things:

    CultureInfo newLanguage = new CultureInfo(language);
    
    System.Threading.Thread.CurrentThread.CurrentCulture = newLanguage;
    System.Threading.Thread.CurrentThread.CurrentUICulture = newLanguage;
    

    Calling it from a helper class, this does not work. I also tried:

    Culture = UICulture = "da-DK"; //Or any other language
    

    Did not work either.

    So, do any of you know what works?

  • Alex Skrypnyk 6060 posts 23282 karma points MVP 6x admin c-trib
    Feb 05, 2018 @ 14:18
    Alex Skrypnyk
    0

    Hi Andreas

    Where are you inserting this code?

  • Andreas Kristensen 58 posts 258 karma points c-trib
    Feb 05, 2018 @ 14:23
    Andreas Kristensen
    0

    The top code is in a helper class. I have tried to call it from a template, and from a surface controller.

    The bottom code, I have tried to call it from the template.

    The template is the "highest" parent template.

  • Alex Skrypnyk 6060 posts 23282 karma points MVP 6x admin c-trib
    Feb 05, 2018 @ 14:30
    Alex Skrypnyk
    0

    Can you show your helper class?

    This code changes the culture for the current request.

  • Andreas Kristensen 58 posts 258 karma points c-trib
    Feb 06, 2018 @ 06:52
    Andreas Kristensen
    0

    This is the code for the helper class. I have read that it only changes the language for the current request, but it is one of the solutions that pop up, again and again, so I included it in my list of "stuff I have already tried"..

           public static void SetLanguage(string language)
           {
               CultureInfo newLanguage = new CultureInfo(language);
    
               System.Threading.Thread.CurrentThread.CurrentCulture = newLanguage;
               System.Threading.Thread.CurrentThread.CurrentUICulture = newLanguage;
           }
    
  • Alex Skrypnyk 6060 posts 23282 karma points MVP 6x admin c-trib
    Feb 06, 2018 @ 15:21
    Alex Skrypnyk
    0

    Hi Andreas,

    This code is working for me:

        Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
    

    I use it in "UmbracoAuthorizedJsonController", and culture is really switching after calling this code.

    What version of Umbraco are you using?

    Alex

  • Andreas Kristensen 58 posts 258 karma points c-trib
    Feb 07, 2018 @ 07:10
    Andreas Kristensen
    0

    I use v. 7.5.14.

    Maybe your code is working, because it's inside code for Authorization. Such could would possibly be called on every request, and thus the culture is always switched.

  • Andreas Kristensen 58 posts 258 karma points c-trib
    Feb 08, 2018 @ 09:05
    Andreas Kristensen
    100

    Finally found a solution. We made an extention method for IIdentity, that uses the code from Alex. We then just call this code on every template, and it works.

Please Sign in or register to post replies

Write your reply to:

Draft