Multilingual site browser redirect and language selector
Hi
We are creating a multilingual site with 24 languages.
I have created a scripting file to detect browser language and redirect to that spesific language version of the website. I have dropped that macro in home page template.
and i have created a language selector to navigate in to other language versions of the website dropped in the master template.
My problem is when i click any language in the language selector it is redirecting to browser language version.
BUMP. I'm facing the same challenge now in Umbraco 7. There seems to be a lot of outdated information in the forum about how to set up multilingual websites in earlier versions of Umbraco, but people that are asking how to things in umb 7 just don't get any answers.
I just managed to get up a single-domain multilingual site with Umbraco 7, but frankly, the videos at umbraco.tv weren't very helpful because they are all done in an earlier version. Hence, you quickly get confused, when details and visuals differ from the current umb version, and of course, none of the code works out of the box.
I finally figured out how to do the language switcher by looking at several outdated examples. (Code below, if anyone is interested. It should work with the plain Txt Starter Kit, allthough I have started customizing it a little bit.)
Now, my next important task is to make the site aware of the user's preferred language, i.e. detecting the browser language and redirect to the appropriate home page, /en/, /de/, /fr/, etc. I know how to do this in ASP.NET but since I'm relatively new to MVC/Razor, I need a little help. And wouldn't it be nice if a good multilingual starter kit was part of the standard Umbraco installation?
Any help much appreciated!
Thanks,
Bendik
---
Partial view macro code for a language selection dropdown in Umbraco 7 and the Txt Starter Kit:
<select id="selectLang" onchange="redirect(this)"> @foreach (umbraco.cms.businesslogic.language.Language language in allLanguages) { string selected = language == curLanguage ? "selected" : ""; string pageInOtherLangUrl = LanguageFunctions.FindPageInOtherLanguage(CurrentPage.Id, language.id); <option value="@pageInOtherLangUrl" @selected>@language.FriendlyName</option> } </select> <script type="text/javascript"> function redirect(selItem) { if (selItem.value != "") window.location = selItem.value; } </script>
And put this in your site's App_Code folder (LanguageFunctions.cshtml):
@using umbraco; @using Umbraco.Core; @using Umbraco.Core.Models; @using umbraco.cms.businesslogic.web; @functions { public static string FindPageInOtherLanguage(int pageId, int langId) { var rs = ApplicationContext.Current.Services.RelationService;
// See if this page has a related child page in the requested language IEnumerable<IRelation> relations = rs.GetByParentId(pageId).Where(r => r.RelationType.Alias == "relateDocumentOnCopy"); foreach (IRelation rel in relations) { Domain[] domains = library.GetCurrentDomains(rel.ChildId); if (domains.Length > 0 && domains[0].Language.id == langId) return library.NiceUrl(rel.ChildId); }
// See if this page has a related parent page in the requested language relations = rs.GetByChildId(pageId).Where(r => r.RelationType.Alias == "relateDocumentOnCopy"); foreach (IRelation rel in relations) { Domain[] domains = library.GetCurrentDomains(rel.ParentId); if (domains.Length > 0 && domains[0].Language.id == langId) return library.NiceUrl(rel.ParentId); } return string.Empty; } }
I guess you can use cookie for these needs. For example, if user visits first time the website the cookie will be IsBrowserLng = true. As soon as user has changed language using language selector the cookie value will be changed to false. Using this cookie value you can detect what language you should use.
Yes, using a cookie is a good idea! On our current website, I keep the user's choice of language in a cookie that has a relatively long expiry timespan. And, on the first request from a client, I use the Accept-Language in the request header (HttpRequest.UserLanguages in ASP.NET) to set the initial language of the cookie and redirect to the appropriate homepage.
I'm intending to do the same on this new Umbraco website, but since I am an Umbraco newbie, I could do with a little help on some basics: I'm not sure where, within the Umbraco framework, to put the initial cookie and redirection code. Would razor in my master template (umbLayout.cshtml) be a good approach?
Yes, there are certainly both pros and cons to the language cookie approach. However, this advice from Google doesn't necessarily mean "don't use cookies". What they do advise you to, is keeping content in different languages at different urls. And this is actually inherent in Umbraco's url handling, so this won't be a problem here. With Umbraco's relationship mechanism, you can easily redirect to the corresponding page in the users preferred language (if it exists), which will have a different url if given a different name. Because, in Umbraco, the relation between the pages does not depend on the name.
Now, is this redirection to a different url based on a cookie the best thing to do? Well, that's an other discussion, but there are good reasons to do it. How annoyed do we always get when we land on websites that asks you to select your language every time!?
Also feel your pain with outdated videos, code examples forum posts, etc. Would be a massive leap forward (and effort) to mark up with with the version they are for. We're not "all" expert enough just to jump syntaxes, particularly when it's all new ;) Maybe, on the forum, a dropdown next to the post allowing us to say what version num (or webforms/mvc) we're talking about would be useful. Could then be used to search on a particular version, or from a version onwards, or before a version backwards:)
Multilingual site browser redirect and language selector
Hi
We are creating a multilingual site with 24 languages.
I have created a scripting file to detect browser language and redirect to that spesific language version of the website. I have dropped that macro in home page template.
and i have created a language selector to navigate in to other language versions of the website dropped in the master template.
My problem is when i click any language in the language selector it is redirecting to browser language version.
Any suggestion on this please?
Thanks,
Lasya.
BUMP. I'm facing the same challenge now in Umbraco 7. There seems to be a lot of outdated information in the forum about how to set up multilingual websites in earlier versions of Umbraco, but people that are asking how to things in umb 7 just don't get any answers.
I just managed to get up a single-domain multilingual site with Umbraco 7, but frankly, the videos at umbraco.tv weren't very helpful because they are all done in an earlier version. Hence, you quickly get confused, when details and visuals differ from the current umb version, and of course, none of the code works out of the box.
I finally figured out how to do the language switcher by looking at several outdated examples. (Code below, if anyone is interested. It should work with the plain Txt Starter Kit, allthough I have started customizing it a little bit.)
Now, my next important task is to make the site aware of the user's preferred language, i.e. detecting the browser language and redirect to the appropriate home page, /en/, /de/, /fr/, etc. I know how to do this in ASP.NET but since I'm relatively new to MVC/Razor, I need a little help. And wouldn't it be nice if a good multilingual starter kit was part of the standard Umbraco installation?
Any help much appreciated!
Thanks,
Bendik
---
Partial view macro code for a language selection dropdown in Umbraco 7 and the Txt Starter Kit:
SelectLanguage.cshtml
And put this in your site's App_Code folder (LanguageFunctions.cshtml):
Hi Guys,
I guess you can use cookie for these needs. For example, if user visits first time the website the cookie will be IsBrowserLng = true. As soon as user has changed language using language selector the cookie value will be changed to false. Using this cookie value you can detect what language you should use.
Hope this helps.
Thanks, Oleg
Yes, using a cookie is a good idea! On our current website, I keep the user's choice of language in a cookie that has a relatively long expiry timespan. And, on the first request from a client, I use the Accept-Language in the request header (HttpRequest.UserLanguages in ASP.NET) to set the initial language of the cookie and redirect to the appropriate homepage.
I'm intending to do the same on this new Umbraco website, but since I am an Umbraco newbie, I could do with a little help on some basics: I'm not sure where, within the Umbraco framework, to put the initial cookie and redirection code. Would razor in my master template (umbLayout.cshtml) be a good approach?
Thanks, Bendik
I'm diving into this same issue and came across an article from Google back in 2010 where they advize against the cookie technique:
"And last but not least, keep the content for each language on separate URLs - don't use cookies to show translated versions."
http://googlewebmastercentral.blogspot.com/2010/03/working-with-multilingual-websites.html
Yes, there are certainly both pros and cons to the language cookie approach. However, this advice from Google doesn't necessarily mean "don't use cookies". What they do advise you to, is keeping content in different languages at different urls. And this is actually inherent in Umbraco's url handling, so this won't be a problem here. With Umbraco's relationship mechanism, you can easily redirect to the corresponding page in the users preferred language (if it exists), which will have a different url if given a different name. Because, in Umbraco, the relation between the pages does not depend on the name.
Now, is this redirection to a different url based on a cookie the best thing to do? Well, that's an other discussion, but there are good reasons to do it. How annoyed do we always get when we land on websites that asks you to select your language every time!?
Also feel your pain with outdated videos, code examples forum posts, etc. Would be a massive leap forward (and effort) to mark up with with the version they are for. We're not "all" expert enough just to jump syntaxes, particularly when it's all new ;) Maybe, on the forum, a dropdown next to the post allowing us to say what version num (or webforms/mvc) we're talking about would be useful. Could then be used to search on a particular version, or from a version onwards, or before a version backwards:)
Just a thought.
Craig
is working on a reply...