zoukankan      html  css  js  c++  java
  • MVC多语言设置 实战简洁版

    此方式可以通过更改进行更改进程语言设定,支持从系统获取默认的区域设定,支持自定义,自定义的方式可以为cookie,可为资料库获取,session等方式。

    具体怎么设定就看个人需要了。

    第一步:

    添加资源文件,添加的时候要将资源文件类型设定为public

    第二步:

    新定义类:

    namespace System.Web.Mvc
    {
        public static class LangHelper
        {
            //界面普通文字的多语言
            public static string GetLangbyKey(this HtmlHelper htmlhelper, string key)
            {
                string lang = "zh-CN";
               
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
                Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture;
    
                Type resourceType = (Thread.CurrentThread.CurrentUICulture.Name == "zh-CN") ? typeof(WebApplication4.Resources.zh_CN) : typeof(WebApplication4.Resources.zh_TW);
               
                PropertyInfo p = resourceType.GetProperty(key);
                if (p != null)
                    return p.GetValue(null, null).ToString();
                else
                    return "undefined";
            }
    
            //js定义多语言弹出框
            public static string LangOutJsVar(this HtmlHelper htmlhelper, string key)
            {
                string lang = "zh-CN";
    
    
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
                Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture;
    
                Type resourceType = (Thread.CurrentThread.CurrentUICulture.Name == "zh-CN") ? typeof(WebApplication4.Resources.zh_CN) : typeof(WebApplication4.Resources.zh_TW);
                PropertyInfo p = resourceType.GetProperty(key);
                if (p != null)
                    return string.Format("var {0} = '{1}'", key, p.GetValue(null, null).ToString());
                else
                    return string.Format("var {0} = '{1}'", key, "undefined");
            }
        }
    }

    页面使用方法

    <h2>@Html.GetLangbyKey("name")</h2>

  • 相关阅读:
    试用第三方web推送GoEasy
    使用intellj idea 搭建本地开发环境
    一种基于struts2 拦截器 和 log4j的轻量级crm权限及行为跟踪方式
    Spring AOP声明式事务异常回滚 若干法则
    spring aop 切面测试
    centos 安装 mysql
    只是 换个方式,
    contrller 是 file's owners,
    色差,15,还是15 ,换了颜色 就显的小了,
    一块,分开,还是不分开 一个整体,
  • 原文地址:https://www.cnblogs.com/xuhongfei/p/5226864.html
Copyright © 2011-2022 走看看