Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Jul 12, 2021 @ 07:11
    Chriztian Steinmeier
    0

    How to get Dictionary Value for specific Culture?

    Hi there,

    How can I get the value of a DictionaryItem for a specific Culture?

    There's a proposal by Bjarne to add an override here - but no takers (yet).

    I have a specific need for this in a template and I'd like to write my own helper/extension method but I have no idea how to approach it - any pointers much appreciated!

    (This is in an Umbraco 8.13.1 project)

    /Chriztian

  • Jannik Anker 48 posts 258 karma points c-trib
    Jul 12, 2021 @ 07:52
    Jannik Anker
    100

    I had this Gist for V7 (I think). Haven't tested on V8, but maybe there are some pointers?

    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 string.Empty;
    }
    
  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Jul 13, 2021 @ 10:27
    Chriztian Steinmeier
    1

    Hi Jannik,

    Thanks for this - I was able to work my way through it and get something that works in V8 (by sending in the Services object from the View):

        public static string GetDictionaryValueForLanguage(string key, CultureInfo culture, ServiceContext services) {
            var dictionaryItem = 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 string.Empty;
        }
    

    /Chriztian

  • Patrick de Mooij 72 posts 622 karma points MVP 3x c-trib
    Jul 12, 2021 @ 08:07
    Patrick de Mooij
    0

    This is what we use for V8:

    public static string GetAndCreateDictionaryValue(this ILocalizationService localizationService, string key, string defaultValue,
            string isoCode = null)
        {
            var languageCode = isoCode ?? System.Threading.Thread.CurrentThread.CurrentCulture.Name;
    
            var dictionaryItem = localizationService.GetDictionaryItemByKey(key) ?? key.Split('.').Aggregate((IDictionaryItem)null, (item, part) =>
            {
                var partKey = item is null ? part : $"{item.ItemKey}.{part}";
                return localizationService.GetDictionaryItemByKey(partKey) ?? localizationService.CreateDictionaryItemWithIdentity(partKey, item?.Key, partKey.Equals(key) ? defaultValue : string.Empty);
            });
            var currentValue = dictionaryItem.Translations?.FirstOrDefault(it => it.Language.IsoCode == languageCode);
            if (currentValue?.Value != null)
                return currentValue.Value;
            return dictionaryItem.Translations?.FirstOrDefault(it => it.Language.IsDefault)?.Value?.IfNullOrWhiteSpace(defaultValue) ?? defaultValue;
        }
    

    It is a bit more complex as it also automatically creates a dictionary item if it doesn't exists yet, but you should be able to use parts of it.

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Jul 13, 2021 @ 10:28
    Chriztian Steinmeier
    0

    Thanks Patrick - this looks great and will come in handy on a future project, I'm sure. Bookmarked!

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft