如何更改用户在 Sitecore 网站上看到的当前语言?
假设我有一个网站被翻译成两种语言 - 英语和西班牙语。我想在页面顶部为用户提供一个切换开关,允许他们在两种语言之间来回切换。我该怎么做呢?我不想建立不同的网站。我只想要一个网站。我只希望用户能够单击按钮或链接或其他内容,然后从英语转到西班牙语。现在,当他们在网站上移动时,他们正在查看整个西班牙语网站。然后,如果他们再次单击按钮或链接,他们将返回英语。我必须在后面的代码中执行某些操作才能告诉 Sitecore“使用”不同的语言吗?
Let's say I have a web site that is translated in to two languages - English and Spanish. I want to give the users a toggle switch at the top of the page that allows them to switch back and forth between the two languages. How would I do that? I don't want to set up different websites. I only want one website. I just want the user to be able to click on a button or link or something and go from English to Spanish. Now as they move around the site they are viewing the entire site in Spanish. Then if they click on the button or link again they would go back to English. Is there something I have to do in the code behind to tell Sitecore to "use" a different language?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用“sc_lang”查询字符串参数切换上下文语言。例如:
You can switch the context language using "sc_lang" query string parameter. For example:
通常在 Sitecore 中,您需要使用内置 URL 前缀来表示语言,例如 http://www.example。 com/en 或 http://www.example.com/es。我们经常使用在 httpRequestBegin 管道中读取的 cookie 来增强 Sitecore LanguageResolver。您的语言“切换”可以适当地设置该 cookie。
有关更多详细信息和示例代码,请参阅 John West 撰写的这篇文章。
https://community.sitecore.net/technical_blogs/b/sitecorejohn_blog/posts/repost-overriding-sitecore-39-s-logic-to-define-the-context-language
Typically in Sitecore you will want to represent the language using the built-in URL prefixes, e.g. http://www.example.com/en or http://www.example.com/es. We often use a cookie that we read in the httpRequestBegin pipeline to enhance the Sitecore LanguageResolver. Your language "toggle" could appropriately set that cookie.
See this article by John West for more detail and sample code.
https://community.sitecore.net/technical_blogs/b/sitecorejohn_blog/posts/repost-overriding-sitecore-39-s-logic-to-determine-the-context-language
您可以编写自己的 LanguageResolver Pipeline 来执行此操作。
或者您可以使用 sc_lang 查询字符串参数来扩展您的 URL,例如:sc_lang=en-EN 或 sc_lang=de-DE。如果您已正确配置链接管理器,这应该可以工作。
You can write your own LanguageResolver Pipeline to do this.
Or you can extend your URL by Using the sc_lang querystring Parameter eg.: sc_lang=en-EN or sc_lang=de-DE. If you have configured your linkmanager fine this should work.