zoukankan      html  css  js  c++  java
  • 小清新版多语言功能实践 么么哒

    创业项目中涉及到多语言切换的需求,对比了一些关于ASP.Net下多语言的解决方案一般分为以下三种方式:

    1.数据库方式 - 建立相应的语言表,可以从后台配置语言数据。

    2.XML方式 - 不同的语言配置不同的xml文件,比较灵活但是xml比较厚重。

    3.Resources 感觉不够灵活,技术操作容易些,要是其他人搭配就比较麻烦。

    以上三种方式网上有很多介绍,不做过多探讨。

    我的方式是结合js文件来统一前后端语言配置,并更容易让技术和翻译进行搭配工作。

    建立语言文件,可以任意添加语言文件

       

    zh_CN.js文件内容:

    var lan = {
        "Size": "大小",
        "Close": "关闭"
    };
    

    ru.js文件内容:

    var lan = {   
        "Size":"Размер",
        "Close":"Закрыть"
    };
    

     前台js使用方式

    <script type="text/javascript" src="/i18n/ru.js"></script> //语言文件可以根据后台参数动态切换
    <script type="text/javascript">
            $(function () {
                alert(lan.Close);
            });
    </script>
    

    后台读取js文件并解析到Hashtable

     public static class Localization
        {        
            /// <summary>
            /// 加载语言文件
            /// </summary>
            /// <param name="code">语言code</param>
            /// <returns></returns>
            public static Hashtable Loadi18n(string code)
            {
                if (string.IsNullOrEmpty(code))
                {
                    throw new ArgumentNullException("语言代码为空...");
                }
    
                string filePath = HttpContext.Current.Server.MapPath("~/i18n/" + code + ".js");
                if (!File.Exists(filePath))
                {
                    throw new FileNotFoundException("语言文件不存在...");
                }
    
                string cacheName = "cache_lan_" + code;
                Hashtable i18nHashtable = new Hashtable();
    
                if (HttpRuntime.Cache[cacheName] != null)
                {
                    i18nHashtable = HttpRuntime.Cache[cacheName] as Hashtable;                
                }
                else
                {
    
                    string lanText = "";
    
                    //获取文件内容
                    using (StreamReader reader = new StreamReader(filePath, System.Text.Encoding.UTF8))
                    {
                        lanText = reader.ReadToEnd();
                    }
    
                    //去掉干扰字符
                    lanText = Regex.Replace(lanText, "[f
    
    	v]", "");
    
                    //匹配文本内容
                    Regex regex = new Regex(@"{.*}", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                    Match match = regex.Match(lanText);
    
                    i18nHashtable = JsonConvert.DeserializeObject<Hashtable>(match.Value);
    
                    //缓存信息
                    CacheDependency fileDepends = new CacheDependency(filePath);
                    HttpRuntime.Cache.Insert(cacheName, i18nHashtable, fileDepends);
                }
    
                return i18nHashtable;
            }
    
    
            /// <summary>
            /// 获取语言值
            /// </summary>
            /// <param name="code">语言code</param>
            /// <param name="key">语言key</param>
            /// <returns></returns>
            public static string M(string code, string key)
            {
                Hashtable i18nHashtable = Loadi18n(code);
    
                if (i18nHashtable.ContainsKey(key))
                {
                    return i18nHashtable[key].ToString();
                }
    
                return string.Empty;
            }
    
            /// <summary>
            /// 获取语言值
            /// </summary>
            /// <param name="i18nHashtable">语言集合</param>
            /// <param name="key">语言key</param>
            /// <returns></returns>
            public static string M(this Hashtable i18nHashtable, string key)
            {
                if (i18nHashtable.ContainsKey(key))
                {
                    return i18nHashtable[key].ToString();
                }
    
                return string.Empty;
            }
        }
    

    调用方式:

    //语言部分可以根据参数自由切换
    Localization.M("ru", "Size")
    
    //如果页面中调用多次可以使用如下方式
    Hashtable lan = Localization.Loadi18n("ru");
    lan.M("Size")
    lan.M("Close")
    

    欢迎有多语言开发经验的进行交流。

  • 相关阅读:
    uC/OS-II时间(OS_time)块
    uC/OS-II任务(OS_task)块
    uC/OS-II信号(OS_sem)块
    uC/OS-II队列(OS_q)块
    uC/OS-II互斥信号(OS_mutex)块
    uC/OS-II内存(OS_mem)块
    elasticsearch-installation
    rabbitmq的安装
    str_翻转字符串
    str_2.判断两个字符串是否互为旋转词
  • 原文地址:https://www.cnblogs.com/okzhangning/p/3834733.html
Copyright © 2011-2022 走看看