原以为可以使用之前的多国语言方案进行编程了,没想到第一个正式点的页面就卡了,为什么呢,因为通过HtmlHelper只能找到当前请求的View是哪个,如果这个View使用了Layout或是Partial就歇菜了。通过一番尝试,发现使用HtmlHelper.ViewDataContainer属性可以获得一个WebViewPage对象,这个对象中的VirtualPath对象代表当前正在Excute的页面属于哪个物理文件(.cshtml),于是修改代码如下:
public static class LocalizationHelper
{
public static string Lang(this HtmlHelper htmlhelper, string key)
{
var viewPath = (htmlhelper.ViewDataContainer as WebViewPage).VirtualPath;
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;
}
}