Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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
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;
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"];
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).
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.
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);
Really interesting, thanks for sharing.
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted