小清新版多語言功能實踐 麼麼噠

創業項目中涉及到多語言切換的需求,對比了一些關於ASP.Net下多語言的解決方案通常分爲如下三種方式:javascript

1.數據庫方式 - 創建相應的語言表,能夠從後臺配置語言數據。java

2.XML方式 - 不一樣的語言配置不一樣的xml文件,比較靈活可是xml比較厚重。數據庫

3.Resources 感受不夠靈活,技術操做容易些,要是其餘人搭配就比較麻煩。後端

以上三種方式網上有不少介紹,不作過多探討。緩存

 

個人方式是結合js文件來統一先後端語言配置,並更容易讓技術和翻譯進行搭配工做。this

創建語言文件,能夠任意添加語言文件翻譯

   

zh_CN.js文件內容:code

var lan = {
    "Size": "大小",
    "Close": "關閉"
};

ru.js文件內容:xml

var lan = {   
    "Size":"Размер",
    "Close":"Закрыть"
};

 前臺js使用方式blog

<script type="text/javascript" src="/i18n/ru.js"></script> //語言文件能夠根據後臺參數動態切換
<script type="text/javascript">
        $(function () {
            alert(lan.Close);
        });
</script>

後臺讀取js文件並解析到Hashtable

 public static class Localization
    {        
        /// <summary>
        /// 加載語言文件
        /// </summary>
        /// <param name="code">語言code</param>
        /// <returns></returns>
        public static Hashtable Loadi18n(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                throw new ArgumentNullException("語言代碼爲空...");
            }

            string filePath = HttpContext.Current.Server.MapPath("~/i18n/" + code + ".js");
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("語言文件不存在...");
            }

            string cacheName = "cache_lan_" + code;
            Hashtable i18nHashtable = new Hashtable();

            if (HttpRuntime.Cache[cacheName] != null)
            {
                i18nHashtable = HttpRuntime.Cache[cacheName] as Hashtable;                
            }
            else
            {

                string lanText = "";

                //獲取文件內容
                using (StreamReader reader = new StreamReader(filePath, System.Text.Encoding.UTF8))
                {
                    lanText = reader.ReadToEnd();
                }

                //去掉干擾字符
                lanText = Regex.Replace(lanText, "[\f\n\r\t\v]", "");

                //匹配文本內容
                Regex regex = new Regex(@"\{.*\}", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                Match match = regex.Match(lanText);

                i18nHashtable = JsonConvert.DeserializeObject<Hashtable>(match.Value);

                //緩存信息
                CacheDependency fileDepends = new CacheDependency(filePath);
                HttpRuntime.Cache.Insert(cacheName, i18nHashtable, fileDepends);
            }

            return i18nHashtable;
        }


        /// <summary>
        /// 獲取語言值
        /// </summary>
        /// <param name="code">語言code</param>
        /// <param name="key">語言key</param>
        /// <returns></returns>
        public static string M(string code, string key)
        {
            Hashtable i18nHashtable = Loadi18n(code);

            if (i18nHashtable.ContainsKey(key))
            {
                return i18nHashtable[key].ToString();
            }

            return string.Empty;
        }

        /// <summary>
        /// 獲取語言值
        /// </summary>
        /// <param name="i18nHashtable">語言集合</param>
        /// <param name="key">語言key</param>
        /// <returns></returns>
        public static string M(this Hashtable i18nHashtable, string key)
        {
            if (i18nHashtable.ContainsKey(key))
            {
                return i18nHashtable[key].ToString();
            }

            return string.Empty;
        }
    }

調用方式:

//語言部分能夠根據參數自由切換
Localization.M("ru", "Size")

//若是頁面中調用屢次能夠使用以下方式
Hashtable lan = Localization.Loadi18n("ru");
lan.M("Size")
lan.M("Close")

 

歡迎有多語言開發經驗的進行交流。