MVC2.0本地化(另類解決方案)<上>
前不久看見一篇文章:在asp.net中使用Response.Filter 過濾網站敏感字符的解決方案。于是我借題發揮用Response.Filter 為MVC2.0 進行多國語言本地化。如果存在不足的地方,希望您指出。
本文主要給出具體思路,希望能給讀者帶來一定的啟發:日常開發中不是所有的方案要中規中矩用常用方法解決問題。比如本文的本地化就不用resource文件來處理。
具體步驟:
一、建立自定義的LocalizationHandler類
LocalizationHandler 繼承System.IO.Stream類 ,LocalizationHandler實例化后賦值給Response.Filter。這里主要通過Response.Filter來本地化MVC2.0程序。具體的Response.Filter 用法請參看MSDN.代碼如下:
public class LocalizationHandler : Stream { private Stream responseStream; public LocalizationHandler(Stream inputStream) { responseStream = inputStream; } public override bool CanRead { get { return true; } } public override bool CanSeek { get { return true; } } public override bool CanWrite { get { return true; } } public override void Flush() { responseStream.Flush(); } public override long Length { get { return 0; } } long postion; public override long Position { get { return postion; } set { postion = value; } } public override int Read(byte[] buffer, int offset, int count) { return responseStream.Read(buffer, offset, count); } public override long Seek(long offset, SeekOrigin origin) { return responseStream.Seek(offset, origin); } public override void SetLength(long value) { responseStream.SetLength(value); } public override void Write(byte[] buffer, int offset, int count) { string sBuffer = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count); string pattern = @"(|)=(.*?)(|)";//正則替換類似頁面格式為這樣的字符串如:=OtherContent sBuffer = Regex.Replace(sBuffer, pattern, delegate(Match c) { return ReadLocalizationResource().FirstOrDefault(d = d.Key == c.Groups[2].Value).Value; }); ReadLocalizationResource(); byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(sBuffer); responseStream.Write(data, 0, data.Length); } ObjectCache cache = MemoryCache.Default; private Dictionarystring, string ReadLocalizationResource() { string _XMLPath = ""; Dictionarystring, string cacheData = null; if (cacheData != null) { return cacheData; } Dictionarystring, string cachedData = new Dictionarystring, string(); string serverPath = System.Web.HttpContext.Current.Server.MapPath("~"); _XMLPath = Path.Combine(serverPath, "LocalizationResource.xml"); //建立緩存(使用.net4.0最新緩存機制:System.Runtime.Caching;) if (cache["myCache"] == null) { CacheItemPolicy policy = new CacheItemPolicy(); policy.SlidingExpiration = TimeSpan.FromMinutes(60); policy.ChangeMonitors.Add(new HostFileChangeMonitor(new Liststring { _XMLPath })); var items = XElement.Load(_XMLPath).Elements("Module").Elements("item"); foreach (var item in items) { string key = item.Attribute("name").Value; string value = item.Value; cachedData.Add(key, value); } cache.Set("myCache", cachedData, policy); return cachedData; } return (Dictionarystring, string)cache["myCache"]; } }
代碼中的65行開始,是本地化核心代碼,在這里我們用正則匹配文本。用.NET4.0 System.Runtime.Caching;(嘗鮮)緩存機制提高程序執行效率。
二、修改global.asax文件
在global.asax中加入以下代碼:
protected void Application_BeginRequest(Object sender, EventArgs e) { Response.Filter = new LocalizationHandler(Response.Filter); }
三、建立自定義的XML本地化資源文件
截圖如下:
OK,一切準備就緒,我們在不用Response.Filter 過濾的情況下,運行截圖如下:
使用上文中Response.Filter過濾后:
結果將第三點中的XML作為傳統的資源文件后本地化了MVC View 頁面。
四、小結
本文用另外一套方案解決了MVC2.0程序的本地化問題,也適用于asp.net webform。同時本文還存在很多不足的地方,比如后臺異步的JSON格式本地化、用戶自定義本地化語言等將會在MVC2.0本地化(另類解決方案)下 文中得到完善。
希望本篇文章可以給您帶來幫助,如有不足之處歡迎指出,謝謝!