public void AddDictionaryTranslationsForAllLanguages(string subscriptionKey, string uriBase, IDictionaryItem dictionaryItem, ILanguage defaultLanguage, IEnumerable <ILanguage> allLanguages, string valueToTranslate) { var allLanguagesList = allLanguages.ToList(); _localizationService.AddOrUpdateDictionaryValue(dictionaryItem, allLanguagesList.FirstOrDefault(x => x.Id == defaultLanguage.Id), valueToTranslate); if (allLanguagesList.Any() && allLanguagesList.Count() > 1) { var otherLanguages = allLanguagesList.Where(x => x.Id != defaultLanguage.Id); foreach (var language in otherLanguages) { var result = _textService.MakeTranslationRequestAsync(valueToTranslate, subscriptionKey, uriBase, new[] { language.IsoCode }, defaultLanguage.IsoCode); JToken translatedValue = CommonHelpers.GetTranslatedValue(result); _localizationService.AddOrUpdateDictionaryValue(dictionaryItem, language, translatedValue.ToString()); } } }
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); }
/// <summary> /// Saves a dictionary item /// </summary> /// <param name="dictionary"> /// The dictionary. /// </param> /// <returns> /// The <see cref="DictionaryDisplay" />. /// </returns> public ActionResult <DictionaryDisplay?> PostSave(DictionarySave dictionary) { IDictionaryItem?dictionaryItem = dictionary.Id is null ? null : _localizationService.GetDictionaryItemById(int.Parse(dictionary.Id.ToString() !, CultureInfo.InvariantCulture)); if (dictionaryItem == null) { return(ValidationProblem("Dictionary item does not exist")); } CultureInfo?userCulture = _backofficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.GetUserCulture(_localizedTextService, _globalSettings); if (dictionary.NameIsDirty) { // if the name (key) has changed, we need to check if the new key does not exist IDictionaryItem?dictionaryByKey = _localizationService.GetDictionaryItemByKey(dictionary.Name !); if (dictionaryByKey != null && dictionaryItem.Id != dictionaryByKey.Id) { var message = _localizedTextService.Localize( "dictionaryItem", "changeKeyError", userCulture, new Dictionary <string, string?> { { "0", dictionary.Name } }); ModelState.AddModelError("Name", message); return(ValidationProblem(ModelState)); } dictionaryItem.ItemKey = dictionary.Name !; } foreach (DictionaryTranslationSave translation in dictionary.Translations) { _localizationService.AddOrUpdateDictionaryValue(dictionaryItem, _localizationService.GetLanguageById(translation.LanguageId), translation.Translation); } try { _localizationService.Save(dictionaryItem); DictionaryDisplay?model = _umbracoMapper.Map <IDictionaryItem, DictionaryDisplay>(dictionaryItem); model?.Notifications.Add(new BackOfficeNotification( _localizedTextService.Localize("speechBubbles", "dictionaryItemSaved", userCulture), string.Empty, NotificationStyle.Success)); return(model); } catch (Exception ex) { _logger.LogError(ex, "Error saving dictionary with {Name} under {ParentId}", dictionary.Name, dictionary.ParentId); return(ValidationProblem("Something went wrong saving dictionary")); } }
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 void CreateDictionaryItem(Type item) { var keyString = item.GetProperty("Key").Value <string>(); var value = item.GetProperty("Value").Value <string>(); var parentKey = item.GetProperty("ParentKey")?.Value <string>(); var dicItem = localizationService.GetDictionaryItemByKey(keyString) ?? new DictionaryItem(keyString); var lang = localizationService.GetLanguageByIsoCode(item.GetProperty("LanguageCode").Value <string>()); if (lang == null) { lang = new Language(item.GetProperty("LanguageCode").Value <string>()); localizationService.Save(lang); } List <IDictionaryTranslation> translations = new List <IDictionaryTranslation>(); foreach (var translation in item.GetProperty("Translations").Value <Dictionary <string, string> >() ?? new Dictionary <string, string>()) { var translationLanguage = localizationService.GetLanguageByIsoCode(translation.Key); if (translationLanguage == null) { translationLanguage = new Language(translation.Key); localizationService.Save(translationLanguage); } var trans = new DictionaryTranslation(translationLanguage, translation.Value); translations.Add(trans); } dicItem.Translations = translations; if (!string.IsNullOrEmpty(parentKey)) { var parentGuid = localizationService.GetDictionaryItemByKey(parentKey)?.GetUdi().Guid; if (parentGuid != null) { dicItem.ParentId = parentGuid; } } localizationService.AddOrUpdateDictionaryValue(dicItem, lang, value); localizationService.Save(dicItem); }
private IDictionaryItem UpdateDictionaryValues(XElement node, Guid?parent, List <ILanguage> languages) { var itemKeyNode = node.Attribute("Key"); if (itemKeyNode != null) { var itemKey = itemKeyNode.Value; LogHelper.Debug <DictionarySerializer>("Deserialize: < {0}", () => itemKey); IDictionaryItem item = default(IDictionaryItem); if (_localizationService.DictionaryItemExists(itemKey)) { // existing item = _localizationService.GetDictionaryItemByKey(itemKey); } else { if (parent.HasValue) { item = new DictionaryItem(parent.Value, itemKey); } else { item = new DictionaryItem(itemKey); } } foreach (var valueNode in node.Elements("Value")) { var languageId = valueNode.Attribute("LanguageCultureAlias").Value; var language = languages.FirstOrDefault(x => x.IsoCode == languageId); if (language != null) { _localizationService.AddOrUpdateDictionaryValue(item, language, valueNode.Value); } } _localizationService.Save(item); // children foreach (var child in node.Elements("DictionaryItem")) { UpdateDictionaryValues(child, item.Key, languages); } return(item); } return(null); }
private void UpdateLanguages(IDictionaryItem item, IDictionary <string, string> values) { var languages = LocalizationService .GetAllLanguages() .ToArray(); foreach (var value in values) { var language = languages.FirstOrDefault(x => x.CultureInfo.Name == value.Key); if (language == null) { continue; } LocalizationService.AddOrUpdateDictionaryValue(item, language, value.Value); } }
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)); }
private static void UpdateDictionaryItemCache(ILocalizationService ls, Umbraco.Core.Models.IDictionaryItem dict, Umbraco.Core.Models.ILanguage language, string defaultValue) { ls.AddOrUpdateDictionaryValue(dict, language, defaultValue); }
private void AddDictionaryItems() { try { var defLang = _localizationService.GetDefaultLanguageId(); ILanguage lang = _localizationService.GetLanguageById(defLang.Value); Current.Logger.Debug(System.Reflection.MethodBase.GetCurrentMethod().GetType(), "Executing AddDictionaryItems" + defLang); var newitem = _localizationService.GetDictionaryItemByKey("ForumAuthConstants.NewAccountMemberType") ?? new DictionaryItem("ForumAuthConstants.NewAccountMemberType"); _localizationService.AddOrUpdateDictionaryValue(newitem, lang, "Member"); _localizationService.Save(newitem); newitem = _localizationService.GetDictionaryItemByKey("ForumAuthConstants.ForgotPasswordView") ?? new DictionaryItem("ForumAuthConstants.ForgotPasswordView"); _localizationService.AddOrUpdateDictionaryValue(newitem, lang, "Member/ForumAuth.ForgotPassword"); _localizationService.Save(newitem); newitem = _localizationService.GetDictionaryItemByKey("ForumAuthConstants.LoginView") ?? new DictionaryItem("ForumAuthConstants.LoginView"); _localizationService.AddOrUpdateDictionaryValue(newitem, lang, "Member/ForumAuth.Login"); _localizationService.Save(newitem); newitem = _localizationService.GetDictionaryItemByKey("ForumAuthConstants.ResetPasswordView") ?? new DictionaryItem("ForumAuthConstants.ResetPasswordView"); _localizationService.AddOrUpdateDictionaryValue(newitem, lang, "Member/ForumAuth.ResetPassword"); _localizationService.Save(newitem); newitem = _localizationService.GetDictionaryItemByKey("ForumAuthConstants.RegisterView") ?? new DictionaryItem("ForumAuthConstants.RegisterView"); _localizationService.AddOrUpdateDictionaryValue(newitem, lang, "Member/ForumAuth.Register"); _localizationService.Save(newitem); newitem = _localizationService.GetDictionaryItemByKey("ForumAuthConstants.ProfileView") ?? new DictionaryItem("ForumAuthConstants.ProfileView"); _localizationService.AddOrUpdateDictionaryValue(newitem, lang, "Member/ForumAuth.ViewProfile"); _localizationService.Save(newitem); newitem = _localizationService.GetDictionaryItemByKey("ForumAuthConstants.ProfileEditView") ?? new DictionaryItem("ForumAuthConstants.ProfileEditView"); _localizationService.AddOrUpdateDictionaryValue(newitem, lang, "Member/ForumAuth.EditProfile"); _localizationService.Save(newitem); newitem = _localizationService.GetDictionaryItemByKey("ForumAuthConstants.LoginUrl") ?? new DictionaryItem("ForumAuthConstants.LoginUrl"); _localizationService.AddOrUpdateDictionaryValue(newitem, lang, "/login"); _localizationService.Save(newitem); newitem = _localizationService.GetDictionaryItemByKey("ForumAuthConstants.ResetUrl") ?? new DictionaryItem("ForumAuthConstants.ResetUrl"); _localizationService.AddOrUpdateDictionaryValue(newitem, lang, "/reset"); _localizationService.Save(newitem); newitem = _localizationService.GetDictionaryItemByKey("ForumAuthConstants.RegisterUrl") ?? new DictionaryItem("ForumAuthConstants.RegisterUrl"); _localizationService.AddOrUpdateDictionaryValue(newitem, lang, "/register"); _localizationService.Save(newitem); newitem = _localizationService.GetDictionaryItemByKey("ForumAuthConstants.VerifyUrl") ?? new DictionaryItem("ForumAuthConstants.VerifyUrl"); _localizationService.AddOrUpdateDictionaryValue(newitem, lang, "/verify"); _localizationService.Save(newitem); _localizationService.Save(lang); } catch (Exception e) { Current.Logger.Error(System.Reflection.MethodBase.GetCurrentMethod().GetType(), e, "Executing AddDictionaryItems"); } }