For my forum package I created an extension method for the localizationservice
public static string GetOrCreateDictionaryValue(this ILocalizationService localizationService, string key, string defaultValue,string isoCode = null)
{
var languageCode = isoCode ?? System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
if (languageCode.StartsWith("en_"))
{
languageCode = "en";
}
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 (!string.IsNullOrWhiteSpace(currentValue?.Value))
return currentValue.Value;
return $"[{key}]";
}