如何通过浏览器的语言在 Sitecore 中设置上下文语言?
Posted
技术标签:
【中文标题】如何通过浏览器的语言在 Sitecore 中设置上下文语言?【英文标题】:How to set context language in Sitecore by browser's language(s)? 【发布时间】:2014-12-31 09:11:35 【问题描述】:我需要通过浏览器语言(语言)设置上下文语言并重定向到正确的语言版本。由于搜索引擎的索引,我还需要排除机器人。
【问题讨论】:
【参考方案1】:我找到了关于 "Browser Language Detection is Sitecore" from Mark Stiles 的文章,但这并不是我想要的,因为他完全覆盖了原生功能。
几个小时后,我创建了自己的语言解析器(Mark 也有部分代码),用于检测浏览器语言并在需要时进行重定向并排除机器人。
public class LanguageResolver : Sitecore.Pipelines.HttpRequest.LanguageResolver
public override void Process(Sitecore.Pipelines.HttpRequest.HttpRequestArgs args)
Assert.ArgumentNotNull(args, "args");
Language lang = GetLanguageFromBrowser(args);
if (lang != null)
Sitecore.Context.Language = lang;
// if current language doesn't match with default site language, it will do a redirect
if (Sitecore.Context.Language.Name != Sitecore.Context.Site.Language)
// do redirect
UriBuilder uriBuilder = new UriBuilder(args.Context.Request.Url);
uriBuilder.Path = Sitecore.Context.Language.Name + "/";
HttpContext.Current.Response.Redirect(uriBuilder.Uri.ToString(), true);
args.AbortPipeline();
else
base.Process(args);
/// <summary>
/// Gets language from browser but only when language cookie doesn't exist yet and URL is related to homepage
/// Logic taken from https://markstiles.net/Blog/2013/04/01/browser-language-detection-in-sitecore.aspx
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
public Language GetLanguageFromBrowser(Sitecore.Pipelines.HttpRequest.HttpRequestArgs args)
// site must be inicialized, language si resolved only for homepage and a site must have in its configuration resolveBrowserLang="true" !
if (IsBrowserLangDetectionAllowed(args))
// default site language
Language defaultLangPrefix = LanguageManager.GetLanguage(Sitecore.Context.Site.Language);
// gets browser languages
string[] userLangs = args.Context.Request.UserLanguages;
if (userLangs != null && userLangs.Any())
foreach (string userLang in userLangs)
// gets first part where information about language is stored
string lang = userLang.Split(new char[] ';' , StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
if (!String.IsNullOrEmpty(lang))
Language scLang;
// if lang starts with the same letters as are in defaultLangPrefix
if (defaultLangPrefix != null && lang.StartsWith(defaultLangPrefix.CultureInfo.TwoLetterISOLanguageName))
scLang = defaultLangPrefix;
else
if (!Language.TryParse(lang, out scLang))
continue;
// then try to get the language item id from the language or two letter iso code
ID scLangId = LanguageManager.GetLanguageItemId(scLang, Sitecore.Context.Database);
if (ID.IsNullOrEmpty(scLangId))
//sometimes the language found is slightly different than official language item used in SC
scLang = LanguageManager.GetLanguage(scLang.CultureInfo.TwoLetterISOLanguageName);
scLangId = LanguageManager.GetLanguageItemId(scLang, Sitecore.Context.Database);
if (ID.IsNullOrEmpty(scLangId))
continue;
Item startItem = Sitecore.Context.Database.GetItem(Sitecore.Context.Site.StartPath, scLang);
//if you've got a version in this language then use that language
if (startItem != null && startItem.Versions.Count > 0)
return scLang;
// fallback language from site configuration
return LanguageManager.GetLanguage(Sitecore.Context.Site.Properties["resolveBrowserFallbackLang"]);
return null;
/// <summary>
/// Determinates if detection is allowed
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
protected bool IsBrowserLangDetectionAllowed(Sitecore.Pipelines.HttpRequest.HttpRequestArgs args)
// site must be defined
return Sitecore.Context.Site != null
// lang cookie is not already set
&& !args.Context.Request.Cookies.AllKeys.Contains(Sitecore.Context.Site.GetCookieKey("lang"))
// URL is related to homepage
&& args.Context.Request.Url.AbsolutePath == "/"
// site must have property resolveBrowserLang="true"
&& Sitecore.Context.Site.Properties["resolveBrowserLang"] == "true"
// user agent is not robot
&& !Sitecore.Analytics.Configuration.AnalyticsSettings.Robots.ExcludeList.ContainsUserAgent(args.Context.Request.UserAgent);
客户的请求只针对首页,所以脚本可以扩展。
【讨论】:
您可以通过单击“勾选”来“接受”您自己的答案。这会将其从 *** 中的未解决问题列表中删除。 更新了"Browser Language Detection is Sitecore" from Mark Stilesmarkstiles.net/blog/2013/4/1/…的链接以上是关于如何通过浏览器的语言在 Sitecore 中设置上下文语言?的主要内容,如果未能解决你的问题,请参考以下文章