Umbraco get dictionary item by language, how?
Asked Answered
H

2

7

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.

Hainan answered 2/3, 2015 at 13:53 Comment(0)
S
7

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
}
Spanking answered 24/3, 2016 at 14:33 Comment(0)
O
1

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.

Okoka answered 22/9, 2015 at 4:30 Comment(1)
So you get the whole list and load it into the cache? For each language? What umbraco function are referring to?Brachium

© 2022 - 2024 — McMap. All rights reserved.