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 97 posts 384 karma points c-trib
    1 week ago
    Mikael Axel Kleinwort
    0

    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:

    UmbracoHelper.GetDictionaryValue(...)
    

    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 (uSkinned.net) 375 posts 1059 karma points
    1 week ago
    Marc Love (uSkinned.net)
    0

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

    Cheers,

    Marc

  • Jason Elkin 35 posts 304 karma points c-trib
    1 week ago
    Jason Elkin
    101

    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 1471 posts 6483 karma points MVP 4x c-trib
    1 week ago
    Nik
    0

    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 (uSkinned.net) 375 posts 1059 karma points
    1 week ago
    Marc Love (uSkinned.net)
    1

    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.

    H5YR

  • Jason Elkin 35 posts 304 karma points c-trib
    1 week ago
    Jason Elkin
    0

    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 (uSkinned.net) 375 posts 1059 karma points
    1 week ago
    Marc Love (uSkinned.net)
    0

    Really interesting, thanks for sharing.

Please Sign in or register to post replies

Write your reply to:

Draft