[原]ASP.NET MVC 3 Razor 多國語言參考解決方案 補充

來源:互聯網
上載者:User

話說當年張古董將老婆借給了李成龍,結果最後竟然一借不回了。這件事呢兩個方面都要怪:張古董動機不純,李成龍作人也不厚道,但一般情況下佔人便宜是很上癮的。

Reflector沒有人不知道吧,.NET用了好多年的人可能已經不需要再去看.NET原始碼了,一來是之前看過了,二來是很多的實現方式和運行原理能猜個七七八八的,但是對於初學者以及想查看有些不常用的.NET類型原始碼的人來說,沒有Reflector真是挺難受的。但是前兩天突然聽到個訊息,Reflector居然收費了!!!這麼好用的一個工具,居然不給免費使用了???這就好像張古董把老婆借給李成龍一樣,讓李成龍夜夜笙歌,好不逍遙快樂,有一天突然張古董要把老婆要回,李成龍寂寞冷清,肯定不是滋味,但好在張古董借出去的是個活物,也不知是啥原因居然就跟定李成龍了,讓張古董來了個人財兩空。但是Reflector是個死物,你就是再千呼萬喚也得遵循主人的命令。又扯遠了...

進入正題:

上文提到,使用System.Resources.ResXResourceReader(System.Windows.Forms.dll)類型擷取資源檔中的項的方式實現了MVC下的Localization,但是這個方案只能算是個原型或是參考方案,今天又將這個類型研究了下,幸虧同事手頭裡有個沒有升過級的Reflector,(我不小心點了不自動升級按鈕,這應該不算是侵權吧),開啟看了ResXResourceReader的實現原理,原來該類型就是使用解析XML的方式把資源項給拿了出來,放到了一個IDictionaryEnumerator裡面。

這樣就清楚多了,該類型可以在Web程式裡面安全的使用;這個類型主要的開銷在於讀取以及解析resx檔案上面。平時習慣上我比較喜歡使用緩衝來解決某些情景下的效能問題,這次沒有例外,.NET4中出一System.Runtime.Caching.MemoryCache還沒有用過呢,於是拿出來練練手。

piapia的,把上文中的代碼修改如下:

    public static class LocalizationHelpers    {        public static string Lang(this HtmlHelper htmlhelper, string key)        {            var viewPath = (htmlhelper.ViewContext.View as BuildManagerCompiledView).ViewPath;            var viewName = viewPath.Substring(viewPath.LastIndexOf('/'), viewPath.Length - viewPath.LastIndexOf('/')).TrimStart('/');            var filePath = htmlhelper.ViewContext.HttpContext.Server.MapPath(viewPath.Substring(0, viewPath.LastIndexOf('/') + 1)) + "App_LocalResources";            var langs = htmlhelper.ViewContext.HttpContext.Request.UserLanguages;            string resxPath = string.Format(@"{0}\{1}.resx", filePath, viewName);            foreach (var lang in langs)            {                if (File.Exists(string.Format(@"{0}\{1}.{2}.resx", filePath, viewName, lang)))                {                    resxPath = string.Format(@"{0}\{1}.{2}.resx", filePath, viewName, lang);                    break;                }            }            var result = ResXCache.GetResValue(resxPath, key);            return result;        }        public static class ResXCache        {            public static string GetResValue(string file, string key)            {                ObjectCache cache = MemoryCache.Default;                IEnumerable<DictionaryEntry> resxs = null;                if (cache.Contains(file) == false)                {                    resxs = new ResXResourceReader(file).Cast<DictionaryEntry>();                    cache.Add(file, resxs, new CacheItemPolicy() { Priority = CacheItemPriority.NotRemovable });                }                else                {                    resxs = cache.GetCacheItem(file).Value as IEnumerable<DictionaryEntry>;                }                return (string)resxs.FirstOrDefault<DictionaryEntry>(x => x.Key.ToString() == key).Value;            }        }    }

OK,基本上這個方案我覺得可以用在項目裡面了。

3月19日更新內容,重寫了Lang方法,減少了驗證資源檔是否存在的步驟----------------------------------------------------------------

    public static class LocalizationHelper    {        public static string Lang(this HtmlHelper htmlhelper, string key)        {            var viewPath = (htmlhelper.ViewContext.View as BuildManagerCompiledView).ViewPath;            var viewName = viewPath.Substring(viewPath.LastIndexOf('/'), viewPath.Length - viewPath.LastIndexOf('/')).TrimStart('/');            var filePath = htmlhelper.ViewContext.HttpContext.Server.MapPath(viewPath.Substring(0, viewPath.LastIndexOf('/') + 1)) + "App_LocalResources";            var langs = htmlhelper.ViewContext.HttpContext.Request.UserLanguages.Union<string>(new string[] { "" });            IEnumerable<DictionaryEntry> resxs = null;            foreach (var lang in langs)            {                var resxKey =                    string.IsNullOrWhiteSpace(lang) ? string.Format(@"{0}\{1}.resx", filePath, viewName) : string.Format(@"{0}\{1}.{2}.resx", filePath, viewName, lang);                resxs = GetResx(resxKey);                if (resxs != null) { break; }            }            return (string)resxs.FirstOrDefault<DictionaryEntry>(x => x.Key.ToString() == key).Value;        }        private static IEnumerable<DictionaryEntry> GetResx(string resxKey)        {            ObjectCache cache = MemoryCache.Default;            IEnumerable<DictionaryEntry> resxs = null;            if (cache.Contains(resxKey))            {                resxs = cache.GetCacheItem(resxKey).Value as IEnumerable<DictionaryEntry>;            }            else            {                if (File.Exists(resxKey))                {                    resxs = new ResXResourceReader(resxKey).Cast<DictionaryEntry>();                    cache.Add(resxKey, resxs, new CacheItemPolicy() { Priority = CacheItemPriority.NotRemovable });                }            }            return resxs;        }    }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.