zoukankan      html  css  js  c++  java
  • [原]ASP.NET MVC 3 Razor 多国语言参考解决方案 补充三

    原以为可以使用之前的多国语言方案进行编程了,没想到第一个正式点的页面就卡了,为什么呢,因为通过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;
            }
        }
    
  • 相关阅读:
    PHP 快速实现大文件上传
    websocketd
    mybatis——一级缓存、二级缓存
    mybatis学习——多对一和一对多查询
    XML文件存在中文注释报错问题( 3 字节的 UTF-8 序列的字节 3 无效)
    mybatis设置自动提交事务
    mybatis之Param注解
    mybatis学习——实现分页
    mybatis学习——日志工厂
    mybatis——解决属性名和数据库字段名不一致问题
  • 原文地址:https://www.cnblogs.com/think8848/p/1989376.html
Copyright © 2011-2022 走看看