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;
//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<LocalizedString> 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);
}
}
}

@ -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<ILocalizationDictionaryProvider> Extensions { get; }
[NotNull]
public List<Type> 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<Type>();
Extensions = new List<ILocalizationDictionaryProvider>();

@ -9,11 +9,10 @@ namespace Volo.Abp.Localization
{
public static LocalizationResource AddVirtualJson<TResource>(
[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('/');

Loading…
Cancel
Save