上一篇博文地址:[Asp.net 5] Localization-簡單易用的本地化-全球化信息
本文繼續介紹asp.net 5多語言。今天重點講的是多語言的resx文件。涉及到的工程有:Microsoft.Framework.Localization.Abstractions以及Microsoft.Framework.Localization。他們之間的類結構如下如所示:

Microsoft.Framework.Localization.Abstractions
該工程定義了本地化(全球化)的基本接口,以及一些抽象類。

public struct LocalizedString { public LocalizedString([NotNull] string name, [NotNull] string value) : this(name, value, resourceNotFound: false) { } public LocalizedString([NotNull] string name, [NotNull] string value, bool resourceNotFound) { Name = name; Value = value; ResourceNotFound = resourceNotFound; } public static implicit Operator string (LocalizedString localizedString) { return localizedString.Value; } public string Name { get; } public string Value { get; } public bool ResourceNotFound { get; } public override string ToString() => Value; }LocalizedString
public interface IStringLocalizer : IEnumerable<LocalizedString> { LocalizedString this[string name] { get; } LocalizedString this[string name, params object[] arguments] { get; } IStringLocalizer WithCulture(CultureInfo culture); }IStringLocalizer 
public interface IStringLocalizer<T> : IStringLocalizer { }public class StringLocalizer<TResourceSource> : IStringLocalizer<TResourceSource> { PRivate IStringLocalizer _localizer; public StringLocalizer([NotNull] IStringLocalizerFactory factory) { _localizer = factory.Create(typeof(TResourceSource)); } public virtual IStringLocalizer WithCulture(CultureInfo culture) => _localizer.WithCulture(culture); public virtual LocalizedString this[[NotNull] string name] => _localizer[name]; public virtual LocalizedString this[[NotNull] string name, params object[] arguments] => _localizer[name, arguments]; public virtual LocalizedString GetString([NotNull] string name) => _localizer.GetString(name); public virtual LocalizedString GetString([NotNull] string name, params object[] arguments) => _localizer.GetString(name, arguments); public IEnumerator<LocalizedString> GetEnumerator() => _localizer.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => _localizer.GetEnumerator(); }StringLocalizer根據上面的介紹,我們可以推斷該類庫的使用方式如下:
public void Test(){ IStringLocalizerFactory factory=new ResourceManagerStringLocalizerFactory(); IStringLocalizer localizer=new StringLocalizer<TType>(factory); //IStringLocalizer<TType> localizer=new StringLocalizer<TType>(); //localizer=localizer.WithCulture(CultureInfo.CurrentUICulture); var localizedString=localizer.GetString(key);}[StringLocalizer<TResource>的作用就是隱藏具體IStringLocalizer,使我們不用關心是哪個IStringLocalizer,這樣做對于直接書寫代碼可能看不出直接意義,但是如果使用依賴注入,則可以明顯的看出好處,我們可以為IStringLocalizer<TType>直接注入StringLocalizer<TType>類]
[如果對于依賴注入了解不多,可以看下篇博客:DependencyInjection項目代碼分析-目錄,略長]
Microsoft.Framework.Localization

public class ResourceManagerStringLocalizerFactory : IStringLocalizerFactory { private readonly IapplicationEnvironment _applicationEnvironment; public ResourceManagerStringLocalizerFactory([NotNull] IApplicationEnvironment applicationEnvironment) { _applicationEnvironment = applicationEnvironment; } public IStringLocalizer Create([NotNull] Type resourceSource) { var typeInfo = resourceSource.GetTypeInfo(); var assembly = new AssemblyWrapper(typeInfo.Assembly); var baseName = typeInfo.FullName; return new ResourceManagerStringLocalizer(new ResourceManager(resourceSource), assembly, baseName); } public IStringLocalizer Create([NotNull] string baseName, [NotNull] string location) { var assembly = Assembly.Load(new AssemblyName(location ?? _applicationEnvironment.ApplicationName)); return new ResourceManagerStringLocalizer( new ResourceManager(baseName, assembly), new AssemblyWrapper(assembly), baseName); } }ResourceManagerStringLocalizerFactory 
public class ResourceManagerStringLocalizer : IStringLocalizer { private static readonly ConcurrentDictionary<string, IList<string>> _resourceNamesCache = new ConcurrentDictionary<string, IList<string>>(); private readonly ConcurrentDictionary<string, object> _
新聞熱點
疑難解答