Copied to clipboard

Flag this post as spam?

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


  • J 351 posts 606 karma points
    Apr 09, 2020 @ 10:48
    J
    0

    How to set culture from code-behind

    Im using Umbraco 7 with a web user control. I have a template set with culture and hostnames set as inherited and on my user control page load event i add

        Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
    

    I have an resx file setup but the info never pulls through for Germany?

    Any pointers please?

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Apr 09, 2020 @ 11:24
    Alex Skrypnyk
    1

    Hi J

    Can you check what culture is set to the domain you are running in Umbraco? Check hostnames and cultures for the root node

    Thanks,

    Alex

  • J 351 posts 606 karma points
    Apr 09, 2020 @ 11:44
    J
    0

    Hi Alex the root is set to inherit?

    Thanks

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Apr 09, 2020 @ 12:36
    Alex Skrypnyk
    1

    Hi J

    What about the domains section? Did you add the domain you use?

  • J 351 posts 606 karma points
    Apr 09, 2020 @ 12:44
    J
    0

    Ah i think i know what you're asking. So i right clicked the root node at the top of the page and that is set to en-US against the domain name.

    I then have a usercontrol further down the line, where i would like to override or set a new culture and language so the content in me resx would display.

    Many thanks

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Apr 09, 2020 @ 13:14
    Alex Skrypnyk
    1

    J, if you want to render "de-DE" in the English version page I think you have to set the culture in the correct event handler in the user control.

    Where do you try to set it? In Which event?

  • J 351 posts 606 karma points
    Apr 10, 2020 @ 11:47
    J
    0

    Hey Alex

    Sorry for the multiple edits. How about Page load? I can set it there to get the info from my resx file (unless it can be automatically which is great)?

    just tried this and this didnt work either :-(

            var Newculture = new System.Globalization.CultureInfo("de-DE");
            System.Globalization.CultureInfo.DefaultThreadCurrentCulture = Newculture;
            System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = Newculture;
    
            lblInfo.Text = (string)GetGlobalResourceObject("Resource", "Info");
    

    The template lang is set to en and inherit on the nodes but on page load i look at the variables and theyre set to German (or whatever i set it to) if i enter the below code i can see the name set by the above code

    Newculture.EnglishName;
    

    not sure what the heck is going on? I then viewed page source and can see href lang="en". Changed to "de" which didnt make a difference unless im missing something?

    Hoping someone could point me in the right direction?

    Thanks again

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Apr 10, 2020 @ 12:55
    Alex Skrypnyk
    0

    Hey J

    You can pass CultureInfo to the method: GetGlobalResourceObject(String, String, CultureInfo)

    lblInfo.Text = (string)GetGlobalResourceObject("Resource", "Info", Newculture);
    
  • J 351 posts 606 karma points
    Apr 10, 2020 @ 13:11
    J
    0

    Hey Alex

    I get the error

    No overload for method 'GetGlobalResourceObject' takes 3 arguments
    

    I then thought about changing the Resource name as my France resource file is called Resource.fr-FR

    lblInfo.Text = (string)GetGlobalResourceObject("Resource.fr-FR", "Info");
    

    (Note the new Resource class name - but this threw an error :-(

    Thanks

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Apr 10, 2020 @ 13:31
    Alex Skrypnyk
    100
    var userLanguage = "de-DE";
    
    System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(userLanguage);
    System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo(userLanguage);
    
    lblInfo.Text = (string)HttpContext.GetGlobalResourceObject("Resource", "Info");
    

    What about this way?

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Apr 10, 2020 @ 13:33
    Alex Skrypnyk
    1

    try to use GetLocalResourceObject method like this:

    HttpContext.GetLocalResourceObject("Resource", "Info", New CultureInfo("de"))

  • J 351 posts 606 karma points
    Apr 10, 2020 @ 15:53
    J
    0

    THANK YOU Alex - This is now working. Thanks again very much. Hopefully i dont come across any more issues but really thankful for getting me on track.

    Just a side question :-). If i want to swap between languages could i store the value in session so if a user goes from German to English to Russian over multiple pages it would display the appropriate language selected?

    Thanks again

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Apr 10, 2020 @ 16:06
    Alex Skrypnyk
    1

    Amazing, happy to help.

    You can store in the session selected language, but as I understood page language is English but you need to show German - isn't it an issue we solved?

  • J 351 posts 606 karma points
    Apr 10, 2020 @ 18:13
    J
    1

    Yes it is an issue that's resolved but just in case I need to add more languages in future.

    I didn't want to complicate things but I save the data in the database for Germany and United Kingdom. I have one template/page and can load the data. The problem I was having certain static words on the template were not displaying in German.

    The above resolved that but I thought to make it a little more reusable where if I need to add another country in future I could save the language in session and load the same template with another country's language.

    Thanks

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies