码迷,mamicode.com
首页 > Web开发 > 详细

json文件生成

时间:2019-06-03 20:12:03      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:this   component   sort   e30   tran   static   repeat   标签   amp   

// import Translate from ‘translate-components‘
/*
 * 匹配所有汉字RegExp: [\u4e00-\u9fa5]   [\u4E00-\u9FA5]|[\uFE30-\uFFA0]
 * 汉字部分除了^的标签jsonRegExp: <Translate>[^\^]+?(?=-{4,}?)-{4,}?([a-zA-Z]+)</Translate>  t(‘$1‘)
 * 汉字部分除换行符之外的任意字符的标签jsonRegExp: <Translate>(?:\^).+?(?=-{4,}?)-{4,}?([a-zA-Z]+)</Translate>
 */

function regExpStrToJson(strCount, language, isDocument) {
    var translation = ‘{\n‘;
    for (var i=0; i<strCount.length; i++) {
        var suffix = strCount.length === (i + 1) ? ‘"\n‘ : ‘",\n‘;
        var patch1 = /(?<=sign\(‘)(.+?)(?=‘\);;;?)/g;
        var result1 = patch1.exec(strCount[i]);
        translation += ‘    "‘ + RegExp.$1;
        // var patch2 = language === ‘cn‘ ? /(?<=cn\(‘)(.+?)(?=‘\);;;?)/g : /(?<=tl\(‘)(.+?)(?=‘\);;;?)/g;
        var patch2 = new RegExp("(?<=" + language + "\\(‘)(.+?)(?=‘\\);;;?)","g");
        var result2 = patch2.exec(strCount[i]);
        translation += ‘": "‘ + RegExp.$1.replace(‘^‘, ‘‘) + suffix;
    }
    translation += ‘}‘;
    isDocument ? $(‘body‘).html(translation.replace(/\n/g, ‘<br>‘).replace(/\s{4}/g, ‘&nbsp;&nbsp;&nbsp;&nbsp;‘)) : console.log(translation);
}


var strToArr = ‘‘, tlStr = ‘???????????????????????????‘;
$.ajax(‘./static/translations.json‘,{
    dataType:‘json‘,
    type:‘get‘,
    success:function(data){
        let repeatObj = {};
        for (key in data) {
            let thisKey = key.split(/-{4,}/);
            if (repeatObj[thisKey[1]] === undefined) {
                repeatObj[thisKey[1]] = 1;
                strToArr += "sign(‘" + thisKey[1] + "‘);;;cn(‘" + thisKey[0] + "‘);;;tl(‘" + tlStr.split(‘‘).sort(() => Math.random() > 0.5 ? -1 : 1).join(‘‘).substring(0, parseInt(Math.random() * (tlStr.length - 1) + 5)) + "‘);;;\n";
            }
        }
        let strArr = strToArr.split(‘\n‘);
        
        regExpStrToJson(strArr, ‘cn‘, 1);
        regExpStrToJson(strArr, ‘tl‘);
    }
});

 

json文件生成

标签:this   component   sort   e30   tran   static   repeat   标签   amp   

原文地址:https://www.cnblogs.com/universe-cosmo/p/10969726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!