以编程方式更改页面的区域设置(语言)

发布于 2024-12-25 13:14:28 字数 1119 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心清如水 2025-01-01 13:14:28

我认为 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

软的没边 2025-01-01 13:14:28

您可以在 Session_Start 上检查主机并将其重定向到特定语言页面,而无需太多麻烦

void Session_Start(object sender, EventArgs e) 
{
    // Your logic will go here

}

You can check for the host on Session_Start and redirect them to particular language page without much hassle

void Session_Start(object sender, EventArgs e) 
{
    // Your logic will go here

}
临风闻羌笛 2025-01-01 13:14:28

我意识到这已经很老了,但我在寻找答案时发现了它,并认为我应该分享我所做的事情。我正在使用 Umbraco 7.5 和 MVC。

首先,我创建了一个过滤器:

public class LanguageFilterAttribute : IActionFilter
{
    public void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var httpContext = filterContext.RequestContext.HttpContext;

        if (!string.IsNullOrEmpty(httpContext?.Request.QueryString["lang"]))
        {
            if (httpContext.Request.QueryString["lang"].ToLower().StartsWith("en"))
                httpContext.Session["lang"] = "en";
            else if (httpContext.Request.QueryString["lang"].ToLower().StartsWith("fr"))
                httpContext.Session["lang"] = "fr";
        }

        if (httpContext.Session["lang"] != null)
        {
            switch (httpContext.Session["lang"].ToString())
            {
                case "en":
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
                    break;
                case "fr":
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
                    break;
            }
        }
    }


    public void OnActionExecuted(ActionExecutedContext filterContext)
    {

    }
}

然后在 OnApplicationStarted 中添加了过滤器

public class MyUmbracoApplication : Umbraco.Web.UmbracoApplication
{       

    protected override void OnApplicationStarted(object sender, EventArgs e)
    {
        base.OnApplicationStarted(sender, e);

        GlobalFilters.Filters.Add(new LanguageFilterAttribute());
    }

}

每当我想更改语言/区域设置时,我只需添加 ?lang=en?lang= fr 到任何网址。这也会改变我显示的文本。我的每个文本字段都以简单语言代码为前缀,例如。 “fr_pageTitle”和“en_pageTitle”。然后我有一个扩展方法可以从我的 MVC 视图中提取正确的文本

public static class PublishedContentExtensions
{
    public static T GetPropertyLangValue<T>(this IPublishedContent content, string fieldName)
    {
        var lang = CoreHelper.GetSessionLanguage();
        if (string.IsNullOrEmpty(lang))
            return content.GetPropertyValue<T>(fieldName);

        return content.GetPropertyValue<T>($"{fieldName}_{lang}");
    }

}

希望这对某人有帮助

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:

public class LanguageFilterAttribute : IActionFilter
{
    public void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var httpContext = filterContext.RequestContext.HttpContext;

        if (!string.IsNullOrEmpty(httpContext?.Request.QueryString["lang"]))
        {
            if (httpContext.Request.QueryString["lang"].ToLower().StartsWith("en"))
                httpContext.Session["lang"] = "en";
            else if (httpContext.Request.QueryString["lang"].ToLower().StartsWith("fr"))
                httpContext.Session["lang"] = "fr";
        }

        if (httpContext.Session["lang"] != null)
        {
            switch (httpContext.Session["lang"].ToString())
            {
                case "en":
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
                    break;
                case "fr":
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
                    break;
            }
        }
    }


    public void OnActionExecuted(ActionExecutedContext filterContext)
    {

    }
}

Then added the filter on in OnApplicationStarted

public class MyUmbracoApplication : Umbraco.Web.UmbracoApplication
{       

    protected override void OnApplicationStarted(object sender, EventArgs e)
    {
        base.OnApplicationStarted(sender, e);

        GlobalFilters.Filters.Add(new LanguageFilterAttribute());
    }

}

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 view

public static class PublishedContentExtensions
{
    public static T GetPropertyLangValue<T>(this IPublishedContent content, string fieldName)
    {
        var lang = CoreHelper.GetSessionLanguage();
        if (string.IsNullOrEmpty(lang))
            return content.GetPropertyValue<T>(fieldName);

        return content.GetPropertyValue<T>($"{fieldName}_{lang}");
    }

}

Hope this helps someone

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文