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);
            }
        }
        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);
        }
Beispiel #3
0
    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"));
        }
    }
Beispiel #4
0
        public JsonResult UpdateDictionaries()
        {
            string nspace          = "Umbraco.Plugins.Connector.Dictionaries";
            var    idictionary     = typeof(Umbraco.Plugins.Connector.Interfaces.IDictionaryItem);
            var    dictionaryTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                                     where t.IsClass && t.Namespace == nspace && idictionary.IsAssignableFrom(t)
                                     select t;

            var updatedDictionaries = new List <string>();

            foreach (var dictionaryType in dictionaryTypes)
            {
                var keyString = dictionaryType.GetProperty("Key").Value <string>();
                if (!_localizationService.DictionaryItemExists(keyString))
                {
                    _languageDictionaryService.CreateDictionaryItem(dictionaryType);
                    updatedDictionaries.Add(keyString);
                }
            }

            return(Json(updatedDictionaries));
        }
Beispiel #5
0
        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));
        }