Umbraco 按语言获取字典项,如何? [英] Umbraco get dictionary item by language, how?

查看:27
本文介绍了Umbraco 按语言获取字典项,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Umbraco v6 中,可以使用以下命令获取字典项:

In Umbraco v6 it's possible to get a dictionaryitem with the following command:

umbraco.library.GetDictionaryItem("EmailSubject");

这会根据用户访问 umbraco 网站的文化来检索EmailSubject"的正确值.

This retrieves the proper value of "EmailSubject" depending on which culture the user is visiting the umbraco website.

现在我正在编写一个简单的电子邮件类库,我不关心 System.Threading.Thread.CurrentThread.CurrentCulture 并且我不想在获取值之前一直设置 CurrentCulture.它有效,但我不喜欢这种方法.我正在编写一个简单的邮件库.对于每个邮件收件人,我认为这样设置文化并不是很有效.

Now I'm writing a simple email class library where I don't care about System.Threading.Thread.CurrentThread.CurrentCulture and I don't want to set the CurrentCulture all the time, before getting the value. It works, but I don't like the approach. I'm writing a simple mailing library. For each mail recipient I think it's not really efficient to set the culture like that.

我找到的解决方案(网上搜索,我丢失了源抱歉)如下示例:

The solution I found (searching online, I lost the source sorry) is the following example:

//2 = the 2nd language installed under Settings > Languages, which is German in my case
var sometext = new umbraco.cms.businesslogic.Dictionary.DictionaryItem("SomeText").Value(2);

我创建了一些帮助方法以使其更容易:

I created some helper method to make it easier:

private string GetDictionaryText(string dictionaryItem, string language)
{
    //try to retrieve from the cache
    string dictionaryText = (string)HttpContext.Current.Cache.Get(dictionaryItem + language);

    if (dictionaryText == null)
    {
        dictionaryText = new umbraco.cms.businesslogic.Dictionary.DictionaryItem(dictionaryItem).Value(GetLanguageId(language));
        //add to cache
        HttpContext.Current.Cache.Insert(dictionaryItem + language, dictionaryText, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero);
    }

    return dictionaryText;
}

private int GetLanguageId(string language)
{
    int languageId = 1; //1 = english, 2 = german, 3 = french, 4 = italian

    switch (language)
    {
        case "de":
            languageId = 2;
            break;  
        case "fr":
            languageId = 3;
            break;
        case "it":
            languageId = 4;
            break;
    }

    return languageId;
}

使用我的助手获取德语EmailSubject"的示例:

Example to get the "EmailSubject" in German, using my helpers:

string emailSubject = GetDictionaryText("EmailSubject", "de");

这有效(使用 umbraco 6.2.x 测试),但正如您所注意到的,每次您想要这样的文本时,都会有一个 umbraco.cms.businesslogic.Dictionary.DictionaryItem 类的新实例必须创建......这不一定是坏事,但我想知道是否有可用的静态方法,也许允许指定语言或文化(作为字符串)而不是语言或文化ID,它们可能因不同而不同环境...

This works (tested with umbraco 6.2.x) but as you could notice, every time you want a text like this, a new instance of the umbraco.cms.businesslogic.Dictionary.DictionaryItem class has to be created... which is not necessary bad but I was wondering if there was a static method available for this, maybe allowwing to specify the language or culture (as string) instead of the language or culture id which can differ in different environments...

由于 umbraco API 很大(有时一些很酷的功能没有记录在案)而且我找不到更好的解决方案,我想知道是否有更好的 umbraco本机"方式来实现这一点,而无需额外的帮助上面列出的方法.

Since the the umbraco API is huge (and sometimes some cool features are undocumented) and I could not find a better solution for this, I was wondering if there is a better umbraco "native" way to achieve this, without extra helper methods as I listed above.

在您的回答中,请列出您正在使用的 umbraco 版本.

In your answer please list the umbraco version you are using.

推荐答案

使用 LocalizationService 按语言获取字典项.我创建了一个执行此操作的静态方法:

Use the LocalizationService to get the dictionary item by language. I created a static method that does this:

public static string GetDictionaryValue(string key, CultureInfo culture, UmbracoContext context)
{
    var dictionaryItem = context.Application.Services.LocalizationService.GetDictionaryItemByKey(key);
    if (dictionaryItem != null)
    {
        var translation = dictionaryItem.Translations.SingleOrDefault(x => x.Language.CultureInfo.Equals(culture));
        if (translation != null)
            return translation.Value;
    }
    return key; // if not found, return key
}

这篇关于Umbraco 按语言获取字典项,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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