本篇比较简单介绍Localization解决方案中:
CultureInfoCache工程的作用是和它的名字一样,缓冲各个地区的CultureInfo。该工程中只有一个类(分成俩个部分类),而且都超级简单,下面我们就简单介绍下把:
CultureInfoCache:核心的缓冲类。对外暴漏静态方法:CultureInfo GetCultureInfo(string name),能够根据各地的语言代码(比如 cn、en等)获取到各地的CultureInfo。
public static partial class CultureInfoCache { private static readonly ConcurrentDictionary<string, CacheEntry> _cache = new ConcurrentDictionary<string, CacheEntry>(); public static CultureInfo GetCultureInfo(string name) { if (name == null || !KnownCultureNames.Contains(name)) { return null; } var entry = _cache.GetOrAdd(name, n => { try { return new CacheEntry(CultureInfo.ReadOnly(new CultureInfo(n))); } catch (CultureNotFoundException) { return new CacheEntry(cultureInfo: null); } }); return entry.CultureInfo; } private class CacheEntry { public CacheEntry(CultureInfo cultureInfo) { CultureInfo = cultureInfo; } public CultureInfo CultureInfo { get; } } } CultureInfoCacheCultureInfoCache Part1
public static partial class CultureInfoCache { public static readonly HashSet<string> KnownCultureNames = new HashSet<string> { #region culture "ar", ......... "zh-CHT" #endregion }; }CultureInfoCache Part2
这个工程的作用就是生成Microsoft.Framework.Globalization.CultureInfoCache的Part2,说实话我觉得这个工程这么架构是有问题的。难道每次都重新生成代码吗?不过现有源码是这样的,我只是按照现有逻辑分享给大家。
说有问题,这个解决方案还真有问题,下面俩个问题需要手工修改下:
public Program(IApplicationEnvironment appEnvironment) { _appName = appEnvironment == null ? "CultureInfoGenerator" : appEnvironment.ApplicationName; _appPath = appEnvironment == null ? "" : appEnvironment.ApplicationBasePath; }Program
var outputFilePath = args.Length > 0 ? args[0] : Path.Combine(_appPath, "../Microsoft.Framework.Globalization.CultureInfoCache/CultureInfoList.cs");outputFilePath
修改后,直接运行该工程,会发现Microsoft.Framework.Globalization.CultureInfoCache解决方案中CultureInfoList.cs文件被更改。
这个文件内还有值得我们注意的,就是版本和.net环境的对应关系
private static string CheckFor45DotVersion(int releaseKey) { if (releaseKey >= 393273) { return "4.6 RC or later"; } if ((releaseKey >= 379893)) { return "4.5.2 or later"; } if ((releaseKey >= 378675)) { return "4.5.1 or later"; } if ((releaseKey >= 378389)) { return "4.5 or later"; } // This line should never execute. A non-null release key should mean // that 4.5 or later is installed. return "No 4.5 or later version detected"; }