Umbraco get dictionary item by language, how?

firepol picture firepol · Mar 2, 2015 · Viewed 8.5k times · Source

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.

Answer

Alex picture Alex · Mar 24, 2016

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
}