zoukankan      html  css  js  c++  java
  • ASP.Net MVC多语言

    .NET MVC 多语言网站

    通过浏览器语言首选项改变MVC的语言,通过浏览器语言选项,修改脚本语言。

    一、添加资源文件

    1、添加App_GlobalResources文件夹。

     

    2、添加默认的资源文件和对应的语言码资源文件。如zh-cn代表中国大陆,en-us代表美制英语。详情:http://www.lingoes.cn/zh/translator/langcode.htm

     

    3、将资源文件设置成public。新建时默认是internal,这样不能被访问。

    右键点击资源文件,在其的属性中将自定义工具(Custom Tool) 从资源代理生成器(GlobalResourceProxyGenerator) 改为 公共文件(PublicResXFileCodeGenerator),然后将生成操作从内容改为嵌入的资源。

     

    4、在web.config中添加语言选项。这样就会根据浏览器语言首选项,判断要使用的资源。

        <!--根据浏览器的语言首选项,决定语言项-->
    
    <globalization uiCulture="auto" culture="auto" enableClientBasedCulture="true" />

    5、在对应的文件中添加键值对。

     

    二、View+Control+Model的普通文字多语言

    1、view

     

    2、control

     

    3、model

    //[Required(ErrorMessage = "密码不能为空")]
    
     [Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "PasswordNotNull")]

    三、DisplayName的多语言化。重写DisplayName方法

     

      public class LocalDisplayName : DisplayNameAttribute
    
        {
    
            private string _defaultName = "";
    
            public Type ResourceType { get; set; }
    public string ResourceName { get; set; } public LocalDisplayName(string defaultName) { _defaultName = defaultName; } public override string DisplayName { get { PropertyInfo p = ResourceType.GetProperty(ResourceName); if (p != null) { return p.GetValue(null, null).ToString(); } else { return _defaultName; } } } }

    model使用:

     [LocalDisplayName("发布时间", ResourceName = "PublishDateTime", ResourceType = typeof(Resource))]

    四、脚本多语言

    原始方法:

    我的方法是在引用一个公用的脚本文件,由该文件通过当前语言引入对应的语言资源脚本。如果当前浏览区语言没有没有对应的资源脚本,就引入默认的资源脚本。

     

    1、通过浏览器语言引入对应的语言资源脚本。

    //将所有的语言资源脚本文件名罗列再次,用&分割
    
    var languageresources = "en-us&zh-cn"
    
     
    
    //根据当前浏览器语言验证语言脚本是否存在,如果在则加载,则加载默认的zh-cn.js
    
    var thislanguagere = navigator.language.toLowerCase();
    
     
    
    var script = document.getElementsByTagName('HEAD').item(0);
    
    script = document.createElement("script");
    
    script.type = "text/javascript";
    
     
    
    var language = languageresources.split("&")
    
    var flag = false;
    
    for (var item in language) {
    
        if (thislanguagere == language[item]) {
    
            var flag = true;
    
            script.src = "/Scripts/controls/" + thislanguagere + ".js";
    
            document.body.appendChild(script);
    
        }
    
    }
    
    if (flag == false) {
    
        script.src = "/Scripts/controls/zh-cn.js";
    
        document.body.appendChild(script);
    
    }

    2、对应资源脚本写法。设置键值对

     

    3、具体页面脚本使用,引用。需要先引用判断语言脚本。

     

    更新方法:

    1、采用jquery.i18n.properties-1.0.9.js提供的方法:

    (1)、jQuery.i18n.properties() 用法:引入对应的资源文件

     jQuery.i18n.properties({ 
    
        name:'strings',// 资源文件名称
    
        path:'bundle/',// 资源文件所在目录路径
    
        mode:'both',// 模式:变量或 Map 
    
        language:'pt_PT',// 对应的语言
    
        cache:false, 
    
        encoding: 'UTF-8', 
    
        callback: function() {// 回调方法
    
        } 
    
     });

    (2)、jQuery.i18n.prop(key)

    该方法以 map 的方式使用资源文件中的值,其中 key 指的是资源文件中的 key。当 key 指定的值含有占位符时,可以使用 jQuery.i18n.prop(key,var1,var2 … ) 的形式,其中 var1,var2 …对各占位符依次进行替换。例如资源文件中有“msg_hello= 您好 {0},今天是 {1}。”的键值对,则我们可以采用“jQuery.i18n.prop( ‘ msg_hello ’ , ’小明’ , ’星期一’ );”的形式使用 msg_hello。

    (3)、jQuery.i18n.browserLang() 用于获取浏览浏览器的语言信息,这里不再单独介绍。

    2、文档结构:其中strings是随便起到,后缀的en-us代表当前浏览器语言项。没有后缀代表默认语言。

    声明变量写法:

     

     

    3、在具体脚本内的写法:

    function loadProperties() {
    
        jQuery.i18n.properties({// 加载资浏览器语言对应的资源文件
    
            name: 'strings', // 资源文件名称
    
            path: '/Scripts/controls/', // 资源文件路径
    
            mode: 'map', // 用 Map 的方式使用资源文件中的值
    
            callback: function () {//加载成功后设置显示内容
    
            }
    
        });
    
    }
    
    $(function () {
    
        loadProperties();//引入资源文件
    
        //忘记密码的点击事件
    
        $("#hex-forgotpassword").click(function () {
    
            if ($("#UserName").val() == "") {
    
                //alert("请在登陆框输入用户名,再点击找回密码!");
    
                alert($.i18n.prop('forgotpasswordnotusername'));
    
            }
    
            else {
    
                //var msg = "您确认要找回密码吗?您的密码将发送到您的邮箱!";
    
                var msg = $.i18n.prop('forgotpassword');
    
                if (confirm(msg) == true) {
    
                    $.ajax({
    
                        type: "Get",
    
                        url: '/Account/ForgetPassword',
    
                        data: "userName=" + $("#UserName").val(),
    
                        success: function (msg) {
    
                            alert(msg);
    
                        }
    
                    });
    
                } else {
    
                    return false;
    
                }
    
            }
    
        });
    
        //用户名文本框获得焦点
    
        $('#UserName').focus();
    
    })

    另附:可修改的多语言,即通过语言超链接修改当前语言项。

    1、在Global.asax文件中:

        

       protected void Application_AcquireRequestState(Object sender, EventArgs e)
    
            {
    
                Authentication.AttachAuthTicketByUrl<WebAuthentication>(Context);
    
                //多语言 可切换
    
                if (HttpContext.Current.Session != null)
    
                {
    
                    System.Globalization.CultureInfo ci = (System.Globalization.CultureInfo)this.Session["CurrentLanguage"];
    
                    if (ci == null)
    
                    {
    
                        ci = new System.Globalization.CultureInfo("zh-cn");
    
                        this.Session["CurrentLanguage"] = ci;
    
                    }
    
                    System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
    
                    System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(ci.Name);
    
                }
    
            }

    2、在control中:

            public ActionResult ChangeLanguage(string language)
    
            {
    
                Session["CurrentLanguage"] =new System.Globalization.CultureInfo(language);
    
                return Redirect("Login");
    
            }

    3、在页面中:

    @Html.ActionLink("English", "ChangeLanguage", "Account", new { language = "en-us" }, new { @class = "hex-a" })
    
    @Html.ActionLink("Chinese", "ChangeLanguage", "Account", new { language = "zh-cn" }, new { @class = "hex-a" })

    参考资料:

    .NET MVC 2 多语言网站的实现: 

     http://blog.163.com/xu_shuhao/blog/static/52577487201092402610920/

    关于在mvc4中多语言建站的实例:

    http://www.cnblogs.com/Joans/archive/2012/08/16/2640473.html

    使用 jQuery.i18n.properties 实现 Web 前端的国际化:

    https://www.ibm.com/developerworks/cn/web/1305_hezj_jqueryi18n/

  • 相关阅读:
    scikitlearn中predict_proba用法 (与predict的区别)
    Sklearn,TensorFlow,keras模型保存与读取
    TensorFlow GPU 的使用
    Keras2.2 predict和fit_generator的区别
    wordcloud词云可视化
    使用scp命令,远程上传下载文件/文件夹
    L0/L1/L2范数的联系与区别
    Python中的正斜杠/与反斜杠\
    在Keras中使用tensorboard可视化acc等曲线
    Linux终端没有GUI,使用matplotlib绘图
  • 原文地址:https://www.cnblogs.com/sanqianjin/p/3804193.html
Copyright © 2011-2022 走看看