mirror of https://github.com/abpframework/abp.git
6 changed files with 112 additions and 73 deletions
@ -0,0 +1,64 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Collections.Immutable; |
||||
|
using System.Linq; |
||||
|
|
||||
|
namespace Volo.Abp.Localization |
||||
|
{ |
||||
|
public class CombinedLocalizationDictionary : ILocalizationDictionary |
||||
|
{ |
||||
|
public string CultureName { get; } |
||||
|
|
||||
|
private readonly List<ILocalizationDictionary> _dictionaries; |
||||
|
|
||||
|
public CombinedLocalizationDictionary(params ILocalizationDictionary[] dictionaries) |
||||
|
{ |
||||
|
Check.NotNullOrEmpty(dictionaries, nameof(dictionaries)); |
||||
|
_dictionaries = dictionaries.ToList(); |
||||
|
CultureName = dictionaries.First().CultureName; |
||||
|
|
||||
|
if (dictionaries.Any(d => d.CultureName != CultureName)) |
||||
|
{ |
||||
|
throw new AbpException($"All given dictionaries should have the same {nameof(CultureName)}"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public LocalString GetOrNull(string name) |
||||
|
{ |
||||
|
foreach (var dictionary in _dictionaries) |
||||
|
{ |
||||
|
var value = dictionary.GetOrNull(name); |
||||
|
if (value != null) |
||||
|
{ |
||||
|
return value; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
public IReadOnlyList<LocalString> GetAllStrings() |
||||
|
{ |
||||
|
var localStrings = new Dictionary<string, LocalString>(); |
||||
|
|
||||
|
foreach (var dictionary in _dictionaries) |
||||
|
{ |
||||
|
foreach (var localString in dictionary.GetAllStrings()) |
||||
|
{ |
||||
|
localStrings[localString.Name] = localString; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return localStrings.Values.ToImmutableList(); |
||||
|
} |
||||
|
|
||||
|
public void AddFirst(ILocalizationDictionary dictionary) |
||||
|
{ |
||||
|
_dictionaries.AddFirst(dictionary); |
||||
|
} |
||||
|
|
||||
|
public void AddLast(ILocalizationDictionary dictionary) |
||||
|
{ |
||||
|
_dictionaries.AddLast(dictionary); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,64 +0,0 @@ |
|||||
using System.Collections; |
|
||||
using System.Collections.Generic; |
|
||||
using System.Collections.Immutable; |
|
||||
|
|
||||
namespace Volo.Abp.Localization |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Represents a simple implementation of <see cref="ILocalizationDictionary"/> interface.
|
|
||||
/// </summary>
|
|
||||
public class LocalizationDictionary : ILocalizationDictionary, IEnumerable<LocalString> |
|
||||
{ |
|
||||
/// <inheritdoc/>
|
|
||||
public string CultureName { get; } |
|
||||
|
|
||||
private readonly Dictionary<string, LocalString> _dictionary; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Creates a new <see cref="LocalizationDictionary"/> object.
|
|
||||
/// </summary>
|
|
||||
/// <param name="cultureName">Culture of the dictionary</param>
|
|
||||
/// <param name="dictionary">The dictionary</param>
|
|
||||
public LocalizationDictionary(string cultureName, Dictionary<string, LocalString> dictionary) |
|
||||
{ |
|
||||
CultureName = cultureName; |
|
||||
_dictionary = dictionary; |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public virtual LocalString GetOrNull(string name) |
|
||||
{ |
|
||||
return _dictionary.GetOrDefault(name); |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public virtual IReadOnlyList<LocalString> GetAllStrings() |
|
||||
{ |
|
||||
return _dictionary.Values.ToImmutableList(); |
|
||||
} |
|
||||
|
|
||||
public void Extend(ILocalizationDictionary dictionary) |
|
||||
{ |
|
||||
foreach (var localizedString in dictionary.GetAllStrings()) |
|
||||
{ |
|
||||
_dictionary[localizedString.Name] = localizedString; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public virtual IEnumerator<LocalString> GetEnumerator() |
|
||||
{ |
|
||||
return GetAllStrings().GetEnumerator(); |
|
||||
} |
|
||||
|
|
||||
IEnumerator IEnumerable.GetEnumerator() |
|
||||
{ |
|
||||
return GetAllStrings().GetEnumerator(); |
|
||||
} |
|
||||
|
|
||||
protected bool Contains(string name) |
|
||||
{ |
|
||||
return _dictionary.ContainsKey(name); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,40 @@ |
|||||
|
using System.Collections; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Collections.Immutable; |
||||
|
|
||||
|
namespace Volo.Abp.Localization |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Represents a simple implementation of <see cref="ILocalizationDictionary"/> interface.
|
||||
|
/// </summary>
|
||||
|
public class StaticLocalizationDictionary : ILocalizationDictionary |
||||
|
{ |
||||
|
/// <inheritdoc/>
|
||||
|
public string CultureName { get; } |
||||
|
|
||||
|
protected Dictionary<string, LocalString> Dictionary { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Creates a new <see cref="StaticLocalizationDictionary"/> object.
|
||||
|
/// </summary>
|
||||
|
/// <param name="cultureName">Culture of the dictionary</param>
|
||||
|
/// <param name="dictionary">The dictionary</param>
|
||||
|
public StaticLocalizationDictionary(string cultureName, Dictionary<string, LocalString> dictionary) |
||||
|
{ |
||||
|
CultureName = cultureName; |
||||
|
Dictionary = dictionary; |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public virtual LocalString GetOrNull(string name) |
||||
|
{ |
||||
|
return Dictionary.GetOrDefault(name); |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public virtual IReadOnlyList<LocalString> GetAllStrings() |
||||
|
{ |
||||
|
return Dictionary.Values.ToImmutableList(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue