Copied to clipboard

Flag this post as spam?

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

  • Mikael Axel Kleinwort 139 posts 482 karma points c-trib
    Sep 14, 2021 @ 15:25
    Mikael Axel Kleinwort

    UmbracoHelper.GetDictionaryValue through Dependency Injection

    Hello friendly Umbraco's :-)

    I need to access dictionary items outside of a controller or view context, and I do not know how to do this in a good way, so how to I do this:


    with DI, when I have no UmbracoHelper object available?

    I read a lot of posts about DI with Umbraco and I am fairly familiar with it. I also read this post from Sebastiaan which shows a trick how to find out what I can ask for from the DI by using Vidual Studio's intellisense on Umbraco.Web.Composing.Current. However, the UmbracoHelper class which is displayed in Sebastiaan's screenshot is not listed in my Visual Studio with Umbraco 8.15.1.

    I know how to get to UmbracoContextby injecting IUmbracoContextFactory, but I don't know how to get to UmbracoHelper from there.

    The dirty shortcut through Umbraco.Web.Composing.Current.UmbracoHelper.GetDictionaryValue works, but I would like to know how to do it properly.

    Background: I am looking for a cached way of retrieving dictionary items. I am coding a simple service which I inject through composer/component with DI myself, into the controller. Inside of my service code, I need to access the dictionary items for later display in views. UmbracoHelper.GetDictionaryValuedoes exactly what I need when I use it directly in controller code (it gives my the dictionary item in the language which is displayed in the current request).

    Kind regards, Mikael

  • Marc Love ( 415 posts 1603 karma points
    Sep 14, 2021 @ 15:31
    Marc Love (

    Hi Mikael,

    Inject ILocalizationService

    You can then access dictionary items using:

    var currentCultureName = Thread.CurrentThread.CurrentUICulture.Name;
    var language = _localizationService.GetLanguageByIsoCode(currentCultureName);
    if (language != null)
       var dictionaryItem= _localizationService.GetDictionaryItemByKey("YOUR DICTIONARY ITEM NAME");
       var dictionaryTranslation= dictionaryItem!= null ? titleDictionaryItem.Translations.FirstOrDefault(x => x.LanguageId == language.Id) : null;



  • Jason Elkin 38 posts 350 karma points MVP 2x c-trib
    Sep 14, 2021 @ 16:48
    Jason Elkin

    Ahoy Marc,

    It's worth flagging that this isn't cached so will hit the database.

    It's possible to inject the ICultureDictionaryFactory which will behave a bit more like the UmbracoHelper.

    var dictionary = _dictionaryFactory.CreateDictionary();
    var translation = dictionary["key"];
  • Nik 1567 posts 7015 karma points MVP 6x c-trib
    Sep 14, 2021 @ 21:08

    I can confirm that Jason's approach is the better one rather than injecting the localisation service. I have to do something very similar to create localisable attributes for model validation. :-) (although I can't use DI with attributes annoyingly).

  • Marc Love ( 415 posts 1603 karma points
    Sep 15, 2021 @ 09:19
    Marc Love (

    Amazing!!! Thanks Jason. I really struggled to find any examples of this in the past. Thanks for the alternative approach. Just tested and works as expected.


  • Jason Elkin 38 posts 350 karma points MVP 2x c-trib
    Sep 15, 2021 @ 12:40
    Jason Elkin

    You're most welcome, I've been hacking away at the dictionary on a few projects recently and learnt a few things.

    It's also trivially easy to create your own ICultureDictionaryFactory or even ICultureDictionary to handle more complex scenarios nicely.

    e.g. the below, for when you need to define the culture explicitly.

    public interface IBetterCultureDictionaryFactory : ICultureDictionaryFactory 
        ICultureDictionary CreateDictionary(CultureInfo culture);
    public class BetterCultureDictionaryFactory : IBetterCultureDictionaryFactory
        public ICultureDictionary CreateDictionary() => new DefaultCultureDictionary();
        public ICultureDictionary CreateDictionary(CultureInfo culture) => new DefaultCultureDictionary(culture);
  • Marc Love ( 415 posts 1603 karma points
    Sep 17, 2021 @ 11:14
    Marc Love (

    Really interesting, thanks for sharing.

Please Sign in or register to post replies

Write your reply to: