以编程方式更改页面的区域设置(语言)
我的 Umbraco 网站中有两个根节点.. 一个设置为英语,另一个使用管理主机名设置为德语 ..
|- en
|---- english page1
|---- english page2
|- de
|---- german page1
|---- german page2
http:// /mywebsite.com 设置为 en 节点和 http://mywebsite.de 设置为de节点。
在某些情况下,我需要将德语节点的语言更改为英语。这可能吗?如何实现?
例如,如果有人使用德语主机名调用英语页面,我需要将语言环境更改为英语
例如 http://mywebsite.de/english-page1.aspx 应该采用英语区域设置..所以字典等需要从英语加载
http://mywebsite.com/german-page1.aspx 应该采用德语语言环境..所以字典等需要从德语加载
我已经编写了一个 HttpModule 来更改 PreRequestHandlerExecute 上的语言环境,但没有成功
void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("de-CH");
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("de-CH");
}
I have two root nodes in my Umbraco website.. one is set to English and other is set to German using Manage Hostnames ..
|- en
|---- english page1
|---- english page2
|- de
|---- german page1
|---- german page2
http://mywebsite.com is set to en node and http://mywebsite.de is set to de node.
I need to change the German node's language to English in certain conditions.. Is this possible and how?
For example if someone calls an English page using German hostname, I need to change the locale to English
For example
http://mywebsite.de/english-page1.aspx should be in English locale.. so the dictionary etc need to be loaded from English
http://mywebsite.com/german-page1.aspx should be in German locale.. so the dictionary etc need to be loaded from German
I have written an HttpModule to change the locale on PreRequestHandlerExecute but without no success
void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("de-CH");
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("de-CH");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为 PreRequestHandler 在页面周期中太早了。在调用 default.aspx 页面时,区域性由 Umbraco 设置。我自己将文化更改添加到我的基本 MasterPage 的构造函数中,该 MasterPage 始终在任何页面上调用。您还可以在页面初始化或页面加载时更改区域性。
亲切的问候,
Corné Hogerheijde
I think the PreRequestHandler is too early in the page cycle. On the call to the default.aspx page the culture is set by Umbraco. I myself added the culture change to the constructor of my base MasterPage, the masterpage which is always called on any page. You could also change culture at Page Init or Page Load.
Kind regards,
Corné Hogerheijde
您可以在
Session_Start
上检查主机并将其重定向到特定语言页面,而无需太多麻烦You can check for the host on
Session_Start
and redirect them to particular language page without much hassle我意识到这已经很老了,但我在寻找答案时发现了它,并认为我应该分享我所做的事情。我正在使用 Umbraco 7.5 和 MVC。
首先,我创建了一个过滤器:
然后在
OnApplicationStarted
中添加了过滤器每当我想更改语言/区域设置时,我只需添加
?lang=en
或?lang= fr
到任何网址。这也会改变我显示的文本。我的每个文本字段都以简单语言代码为前缀,例如。 “fr_pageTitle”和“en_pageTitle”。然后我有一个扩展方法可以从我的 MVC 视图中提取正确的文本希望这对某人有帮助
I realise this is very old, but I found it when looking for an answer and thought I'd share what I've done. I'm using Umbraco 7.5 and MVC.
First I created a Filter:
Then added the filter on in
OnApplicationStarted
Whenever I want to change the lang/locale I just add
?lang=en
or?lang=fr
to any url. This also changes which text I display. Each of my text fields are prefixed with the simple language code eg. 'fr_pageTitle' and 'en_pageTitle'. I then have an extension method to pull out the correct text from my MVC viewHope this helps someone