以编程方式更改页面的语言环境(语言) [英] Programmatically change locale (language) of a page

查看:138
本文介绍了以编程方式更改页面的语言环境(语言)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Umbraco网站上有两个根节点..使用管理主机名..一个设置为英语,另一个设置为德语.

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 设置为 en 节点和 http://mywebsite.de 设置为 de 节点.

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

例如 http://mywebsite .de /english-page1.aspx应该使用英语语言环境.字典等需要从英语中加载
http://mywebsite .com /german-page1.aspx应该在德语语言环境中.字典等需要从德语加载

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

我写了一个HttpModule来更改PreRequestHandlerExecute上的语言环境,但没有成功

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");
}

推荐答案

我意识到这已经很老了,但是我在寻找答案时发现了它,并认为我会分享自己所做的事情.我正在使用Umbraco 7.5和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:

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添加到任何url中.这也会更改我显示的文本.我的每个文本字段都以简单的语言代码为前缀,例如. "fr_pageTitle"和"en_pageTitle".然后,我有了一种扩展方法,可以从MVC视图中提取正确的文本

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}");
    }

}

希望这对某人有帮助

这篇关于以编程方式更改页面的语言环境(语言)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆