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