public override void Up() { ILocalizationService localizationService = ApplicationContext.Current.Services.LocalizationService; IEnumerable <ILanguage> languages = localizationService.GetAllLanguages(); ILanguage language = languages.FirstOrDefault(x => x.IsoCode == "en-GB" || x.IsoCode == "en-US") ?? languages.FirstOrDefault(); if (language == null) { return; } IDictionaryItem dataAnnotations = localizationService.GetDictionaryItemByKey("DataAnnotions"); if (dataAnnotations != null) { return; } dataAnnotations = localizationService.CreateDictionaryItemWithIdentity("DataAnnotations", null); foreach (var item in defaultDictionaryItems) { if (localizationService.DictionaryItemExists(item.Key)) { continue; } localizationService.CreateDictionaryItemWithIdentity(item.Key, dataAnnotations.Key, item.Value); } }
protected DictionaryModel Create(string itemKey, DictionaryModel parent, Func <CultureInfo, string> valueResolver) { DictionaryModelWrapper parentModel = parent as DictionaryModelWrapper; // Get the dictionary item from Umbraco. Otherwise crete a new. IDictionaryItem dictionaryItem = _localizationService.GetDictionaryItemByKey(itemKey) ?? _localizationService.CreateDictionaryItemWithIdentity(itemKey, parentModel?.Key); // Create the model DictionaryModelWrapper model = new DictionaryModelWrapper(dictionaryItem, parentModel); // If no value resolver are provided, then return the model if (valueResolver == null) { return(model); } // A save indicator bool save = false; // Iterate through all languages foreach (ILanguage language in Languages) { IDictionaryTranslation translation = dictionaryItem.Translations.SingleOrDefault(x => x.Language == language); // If the dictionary translation already has a value, then don't change it if (!string.IsNullOrEmpty(translation?.Value)) { continue; } // Get the culture specific default value from the value resolver string value = valueResolver(language.CultureInfo); // If there is no value, then do nothing if (value == null) { continue; } // Add the value to the Umbraco dictionary item _localizationService.AddOrUpdateDictionaryValue(dictionaryItem, language, value); // Indicate that a value has been added to the dictionary, and ensure save save = true; // Log the addition Log.Information( $"Adding '{language}' value:'{value}' for dictionary item: '{dictionaryItem.ItemKey}'"); } // Save the dictionary item if (save) { _localizationService.Save(dictionaryItem); } // Return the model return(model); }
private IDictionaryItem AddOrUpdateDictionaryItem(ILanguage language, string key, string value = null, Guid?parentId = null) { var item = _localizationService.GetDictionaryItemByKey(key); if (item == null) { return(_localizationService.CreateDictionaryItemWithIdentity(key, parentId, value)); } _localizationService.AddOrUpdateDictionaryValue(item, language, value); return(item); }
public ActionResult <int> Create(int parentId, string key) { if (string.IsNullOrEmpty(key)) { return(ValidationProblem("Key can not be empty.")); // TODO: translate } if (_localizationService.DictionaryItemExists(key)) { var message = _localizedTextService.Localize( "dictionaryItem", "changeKeyError", _backofficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.GetUserCulture(_localizedTextService, _globalSettings), new Dictionary <string, string?> { { "0", key } }); return(ValidationProblem(message)); } try { Guid?parentGuid = null; if (parentId > 0) { parentGuid = _localizationService.GetDictionaryItemById(parentId)?.Key; } IDictionaryItem item = _localizationService.CreateDictionaryItemWithIdentity( key, parentGuid, string.Empty); return(item.Id); } catch (Exception ex) { _logger.LogError(ex, "Error creating dictionary with {Name} under {ParentId}", key, parentId); return(ValidationProblem("Error creating dictionary item")); } }
private static string AddDictionaryItemIfNotExist(string uniqueKey, string key, string defaultText) { var comboKey = string.Format("{0}.{1}", uniqueKey, key); defaultText = !string.IsNullOrWhiteSpace(defaultText) ? defaultText : string.Format("[{0}]", key); if (!Service.DictionaryItemExists(uniqueKey)) { var newItem = new DictionaryItem(uniqueKey); Service.Save(newItem); } var guidKey = Service.GetDictionaryItemByKey(uniqueKey).Key; var dictionary = Service.CreateDictionaryItemWithIdentity(comboKey, guidKey, defaultText); var dictionaryId = dictionary.Id; var dictionaryItem = Service.GetDictionaryItemById(dictionaryId); foreach (var item in dictionaryItem.Translations) { var value = item.Language.IsoCode.Substring(0, 2).ToLowerInvariant() == "en" ? defaultText : string.Format("[{0}]", key); Service.AddOrUpdateDictionaryValue(dictionaryItem, item.Language, value); } Service.Save(dictionaryItem); return(UmbracoHelper.GetDictionaryValue(comboKey)); }