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
Hi there,
How can I get the value of a DictionaryItem for a specific Culture?
DictionaryItem
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
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; }
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):
Services
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; }
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.
Thanks Patrick - this looks great and will come in handy on a future project, I'm sure. Bookmarked!
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.
Continue discussion
How to get Dictionary Value for specific Culture?
Hi there,
How can I get the value of a
DictionaryItem
for a specificCulture
?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
I had this Gist for V7 (I think). Haven't tested on V8, but maybe there are some pointers?
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):/Chriztian
This is what we use for V8:
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.
Thanks Patrick - this looks great and will come in handy on a future project, I'm sure. Bookmarked!
/Chriztian
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.