Copied to clipboard

Flag this post as spam?

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


  • Girish Khadse 52 posts 132 karma points
    Aug 01, 2013 @ 09:35
    Girish Khadse
    0

    How to maintain current culture across threads

    We are creating multilingual site in 3 languages. We have created different node for each of the language and added pages for each of the name. Now I am facing some issues with the localization :
    1. Thread.CurrentThread.CurrentCulture is not maintained across threads. As I see it is most common issue, but somehow I am not able to find a good solution to it. Right now I am maintaining one session variable which also maintains the current culture. But the issue is it gets cleared as soon as session ends. I wanted how localization and this thread issue is handled normally in umbraco.
    2. There is also requirement to set the default language of the site to the users browsers language. that mean , user should be able to see  the site in browsers language first time and then he can change the language. Is there some way in umbraco to handle this?

  • Kasper Holm 47 posts 180 karma points
    Aug 01, 2013 @ 09:52
    Kasper Holm
    0

    Hello Girish

    what version of umbraco are you using ? :)

    and im not sure what you mean by the first point have you set the language / host name for the root node ? then Thread.CurrentThread.CurrentCulture should be okay :)

    and for point two, i actually often get this requst, and what we often end up doing is checking the browser language and redirect them to the language that macthes, if theres is one. This is not done on deep links only the front page and lading pages :)

  • Girish Khadse 52 posts 132 karma points
    Aug 01, 2013 @ 10:37
    Girish Khadse
    0

    Hi Kasper..

    Thanks a lot for the quick reply.

    1. I am using umbraco version 6.1.1. And I have set the language of root node and normally I get the selected language using Thread.CurrentThread.CurrentCulture.Name. But in some scenarios(ajax calls) my currentThread changes and I get the default umbraco language through Thread.CurrentThread.CurrentCulture.Name and my localization fails :( . Did you not ever faced such issue with CurrentThread.CurrentCulture?
    2. Yeah, I thought of it. But I am not getting where exactly I should write "Redirect" code. Currently I have overridden UmbracoApplication class by custom class which has SessionOnStart method setting currentCulture of currentThread to browser language. But SessionOnStart will also be called when user logs out. If you can suggest place where I should write my Redirect to browser language code, it would be of great help.
  • Kasper Holm 47 posts 180 karma points
    Aug 01, 2013 @ 11:15
    Kasper Holm
    0
    1. i certenly have, this should fix that, i usually call this in my constructor of my surfacecontrollers (this is stolen code from the core :) ) it simply sets the culture from the domain name works like a charm.

    public static class HostHelper { public static void TrySetCulture() { string domain = HttpContext.Current.Request.Url.Host; // host only if (TrySetCulture(domain)) return;

            domain = HttpContext.Current.Request.Url.Authority; // host with port
            if (TrySetCulture(domain)) return;
        }
    
        private static bool TrySetCulture(string domain)
        {
            var uDomain = Domain.GetDomain(domain);
            if (uDomain == null) return false;
            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(uDomain.Language.CultureAlias);
            return true;
        }
    }
    

    1. well here we have done it two ways, older forms solutions we do it in the masterpage for the frontpage / landingpages, and never MVC we do it in the controller, but we usually advises agienst it because its if go to a .dk domain you would expect toi get the danish site even if your browser is set to english :)

    i hope this helps :)

  • Girish Khadse 52 posts 132 karma points
    Aug 01, 2013 @ 11:43
    Girish Khadse
    0

    Thanks kasper.

    1. I will give try with that code and get back to you but it certainly looks something worth trying.
    2. I just want to set site lanaguage to browser lang only at first time when user opens the site. Then he is free to change his language. And I have only one domain hosting all 3 languages in which language is passed through url. (www.abc.com/en-us). I am still unclear about the solution you are proposing.
  • Girish Khadse 52 posts 132 karma points
    Aug 01, 2013 @ 12:32
    Girish Khadse
    0

    Update : I tried the code locally but seems like it is not working. I get domain as "localhost",hence "Domain.GetDomain(domain);" returns null.

  • Kasper Holm 47 posts 180 karma points
    Aug 01, 2013 @ 12:43
    Kasper Holm
    0

    Have added localhost as a hostname on a node in umbraco ? :)

  • Girish Khadse 52 posts 132 karma points
    Aug 01, 2013 @ 13:02
    Girish Khadse
    0

    Yup it is there.. But It is there with port no. Like this - "localhost:13267/en-us". Is it ok?

  • Kasper Holm 47 posts 180 karma points
    Aug 01, 2013 @ 13:55
    Kasper Holm
    0

    hey agien

    1. one thing i forgot to say about the host helper is has a bug if youre using http:// in the hostnames in umbraco, ex. host name set to http://www.abc.com, but it works fine whit just www.abc.com :) dont know how it works whit www.abc.com/en-us but let me know :) and it should just be local host without port :)
  • Moran 285 posts 934 karma points
    Jan 28, 2014 @ 10:33
    Moran
    0

    @Kasper Holm how did you call this in your controller constructor? can you share the code?

  • Moran 285 posts 934 karma points
    Mar 05, 2014 @ 08:36
    Moran
    0

    Any comments? can someone share the code for the constructor?

Please Sign in or register to post replies

Write your reply to:

Draft