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?
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 :)
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?
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.
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;
}
}
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 will give try with that code and get back to you but it certainly looks something worth trying.
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.
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 :)
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?
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 :)
Hi Kasper..
Thanks a lot for the quick reply.
public static class HostHelper { public static void TrySetCulture() { string domain = HttpContext.Current.Request.Url.Host; // host only if (TrySetCulture(domain)) return;
i hope this helps :)
Thanks kasper.
Update : I tried the code locally but seems like it is not working. I get domain as "localhost",hence "Domain.GetDomain(domain);" returns null.
Have added localhost as a hostname on a node in umbraco ? :)
Yup it is there.. But It is there with port no. Like this - "localhost:13267/en-us". Is it ok?
hey agien
@Kasper Holm how did you call this in your controller constructor? can you share the code?
Any comments? can someone share the code for the constructor?
is working on a reply...