-
-
Save ndias86/f9f251b914a6eccf5a9b99d3591aca38 to your computer and use it in GitHub Desktop.
Umbraco : on root node, redirect to current users browser culture ex: /en/ or /fr/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using System.Globalization | |
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage | |
@{ | |
Layout = null; | |
string[] languages = HttpContext.Current.Request.UserLanguages; | |
var detectedLanguage = false; | |
var urlToRedirect = "/en/"; // TODO : go to first node homepage? | |
HttpCookie cookieOptIn = Request.Cookies.Get("curLang"); | |
@* If it's the first time a user goes to this website and there's no cookie yet *@ | |
if(cookieOptIn == null) | |
{ | |
if (languages != null && languages.Any()) | |
{ | |
var allHomapeges = Model.Content.Descendants("Homepage"); | |
if (allHomapeges != null && allHomapeges.Any()) | |
{ | |
foreach (var lang in languages) | |
{ | |
if (detectedLanguage) | |
{ | |
break; | |
} | |
var letfLanguageTag = CultureInfo.GetCultureInfoByIetfLanguageTag(lang.Split(';')[0]).IetfLanguageTag; | |
<div>@lang » @letfLanguageTag</div> | |
foreach (var homepage in allHomapeges) | |
{ | |
if (homepage.Name.Equals(letfLanguageTag, StringComparison.InvariantCultureIgnoreCase)) | |
{ | |
urlToRedirect = homepage.Url; | |
cookieOptIn = new HttpCookie("curLang"); | |
cookieOptIn.Value = urlToRedirect; | |
cookieOptIn.Expires = DateTime.Now.AddDays(30); | |
Response.Cookies.Add(cookieOptIn); | |
detectedLanguage = true; | |
break; | |
} | |
if (detectedLanguage) | |
{ | |
break; | |
} | |
} | |
} | |
} | |
} | |
} | |
else | |
{ | |
var urlPPath = cookieOptIn.Value; | |
urlToRedirect = urlPPath; | |
} | |
Response.Redirect(urlToRedirect); // DONT END RESPONSE | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment