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"];        }    }

NET技术MVC2.0本地化(另类解决方案)<上>,转载需保留来源!

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。