Created
March 21, 2018 16:17
-
-
Save hardye/47e979e7b473beba3349df0b87df49c0 to your computer and use it in GitHub Desktop.
Sitefinity Language Redirect
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
protected void Session_Start(object sender, EventArgs e) | |
{ | |
// Honor the accept-language-header sent by the user's browser and redirect them to the | |
// desired language version of the site (if it is available). | |
ResourcesConfig config = Config.Get<ResourcesConfig>(); | |
if (!config.Multilingual) | |
{ | |
return; | |
} | |
try | |
{ | |
RedirectByLanguage(config); | |
} | |
catch (Exception ex) | |
{ | |
// Don't let redirection errors cause visible exceptions. | |
// Simply log them and move on. | |
Log.Write(ex, ConfigurationPolicy.ErrorLog); | |
} | |
} | |
/// <summary> | |
/// Redirects a user according to their browser-configured language preferences. | |
/// </summary> | |
private void RedirectByLanguage(ResourcesConfig config) | |
{ | |
// Do this only if the request is for the domain root | |
string currentRequestPath = System.Web.HttpContext.Current.Request.Path; | |
if (!currentRequestPath.Equals("/", StringComparison.InvariantCultureIgnoreCase)) | |
{ | |
return; | |
} | |
// Abort if there is no accept-language header (search engines etc.) | |
string acceptLanguageHeader = Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"]; | |
if (String.IsNullOrWhiteSpace(acceptLanguageHeader)) | |
{ | |
return; | |
} | |
// Get the default language of the site | |
string defaultLanguage = config.DefaultCulture.Culture; | |
// Abort if the accept-language header contains the default language as the first entry. | |
if (acceptLanguageHeader.StartsWith(defaultLanguage, StringComparison.InvariantCultureIgnoreCase)) | |
{ | |
return; | |
} | |
// Do we have any language in our configured languages that matches any of the accept-language header values? | |
string targetLang = null; | |
string[] acceptLangs = acceptLanguageHeader.Split(new[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries); | |
int i = 0; | |
do // Loop through all the browser-configured languages ... | |
{ | |
string langKey = acceptLangs[i].Substring(0, 2); | |
if (!langKey.Equals("q=", StringComparison.InvariantCultureIgnoreCase)) | |
{ | |
// ... and try to match them to one of the languages of our site | |
if (config.Cultures.Values.Any(culture => langKey.Equals(culture.Culture, StringComparison.InvariantCultureIgnoreCase))) | |
{ | |
targetLang = langKey; | |
} | |
} | |
i += 1; | |
} while (String.IsNullOrEmpty(targetLang) && i < acceptLangs.Length); | |
if (!String.IsNullOrEmpty(targetLang)) | |
{ | |
Response.Redirect(String.Format("~/{0}", targetLang)); // This assumes that the URL localization strategy is "subfolders"! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment