Make LocalizationResource.DefaultCultureName optional.

pull/279/head
Halil İbrahim Kalkan 7 years ago
parent efed02689e
commit d100b20258

@ -74,8 +74,7 @@ namespace Volo.Abp.Localization
var dictionaries = Resource.DictionaryProvider.Dictionaries; var dictionaries = Resource.DictionaryProvider.Dictionaries;
//Try to get from original dictionary (with country code) //Try to get from original dictionary (with country code)
ILocalizationDictionary originalDictionary; if (dictionaries.TryGetValue(cultureName, out var originalDictionary))
if (dictionaries.TryGetValue(cultureName, out originalDictionary))
{ {
var strOriginal = originalDictionary.GetOrNull(name); var strOriginal = originalDictionary.GetOrNull(name);
if (strOriginal != null) if (strOriginal != null)
@ -92,8 +91,7 @@ namespace Volo.Abp.Localization
//Try to get from same language dictionary (without country code) //Try to get from same language dictionary (without country code)
if (cultureName.Contains("-")) //Example: "tr-TR" (length=5) if (cultureName.Contains("-")) //Example: "tr-TR" (length=5)
{ {
ILocalizationDictionary langDictionary; if (dictionaries.TryGetValue(GetBaseCultureName(cultureName), out var langDictionary))
if (dictionaries.TryGetValue(GetBaseCultureName(cultureName), out langDictionary))
{ {
var strLang = langDictionary.GetOrNull(name); var strLang = langDictionary.GetOrNull(name);
if (strLang != null) if (strLang != null)
@ -104,19 +102,21 @@ namespace Volo.Abp.Localization
} }
//Try to get from default language //Try to get from default language
var defaultDictionary = dictionaries.GetOrDefault(Resource.DefaultCultureName); //TODO: What if not contains a default dictionary? if (!Resource.DefaultCultureName.IsNullOrEmpty())
if (defaultDictionary == null)
{
return null;
}
var strDefault = defaultDictionary.GetOrNull(name);
if (strDefault == null)
{ {
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<LocalizedString> GetAllStrings(string cultureName, bool includeParentCultures = true) protected virtual IReadOnlyList<LocalizedString> GetAllStrings(string cultureName, bool includeParentCultures = true)
@ -131,12 +131,15 @@ namespace Volo.Abp.Localization
if (includeParentCultures) if (includeParentCultures)
{ {
//Fill all strings from default dictionary //Fill all strings from default dictionary
var defaultDictionary = dictionaries.GetOrDefault(Resource.DefaultCultureName); if (!Resource.DefaultCultureName.IsNullOrEmpty())
if (defaultDictionary != null)
{ {
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);
}
} }
} }

@ -7,28 +7,29 @@ namespace Volo.Abp.Localization
{ {
public class LocalizationResource public class LocalizationResource
{ {
[NotNull]
public Type ResourceType { get; } public Type ResourceType { get; }
[CanBeNull]
public string DefaultCultureName { get; set; } public string DefaultCultureName { get; set; }
[NotNull]
public ILocalizationDictionaryProvider DictionaryProvider { get; } public ILocalizationDictionaryProvider DictionaryProvider { get; }
[NotNull]
public List<ILocalizationDictionaryProvider> Extensions { get; } public List<ILocalizationDictionaryProvider> Extensions { get; }
[NotNull]
public List<Type> BaseResourceTypes { get; } public List<Type> BaseResourceTypes { get; }
public LocalizationResource( public LocalizationResource(
[NotNull] Type resourceType, [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) [NotNull] ILocalizationDictionaryProvider dictionaryProvider)
{ {
Check.NotNull(resourceType, nameof(resourceType)); ResourceType = Check.NotNull(resourceType, nameof(resourceType));
Check.NotNull(defaultCultureName, nameof(defaultCultureName)); DictionaryProvider = Check.NotNull(dictionaryProvider, nameof(dictionaryProvider));
Check.NotNull(dictionaryProvider, nameof(dictionaryProvider));
ResourceType = resourceType;
DefaultCultureName = defaultCultureName; DefaultCultureName = defaultCultureName;
DictionaryProvider = dictionaryProvider;
BaseResourceTypes = new List<Type>(); BaseResourceTypes = new List<Type>();
Extensions = new List<ILocalizationDictionaryProvider>(); Extensions = new List<ILocalizationDictionaryProvider>();

@ -9,11 +9,10 @@ namespace Volo.Abp.Localization
{ {
public static LocalizationResource AddVirtualJson<TResource>( public static LocalizationResource AddVirtualJson<TResource>(
[NotNull] this LocalizationResourceDictionary resourceDictionary, [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) [NotNull] string virtualPath)
{ {
Check.NotNull(resourceDictionary, nameof(resourceDictionary)); Check.NotNull(resourceDictionary, nameof(resourceDictionary));
Check.NotNull(defaultCultureName, nameof(defaultCultureName));
Check.NotNull(virtualPath, nameof(virtualPath)); Check.NotNull(virtualPath, nameof(virtualPath));
virtualPath = virtualPath.EnsureStartsWith('/'); virtualPath = virtualPath.EnsureStartsWith('/');

Loading…
Cancel
Save