国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[Asp.net 5] Localization-簡單易用的本地化-全球化信息

2019-11-17 02:07:47
字體:
來源:轉載
供稿:網友

[asp.net 5] Localization-簡單易用的本地化-全球化信息

本篇比較簡單介紹Localization解決方案中:

  • Microsoft.Framework.Globalization.CultureInfoCache 工程
  • CultureInfoGenerator 工程

Microsoft.Framework.Globalization.CultureInfoCache

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; }        }    }CultureInfoCache
CultureInfoCache Part1
 public static partial class CultureInfoCache    {        public static readonly HashSet<string> KnownCultureNames = new HashSet<string>        {            #region culture            "ar",            .........            "zh-CHT"            #endregion        };    }
CultureInfoCache Part2
  • 系統內部使用ConcurrentDictionary<string, CacheEntry>進行數據緩存
  • ConcurrentDictionary是線程安全性的字典表和Dictionary<,>功能類似
  • CacheEntry實際是對CultureInfo的封裝,還僅僅在內部使用
  • GetOrAdd方法:如果字典里包含,直接返回;如果不包含,創建之后返回。第一個參數是查找的參數,第二個是func參數,用于創建。
  • KnownCultureNames:用于存放當前環境支持的語言CultureInfo。(和操作系統,.net環境有關系)
  • 因為KnownCultureNames需要動態生成,所以兩部分分離了。

CultureInfoGenerator

這個工程的作用就是生成Microsoft.Framework.Globalization.CultureInfoCache的Part2,說實話我覺得這個工程這么架構是有問題的。難道每次都重新生成代碼嗎?不過現有源碼是這樣的,我只是按照現有邏輯分享給大家。

說有問題,這個解決方案還真有問題,下面倆個問題需要手工修改下:

  • 修改構造函數,當appEnvironment為null的時候,給默認值。
 public Program(IapplicationEnvironment appEnvironment)        {            _appName = appEnvironment == null ? "CultureInfoGenerator" : appEnvironment.ApplicationName;            _appPath = appEnvironment == null ? "" : appEnvironment.ApplicationBasePath;        }
Program
  • 修改outputFilePath路徑:../Microsoft.Framework.Globalization.CultureInfoCache/CultureInfoList.cs至../Microsoft.Framework.Globalization.CultureInfoCache/CultureInfoList.cs.
 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";        }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德昌县| 张家口市| 广灵县| 巩义市| 临城县| 滁州市| 阳山县| 诸城市| 盈江县| 庐江县| 巧家县| 革吉县| 商洛市| 英超| 宿松县| 闽侯县| 天台县| 竹溪县| 浮梁县| 兴和县| 海林市| 麻栗坡县| 台南市| 荥阳市| 航空| 图们市| 宣城市| 同江市| 鄂伦春自治旗| 化德县| 林周县| 监利县| 宕昌县| 石景山区| 定边县| 建平县| 高邮市| 平果县| 贡嘎县| 威海市| 奉贤区|