Last active
August 9, 2016 09:49
-
-
Save pedroadaodev/081a32c773bd20224b095cb0f8280748 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? | |
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> *@ // DEBUG | |
foreach (var homepage in allHomapeges) | |
{ | |
if (homepage.Name.Equals(letfLanguageTag, StringComparison.InvariantCultureIgnoreCase)) | |
{ | |
urlToRedirect = homepage.Url; | |
detectedLanguage = true; | |
break; | |
} | |
if (detectedLanguage) | |
{ | |
break; | |
} | |
} | |
} | |
} | |
} | |
Response.RedirectPermanent(urlToRedirect); // DONT END RESPONSE | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More info in my blog
http://adaodeveloper.blogspot.pt/2016/04/umbraco-redirect-root-node-to-users.html