首页 > 编程语言 > 详细

ASP.net MVC5 多语言

时间:2019-02-13 16:51:49      阅读:291      评论:0      收藏:0      [点我收藏+]

1.创建项目添加App_GlobalResources文件夹

2.添加资源文件Global.resx

3.添加资源文件内容

4..创建全局过滤器

 protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        }

  

 public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            filters.Add(new LanageFilter());
        }
    }

  

 public class LanageFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var lang = filterContext.HttpContext.Session["lanage"];
            if (lang==null)
            {
                lang = "zh";
            }
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(lang.ToString());
            Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
            filterContext.HttpContext.Session["lanage"] = lang;
        }
    }

  

5.创建更新session的方法

 [HttpGet]
        public JsonResult ChangeLanage(string lang)
        {
            Session["lanage"] = lang;
            return Json("true", JsonRequestBehavior.AllowGet);
        }

  

6.写页面点击事件

  $("#zn").click(function () {
        $.ajax({
            url: ‘@Url.Action("ChangeLanage", "Home")‘,
            type: "get",
            dataType: "json",
            data: {
                "lang": "zn"
            },
            success: function (data) {
                if (data) {
                    window.location.reload();
                }
            }
        })
    })

7.显示信息

<h2>@Resources.Global.Index</h2>

  技术分享图片

ASP.net MVC5 多语言

原文:https://www.cnblogs.com/JueXiaoQiang/p/10370293.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!