今天介绍ASP.NET MVC2实现国际化与本地化方案,在asp.net中,本地化方案有很多种,可是ASP.NET MVC中呢?伤脑了吧,不过放心,今天给大伙介绍一下,包你有启发,哈哈。
一、建立自定义的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 Dictionary<string, string> ReadLocalizationResource()
{
string _XMLPath = "";
Dictionary<string, string> cacheData = null;
if (cacheData != null)
{
return cacheData;
}
Dictionary<string, string> cachedData = new Dictionary<string, 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 List<string> { _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 (Dictionary<string, 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 页面。
四、小结
本文ASP.NET MVC2实现国际化与本地化方案用另外一套方案解决了MVC2.0程序的本地化问题,是否对您有所启发?
2011/9/8 19:17:06 | ASP.NET MVC教程 | |