private void AddLanguages()
 {
     var norwegian = new Core.Models.Language("nb-NO");
     var english = new Core.Models.Language("en-GB");
     ServiceContext.LocalizationService.Save(norwegian, 0);
     ServiceContext.LocalizationService.Save(english, 0);
 }
Exemplo n.º 2
0
        public Dictionary <string, bool> CreateDictionaryItems(List <ImportExportDictionaryItem> items)
        {
            Dictionary <string, bool> results = new Dictionary <string, bool>();

            foreach (var item in items)
            {
                var keyString = item.Key;
                var value     = item.Value;
                var parentKey = item.ParentKey;
                var dicItem   = localizationService.GetDictionaryItemByKey(keyString) ?? new DictionaryItem(keyString);

                var lang = localizationService.GetLanguageByIsoCode(item.LanguageCode);
                if (lang == null)
                {
                    lang = new Language(item.LanguageCode);
                    localizationService.Save(lang);
                }

                List <IDictionaryTranslation> translations = new List <IDictionaryTranslation>();
                foreach (var translation in item.Translations ?? 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;
                    }
                }

                try
                {
                    localizationService.AddOrUpdateDictionaryValue(dicItem, lang, value);
                    localizationService.Save(dicItem);

                    results.Add(dicItem.ItemKey, true);
                }
                catch
                {
                    results.Add(dicItem.ItemKey, false);
                }
            }
            return(results);
        }
        public void Save_Language_And_GetLanguageById()
        {
            var localizationService = ServiceContext.LocalizationService;
            var isoCode             = "en-AU";
            var language            = new Core.Models.Language(isoCode);

            // Act
            localizationService.Save(language);
            var result = localizationService.GetLanguageById(language.Id);

            // Assert
            Assert.NotNull(result);
        }
        public void Deleted_Language_Should_Not_Exist()
        {
            var localizationService = ServiceContext.LocalizationService;
            var isoCode             = "en-AU";
            var language            = new Core.Models.Language(isoCode);

            localizationService.Save(language);

            // Act
            localizationService.Delete(language);
            var result = localizationService.GetLanguageByIsoCode(isoCode);

            // Assert
            Assert.Null(result);
        }
Exemplo n.º 5
0
        public void CreateDictionary(Tenant tenant)
        {
            var lang = localizationService.GetLanguageByIsoCode(tenant.Languages.Default);

            if (string.IsNullOrEmpty(tenant.Languages.Default))
            {
                throw new TenantException(ExceptionCode.DefaultLanguageIsMandatory.CodeToString(), ExceptionCode.DefaultLanguageIsMandatory, tenant.TenantUId);
            }
            else
            {
                try
                {
                    if (lang == null)
                    {
                        lang = new Language(tenant.Languages.Default)
                        {
                            IsDefault   = true,
                            IsMandatory = true
                        };
                        localizationService.Save(lang);
                    }

                    foreach (var language in tenant.Languages.Alternate ?? new List <string>())
                    {
                        var translationLanguage = localizationService.GetLanguageByIsoCode(language);
                        if (translationLanguage == null)
                        {
                            translationLanguage = new Language(language)
                            {
                                FallbackLanguageId = lang.Id,
                                IsMandatory        = false
                            };
                            localizationService.Save(translationLanguage);
                        }
                    }
                }
                catch (Exception ex)
                {
                    logger.Error(typeof(LanguageDictionaryService), ex.Message);
                    logger.Error(typeof(LanguageDictionaryService), ex.StackTrace);
                    throw;
                }
            }
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        public void Set_Default_Language()
        {
            var localizationService = ServiceContext.LocalizationService;
            var language            = new Core.Models.Language("en-AU");

            language.IsDefault = true;
            localizationService.Save(language);
            var result = localizationService.GetLanguageById(language.Id);

            Assert.IsTrue(result.IsDefault);

            var language2 = new Core.Models.Language("en-NZ");

            language2.IsDefault = true;
            localizationService.Save(language2);
            var result2 = localizationService.GetLanguageById(language2.Id);

            //re-get
            result = localizationService.GetLanguageById(language.Id);

            Assert.IsTrue(result2.IsDefault);
            Assert.IsFalse(result.IsDefault);
        }
Exemplo n.º 8
0
        public (IDictionaryItem, bool) CreateDictionaryItem(ImportExportDictionaryItem item)
        {
            var dicItem = localizationService.GetDictionaryItemByKey(item.Key) ?? new DictionaryItem(item.Key);

            var lang = localizationService.GetLanguageByIsoCode(item.LanguageCode);

            if (lang == null)
            {
                lang = new Language(item.LanguageCode);
                localizationService.Save(lang);
            }

            List <IDictionaryTranslation> translations = new List <IDictionaryTranslation>();
            var mainLanguage    = localizationService.GetLanguageByIsoCode(item.LanguageCode);
            var mainTranslation = dicItem.Translations.FirstOrDefault(x => x.Language.Equals(mainLanguage));

            if (mainTranslation != null)
            {
                if (!string.IsNullOrEmpty(item.Value))
                {
                    mainTranslation.Value = item.Value;
                    translations.Add(mainTranslation);
                }
            }
            else
            {
                translations.Add(new DictionaryTranslation(mainLanguage, item.Value ?? string.Empty));
            }


            foreach (var translation in item.Translations ?? new Dictionary <string, string>())
            {
                var translationLanguage = localizationService.GetLanguageByIsoCode(translation.Key);
                if (translationLanguage == null)
                {
                    translationLanguage = new Language(translation.Key);
                    localizationService.Save(translationLanguage);
                }

                var t = dicItem.Translations.FirstOrDefault(x => x.Language.IsoCode.Equals(translation.Key));
                if (t != null)
                {
                    t.Value = translation.Value ?? string.Empty;
                    translations.Add(t);
                }
                else
                {
                    translations.Add(new DictionaryTranslation(translationLanguage, translation.Value ?? string.Empty));
                }
            }

            dicItem.Translations = translations;

            if (!string.IsNullOrEmpty(item.ParentKey))
            {
                var parentGuid = localizationService.GetDictionaryItemByKey(item.ParentKey)?.GetUdi().Guid;
                if (parentGuid != null && !dicItem.ParentId.Equals(parentGuid))
                {
                    dicItem.ParentId = parentGuid;
                }
            }

            try
            {
                localizationService.Save(dicItem);
                return(dicItem, true);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return(dicItem, false);
            }
        }