From d100b202582c64709f90e8df26512ce8dfde54b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 1 May 2018 14:46:56 +0300 Subject: [PATCH] Make LocalizationResource.DefaultCultureName optional. --- .../AbpDictionaryBasedStringLocalizer.cs | 39 ++++++++++--------- .../Abp/Localization/LocalizationResource.cs | 15 +++---- .../LocalizationResourceListExtensions.cs | 3 +- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/Volo.Abp.Localization/Volo/Abp/Localization/AbpDictionaryBasedStringLocalizer.cs b/src/Volo.Abp.Localization/Volo/Abp/Localization/AbpDictionaryBasedStringLocalizer.cs index fc992fe93f..46a626b036 100644 --- a/src/Volo.Abp.Localization/Volo/Abp/Localization/AbpDictionaryBasedStringLocalizer.cs +++ b/src/Volo.Abp.Localization/Volo/Abp/Localization/AbpDictionaryBasedStringLocalizer.cs @@ -74,8 +74,7 @@ namespace Volo.Abp.Localization var dictionaries = Resource.DictionaryProvider.Dictionaries; //Try to get from original dictionary (with country code) - ILocalizationDictionary originalDictionary; - if (dictionaries.TryGetValue(cultureName, out originalDictionary)) + if (dictionaries.TryGetValue(cultureName, out var originalDictionary)) { var strOriginal = originalDictionary.GetOrNull(name); if (strOriginal != null) @@ -92,8 +91,7 @@ namespace Volo.Abp.Localization //Try to get from same language dictionary (without country code) if (cultureName.Contains("-")) //Example: "tr-TR" (length=5) { - ILocalizationDictionary langDictionary; - if (dictionaries.TryGetValue(GetBaseCultureName(cultureName), out langDictionary)) + if (dictionaries.TryGetValue(GetBaseCultureName(cultureName), out var langDictionary)) { var strLang = langDictionary.GetOrNull(name); if (strLang != null) @@ -104,19 +102,21 @@ namespace Volo.Abp.Localization } //Try to get from default language - var defaultDictionary = dictionaries.GetOrDefault(Resource.DefaultCultureName); //TODO: What if not contains a default dictionary? - if (defaultDictionary == null) - { - return null; - } - - var strDefault = defaultDictionary.GetOrNull(name); - if (strDefault == null) + if (!Resource.DefaultCultureName.IsNullOrEmpty()) { - return null; + var defaultDictionary = dictionaries.GetOrDefault(Resource.DefaultCultureName); + if (defaultDictionary != null) + { + var strDefault = defaultDictionary.GetOrNull(name); + if (strDefault != null) + { + return new LocalizedString(name, strDefault.Value); + } + } } - return new LocalizedString(name, strDefault.Value); + //Not found + return null; } protected virtual IReadOnlyList GetAllStrings(string cultureName, bool includeParentCultures = true) @@ -131,12 +131,15 @@ namespace Volo.Abp.Localization if (includeParentCultures) { //Fill all strings from default dictionary - var defaultDictionary = dictionaries.GetOrDefault(Resource.DefaultCultureName); - if (defaultDictionary != null) + if (!Resource.DefaultCultureName.IsNullOrEmpty()) { - foreach (var defaultDictString in defaultDictionary.GetAllStrings()) + var defaultDictionary = dictionaries.GetOrDefault(Resource.DefaultCultureName); + if (defaultDictionary != null) { - allStrings[defaultDictString.Name] = new LocalizedString(defaultDictString.Name, defaultDictString.Value); + foreach (var defaultDictString in defaultDictionary.GetAllStrings()) + { + allStrings[defaultDictString.Name] = new LocalizedString(defaultDictString.Name, defaultDictString.Value); + } } } diff --git a/src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizationResource.cs b/src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizationResource.cs index 547035fbaf..d79c3c48ff 100644 --- a/src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizationResource.cs +++ b/src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizationResource.cs @@ -7,28 +7,29 @@ namespace Volo.Abp.Localization { public class LocalizationResource { + [NotNull] public Type ResourceType { get; } + [CanBeNull] public string DefaultCultureName { get; set; } + [NotNull] public ILocalizationDictionaryProvider DictionaryProvider { get; } + [NotNull] public List Extensions { get; } + [NotNull] public List BaseResourceTypes { get; } public LocalizationResource( [NotNull] Type resourceType, - [NotNull] string defaultCultureName, + [CanBeNull] string defaultCultureName, //TODO: defaultCultureName should be optional (and second argument) because it's not required for the LocalizationResource! [NotNull] ILocalizationDictionaryProvider dictionaryProvider) { - Check.NotNull(resourceType, nameof(resourceType)); - Check.NotNull(defaultCultureName, nameof(defaultCultureName)); - Check.NotNull(dictionaryProvider, nameof(dictionaryProvider)); - - ResourceType = resourceType; + ResourceType = Check.NotNull(resourceType, nameof(resourceType)); + DictionaryProvider = Check.NotNull(dictionaryProvider, nameof(dictionaryProvider)); DefaultCultureName = defaultCultureName; - DictionaryProvider = dictionaryProvider; BaseResourceTypes = new List(); Extensions = new List(); diff --git a/src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizationResourceListExtensions.cs b/src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizationResourceListExtensions.cs index d9c98d39d5..1c51f0592a 100644 --- a/src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizationResourceListExtensions.cs +++ b/src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizationResourceListExtensions.cs @@ -9,11 +9,10 @@ namespace Volo.Abp.Localization { public static LocalizationResource AddVirtualJson( [NotNull] this LocalizationResourceDictionary resourceDictionary, - [NotNull] string defaultCultureName, + [CanBeNull] string defaultCultureName, //TODO: defaultCultureName should be optional (and second argument) because it's not required for the LocalizationResource! [NotNull] string virtualPath) { Check.NotNull(resourceDictionary, nameof(resourceDictionary)); - Check.NotNull(defaultCultureName, nameof(defaultCultureName)); Check.NotNull(virtualPath, nameof(virtualPath)); virtualPath = virtualPath.EnsureStartsWith('/');