Umbraco get dictionary item by language, how?

我只是一个虾纸丫 提交于 2019-12-07 01:15:18

问题


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

umbraco.library.GetDictionaryItem("EmailSubject");

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

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

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

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

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...

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.

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


回答1:


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
}



回答2:


As far as I know, Umbraco does not have the static method to get dictionary item by a specific language at the moment. I must do same as you to get the dictionary item by language (I used Umbraco version 7.2.8). However, I get the language list by function that is provided by Umbraco.

I hope Umbraco will add this function in future versions. I think it is necessary as you told.



来源:https://stackoverflow.com/questions/28811485/umbraco-get-dictionary-item-by-language-how

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!