From 88c22458ba960f855a04609553a431f2dbfcdc66 Mon Sep 17 00:00:00 2001 From: nianqin Date: Mon, 30 Sep 2019 16:50:46 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9B=BD=E9=99=85?= =?UTF-8?q?=E5=8C=96=E6=8A=BD=E5=8F=96=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- i18n/configuration.js | 63 +++++++++++ i18n/index.js | 234 +++++++++++++++++++++++++++++++++++++++++ i18n/res/index.js | 9 ++ i18n/translation.js | 45 ++++++++ i18n/utils.js | 55 ++++++++++ package.json | 10 +- vve-i18n-cli.config.js | 3 + 7 files changed, 418 insertions(+), 1 deletion(-) create mode 100644 i18n/configuration.js create mode 100644 i18n/index.js create mode 100644 i18n/res/index.js create mode 100644 i18n/translation.js create mode 100644 i18n/utils.js create mode 100644 vve-i18n-cli.config.js diff --git a/i18n/configuration.js b/i18n/configuration.js new file mode 100644 index 0000000..80e315f --- /dev/null +++ b/i18n/configuration.js @@ -0,0 +1,63 @@ +const buildDebug = require("debug"); +const path = require("path"); +const fs = require("fs"); + +const debug = buildDebug("files:configuration"); + +const CONFIG_JS_FILENAME = "vve-i18n-cli.config.js"; + +function findConfigUpwards() { + let dirname = rootDir; + while (true) { + if (fs.existsSync(path.join(dirname, CONFIG_JS_FILENAME))) { + return dirname; + } + const nextDir = path.dirname(dirname); + if (dirname === nextDir) break; + dirname = nextDir; + } + return null; +} + +function findConfig (dirname) { + const filepath = path.resolve(dirname, CONFIG_JS_FILENAME); + const conf = readConfig(filepath); + if (conf) { + debug("Found root config %o in %o.", CONFIG_JS_FILENAME, dirname); + } + return conf; +} + +function loadConfig (filepath) { + try { + const conf = readConfig(filepath) + return conf + } catch (e) { + debug('error', e) + return null + } +} + +function readConfig(filepath) { + let options; + try { + const configModule = require(filepath); + options = configModule && configModule.__esModule + ? configModule.default || undefined + : configModule; + } catch (err) { + throw err; + } finally { + } + return { + filepath, + dirname: path.dirname(filepath), + options, + } +} + +module.exports = { + findConfigUpwards, + loadConfig, + readConfig, +} \ No newline at end of file diff --git a/i18n/index.js b/i18n/index.js new file mode 100644 index 0000000..970bd88 --- /dev/null +++ b/i18n/index.js @@ -0,0 +1,234 @@ +/** + * 根据配置的根文件夹(rootDir) + * 根据配置的规则数组,文件匹配规则 + * 根据配置的提出国际化的正则数组,比如:/(?:\$)?t\(['"](.+?)['"]/g + * 根据配置的输出目录,输出国际化 + * 根据配置的输出国际化语言列表 + * 配置文件目录 + * 配置文件不生效 + */ +/** + * 示例命令 + * 【抽取全部模块】npm run i18n + * 【抽离某个模块】npm run i18n -- -m eweb-setting-safe + * 【翻译】npm run i18n -- -m eweb-setting-safe -t + * 【强制翻译】npm run i18n -- -m eweb-setting-safe -t -F + * 【强制翻译某种语言】npm run i18n -- -m eweb-setting-safe -t -F -L en + */ +'use strict' +const program = require('commander'); +const jsonfile = require('jsonfile') +const utils = require('./utils') +const trans = require('./translation') // trans +const { loadConfig, findConfigUpwards } = require('./configuration') +const vfs = require('vinyl-fs') +const map = require('map-stream') +const path = require('path') +const fs = require('fs'); +const uniq = require('lodash.uniq') + +function commaSeparatedList(value, split = ',') { + return value.split(split).filter(item => item); +} + +program + .version('1.0.0') + .option('--root-dir ', '提交国际化文本的根目录') + .option('--files ', '文件规则,指定哪些文件中的规则化文本需要被提取') + .option('--regulars ', '国际化文本的正则表达式,正则中第一个捕获对象当做国际化文本', commaSeparatedList) + .option('--out-dir ', '生成的国际化资源包的输出目录') + .option('--languages ', '需要生成的国际化语言文件,目前支持zh、en多个用逗号分割,默认全部', commaSeparatedList) + .option('--config ', '配置文件路径') + .option('--no-config', '忽略rc配置文件') + .option('--module [name]', '模块名称,已eweb开头') + .option('-t, --translate', '是否使用翻译,默认翻译新增的文本,只能再bash环境下,window环境需要再git bash环境下执行') + .option('--force-translate', '强制翻译所有的内容') + .option('--translate-language ', '配合--translate使用,需要翻译的语言,目前支持en、ko,多个用逗号分割,默认全部', commaSeparatedList) + .parse(process.argv); + +const config = { + cwd: '.', + rootDir: '.', + files: [], + regulars: [], + outDir: '', + languages: ['zh', 'en'], + config: '', + noConfig: false, + translate: false, + forceTranslate: false, + translateLanguage: [] +} + +Object.assign(config, program) + +const CONFIG_JS_FILENAME = "vve-i18n-cli.config.js"; + +const absoluteCwd = path.resolve(config.cwd); + +// 优先判断是否需要读取文件 +if (!config.noConfig) { + let configFilePath = path.join(absoluteCwd, CONFIG_JS_FILENAME) + if (config.config) { + configFilePath = path.resolve(config.config) + } + if (fs.existsSync(configFilePath)) { + const conf = loadConfig(configFilePath) + if (conf) { + Object.assign(config, conf.options, program) + } + } +} + +const absoluteRootDir = path.resolve(absoluteCwd, config.rootDir); + +// const pathResolve = utils.pathResolve +// const fsExistsSync = utils.fsExistsSync +// const copyFile= utils.copyFile +// const filterObjByKeyRules = utils.filterObjByKeyRules +// const tranlateArr = trans.tranlateArr + +// // 模块的国际化的json文件需要被保留下的key,即使这些组件在项目中没有被引用 +// // key可以是一个字符串,正则,或者是函数 +// const MODULE_KEEP_KEY_RULES = [ +// /^G\/+/ // G/开头的会被保留 +// ] + +// // 匹配正则 +// const regexps = { +// regI18n: /(?:\$)?t\(['"](.+?)['"]/g, +// } + +// const I18N_TYPES = [ +// 'zh', // 中文 +// 'en', // 英文 +// ] + +// const i18nData = {} +// const tmpRegData = {} + +// // 从文件中提取模块的的国际化KEY +// function getModuleI18nData (modulePath, fileContent) { +// const regI18n = new RegExp(regexps.regI18n) +// if (!i18nData[modulePath]) { +// i18nData[modulePath] = [] +// } +// while ((tmpRegData.matches = regI18n.exec(fileContent))) { +// i18nData[modulePath].push(tmpRegData.matches[1]) +// } +// } + +// // 删除重复的key,并排序方便git比对 +// function normalizeI18nData () { +// const moduleKeys = Object.keys(i18nData) +// moduleKeys.forEach(key => { +// i18nData[key] = uniq(i18nData[key]).sort() +// }) +// } + +// // 根据旧数据,生成新数据 +// async function makeNewData (key, lang, originData) { +// const newData = filterObjByKeyRules(originData, MODULE_KEEP_KEY_RULES) // 根据配置保留一些keys值,保证即使在项目中不被引用也能保存下来 + +// let newAddDataArr = [] // 新增的数据,即在旧的翻译文件中没有出现 + +// i18nData[key].forEach(key => { +// if (originData.hasOwnProperty(key)) { +// newData[key] = originData[key] +// } else { +// newData[key] = key +// newAddDataArr.push(key) +// } +// }) + +// // 中文的不翻译 +// if (program.translate && lang !== 'zh') { +// let tranlateRst = {} + +// // 如果强制翻译,则翻译所有的key +// if (program.forceTranslate) { +// newAddDataArr= Object.keys(newData) +// } + +// // 配合--translate使用,需要翻译的语言,目前支持en、ko,多个用逗号分割,默认全部 +// if (!program.translateLanguage) { +// tranlateRst = await tranlateArr(lang, newAddDataArr) +// } else if (program.translateLanguage.includes(lang)) { +// tranlateRst = await tranlateArr(lang, newAddDataArr) +// } +// Object.assign(newData, tranlateRst) +// } +// return newData +// } + +// // 保存国际化文件 +// async function saveI18nFile({ +// dirPath, +// } = {}) { +// const i18nTypes = program.language ? I18N_TYPES.filter(item => program.language.includes(item)) : I18N_TYPES + +// for (let i = 0; i < i18nTypes.length; i++) { +// const item = i18nTypes[i] +// const i18nDir = path.resolve(dirPath, 'i18n') +// if (!fsExistsSync(i18nDir)) { +// fs.mkdirSync(i18nDir); +// } + +// // 模块下i18n/index.js文件不存在才拷贝index.js,或者forceCopyIndex=true强制拷贝 +// const i18nIndexFile = path.resolve(i18nDir, 'index.js') +// if (!fsExistsSync(i18nIndexFile) || program.forceCopyIndex) { +// copyFile(pathResolve.root('config/i18n/res/index.js'), i18nIndexFile) +// } + +// // 没有对应语言的国际化文件,就创建一个 +// const langFilePath = path.resolve(i18nDir, item + '.json') +// if (!fsExistsSync(langFilePath)) { +// jsonfile.writeFileSync(langFilePath, {}, { spaces: 2, EOL: '\n' }) +// } + +// // 读取原有的国际化文件信息,重新与新收集的国际化信息合并 +// const originData = jsonfile.readFileSync(langFilePath) || {} +// const newData = await makeNewData(dirPath, item, originData) + +// // 写文件 +// jsonfile.writeFile(langFilePath, newData, { spaces: 2, EOL: '\n' }, err => { +// if (err) return console.log('提取失败' + langFilePath + '\n' + err) +// console.log('提取完成' + langFilePath) +// }) +// } +// } + +// // 保存模块的I18n文件 +// function saveModuleI18nFile() { +// const moduleKeys = Object.keys(i18nData) +// moduleKeys.forEach(key => { +// saveI18nFile({ dirPath: key }) +// }) +// } + +// // pathResolve.root('**/eweb-demo/**/pro.properties'), +// // 默认所有模块,如果有传module参数,就只处理某个模块 +// let moduleProPath = pathResolve.root('**/eweb-**/**/pro.properties') +// if (program.module && typeof (program.module) === 'string') { +// moduleProPath = pathResolve.root('**/' + program.module + '/**/pro.properties') +// } + +// vfs.src([ +// moduleProPath +// ], { +// dot: false +// }).pipe(map((file, cb) => { +// const modulePath = path.resolve(file.path, '..') +// vfs.src([ +// path.resolve(modulePath, '**/*.+(vue|js)') // +// ], {dot: false}).pipe(map((file, cb) => { +// const contents = file.contents.toString() +// getModuleI18nData(modulePath, contents) +// cb(null) +// })).on('end', () => { +// cb(null) +// }) +// })).on('end', () => { +// normalizeI18nData() +// saveModuleI18nFile() +// }) diff --git a/i18n/res/index.js b/i18n/res/index.js new file mode 100644 index 0000000..1ace85d --- /dev/null +++ b/i18n/res/index.js @@ -0,0 +1,9 @@ +import zh from './zh.json' +import en from './en.json' +import ko from './ko.json' + +export default { + zh, + en, + ko +} diff --git a/i18n/translation.js b/i18n/translation.js new file mode 100644 index 0000000..f615901 --- /dev/null +++ b/i18n/translation.js @@ -0,0 +1,45 @@ +const { google, baidu, youdao } = require('translation.js') + +/** + * 翻译 + * https://github.com/Selection-Translator/translation.js + * youdao, baidu, google + */ +function tranlate (lang, word) { + // 默认使用Baidu + return baidu.translate({ + text: word, + from: 'zh-CN', + to: lang + }).then(result => { + return (result.result[0] || '') + }) +} + +exports.tranlate = tranlate + +/** + * 翻译列表 + * 如果其中一个翻译错误,跳过 + * 顺序执行,防止同时开太多进程,程序异常 + */ +async function tranlateArr (lang, wordArr) { + const result = [] + for (let i = 0; i < wordArr.length; i++) { + const word = wordArr[i] + const p = tranlate(lang, word).then(res => { + console.log(word, '\t' + res) + result[word] = res + }).catch(err => { + console.log(err) + }) + await p + } + return result +} + +exports.tranlateArr = tranlateArr + +// tranlateArr('en', ['您好', '哈哈']).then(res => { +// console.log(res) +// }) diff --git a/i18n/utils.js b/i18n/utils.js new file mode 100644 index 0000000..7e581c2 --- /dev/null +++ b/i18n/utils.js @@ -0,0 +1,55 @@ +'use strict' +const path = require('path') +const fs = require('fs') + +const rootPathResolve = function () { + return path.resolve.apply(path.resolve, [__dirname, '../../'].concat(Array.prototype.slice.call(arguments))) +} + +const pathResolve = { + root: rootPathResolve, +} + +exports.pathResolve = pathResolve + +// 判断文件是否存在 +function fsExistsSync(path) { + try{ + fs.accessSync(path, fs.F_OK) + }catch(e){ + return false; + } + return true; +} + +exports.fsExistsSync = fsExistsSync + +// 拷贝文件 +function copyFile(src, dist) { + fs.writeFileSync(dist, fs.readFileSync(src)); +} + +exports.copyFile = copyFile + +// 过滤出满足规则的key,规则可以是一个字符串,正则或者函数 +function filterObjByKeyRules(obj = {}, keyRules = []) { + const newObj = {} + if (keyRules.length === 0) { + return newObj + } + const keys = Object.keys(obj) + keys.forEach(key => { + for (let i = 0; i < keyRules.length; i++) { + const keyRole = keyRules[i] + if ((Object.prototype.toString.call(keyRole) === '[object RegExp]' && keyRole.test(key)) + || (Object.prototype.toString.call(keyRole) === '[object Function]' && keyRole(key)) + || keyRole === key) { + newObj[key] = obj[key] + break + } + } + }) + return newObj +} + +exports.filterObjByKeyRules = filterObjByKeyRules diff --git a/package.json b/package.json index c636b34..6ce5281 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "test:unit": "jest --clearCache && vue-cli-service test:unit", "test:ci": "npm run lint && npm run test:unit", "test": "npm run lint && npm run test:unit", - "svgo": "svgo -f src/icons/svg --config=src/icons/svgo.yml" + "svgo": "svgo -f src/icons/svg --config=src/icons/svgo.yml", + "i18n": "node i18n/index.js" }, "dependencies": { "axios": "0.18.1", @@ -40,10 +41,15 @@ "babel-eslint": "10.0.1", "babel-jest": "23.6.0", "chalk": "2.4.2", + "commander": "^3.0.1", "connect": "3.6.6", + "debug": "^4.1.1", "eslint": "5.15.3", "eslint-plugin-vue": "5.2.2", "html-webpack-plugin": "3.2.0", + "jsonfile": "^5.0.0", + "lodash.uniq": "^4.5.0", + "map-stream": "0.0.7", "mockjs": "1.0.1-beta3", "node-sass": "^4.9.0", "runjs": "^4.3.2", @@ -53,6 +59,8 @@ "serve-static": "^1.13.2", "svg-sprite-loader": "4.1.3", "svgo": "1.2.2", + "translation.js": "^0.7.9", + "vinyl-fs": "^3.0.3", "vue-template-compiler": "2.6.10" }, "engines": { diff --git a/vve-i18n-cli.config.js b/vve-i18n-cli.config.js new file mode 100644 index 0000000..101812b --- /dev/null +++ b/vve-i18n-cli.config.js @@ -0,0 +1,3 @@ +module.exports = { + outDir: 'aaaaa' +} \ No newline at end of file From 7854b51fc92e5dd12596def14a521763ecccb538 Mon Sep 17 00:00:00 2001 From: nianqin Date: Mon, 30 Sep 2019 17:57:17 +0800 Subject: [PATCH 2/7] update --- i18n/index.js | 327 +++++++++++++++++++++++--------------------- i18n/res/index.js | 4 +- i18n/translation.js | 12 +- i18n/utils.js | 10 -- 4 files changed, 181 insertions(+), 172 deletions(-) diff --git a/i18n/index.js b/i18n/index.js index 970bd88..e487424 100644 --- a/i18n/index.js +++ b/i18n/index.js @@ -20,7 +20,7 @@ const program = require('commander'); const jsonfile = require('jsonfile') const utils = require('./utils') const trans = require('./translation') // trans -const { loadConfig, findConfigUpwards } = require('./configuration') +const { loadConfig } = require('./configuration') const vfs = require('vinyl-fs') const map = require('map-stream') const path = require('path') @@ -40,24 +40,64 @@ program .option('--languages ', '需要生成的国际化语言文件,目前支持zh、en多个用逗号分割,默认全部', commaSeparatedList) .option('--config ', '配置文件路径') .option('--no-config', '忽略rc配置文件') - .option('--module [name]', '模块名称,已eweb开头') .option('-t, --translate', '是否使用翻译,默认翻译新增的文本,只能再bash环境下,window环境需要再git bash环境下执行') .option('--force-translate', '强制翻译所有的内容') .option('--translate-language ', '配合--translate使用,需要翻译的语言,目前支持en、ko,多个用逗号分割,默认全部', commaSeparatedList) + .option('--copy-index', '模块下${outDir}/index.js文件不存在才拷贝index.js') + .option('--force-copy-index', '是否强制拷贝最新index.js') .parse(process.argv); +// 模块的国际化的json文件需要被保留下的key,即使这些组件在项目中没有被引用 +// key可以是一个字符串,正则,或者是函数 +const KEEP_KEY_RULES = [ + /^G\/+/ // G/开头的会被保留 +] + +const REG_I18N = /(?:\$)?t\(['"](.+?)['"]/g + +const I18N_LANGUAGES = [ + 'zh', // 中文 + 'en', // 英文 +] + +// 翻译的基础语言,默认是用中文翻译 +const TRANSLATE_FORM_LANG = 'zh' + +// 默认所有模块,如果有传module参数,就只处理某个模块 +// let moduleProPath = path.resolve(absoluteRootDir, '**/eweb-**/**/pro.properties') +const MODULE_INDEX_RULES = [ + 'main.js' +] + +const I18N_FILE_RULES = [ + '**/*.+(vue|js)' +] + +const OUT_DIR = 'lang' + const config = { cwd: '.', - rootDir: '.', + rootDir: 'src', files: [], regulars: [], - outDir: '', - languages: ['zh', 'en'], + outDir: 'lang', + // 国际化的语言 + i18nLanguages: [ + 'zh', // 中文 + 'en', // 英文 + ], + // 模块的国际化的json文件需要被保留下的key,即使这些组件在项目中没有被引用 + // key可以是一个字符串,正则,或者是函数 + keepKeyRules: [ + /^G\/+/ // G/开头的会被保留 + ], config: '', noConfig: false, translate: false, forceTranslate: false, - translateLanguage: [] + translateLanguage: [], + copyIndex: false, + forceCopyIndex: false, } Object.assign(config, program) @@ -82,153 +122,132 @@ if (!config.noConfig) { const absoluteRootDir = path.resolve(absoluteCwd, config.rootDir); -// const pathResolve = utils.pathResolve -// const fsExistsSync = utils.fsExistsSync -// const copyFile= utils.copyFile -// const filterObjByKeyRules = utils.filterObjByKeyRules -// const tranlateArr = trans.tranlateArr - -// // 模块的国际化的json文件需要被保留下的key,即使这些组件在项目中没有被引用 -// // key可以是一个字符串,正则,或者是函数 -// const MODULE_KEEP_KEY_RULES = [ -// /^G\/+/ // G/开头的会被保留 -// ] - -// // 匹配正则 -// const regexps = { -// regI18n: /(?:\$)?t\(['"](.+?)['"]/g, -// } - -// const I18N_TYPES = [ -// 'zh', // 中文 -// 'en', // 英文 -// ] - -// const i18nData = {} -// const tmpRegData = {} - -// // 从文件中提取模块的的国际化KEY -// function getModuleI18nData (modulePath, fileContent) { -// const regI18n = new RegExp(regexps.regI18n) -// if (!i18nData[modulePath]) { -// i18nData[modulePath] = [] -// } -// while ((tmpRegData.matches = regI18n.exec(fileContent))) { -// i18nData[modulePath].push(tmpRegData.matches[1]) -// } -// } - -// // 删除重复的key,并排序方便git比对 -// function normalizeI18nData () { -// const moduleKeys = Object.keys(i18nData) -// moduleKeys.forEach(key => { -// i18nData[key] = uniq(i18nData[key]).sort() -// }) -// } - -// // 根据旧数据,生成新数据 -// async function makeNewData (key, lang, originData) { -// const newData = filterObjByKeyRules(originData, MODULE_KEEP_KEY_RULES) // 根据配置保留一些keys值,保证即使在项目中不被引用也能保存下来 +const fsExistsSync = utils.fsExistsSync +const copyFile= utils.copyFile +const filterObjByKeyRules = utils.filterObjByKeyRules +const tranlateArr = trans.tranlateArr + + + +const i18nData = {} +const tmpRegData = {} + +// 从文件中提取模块的的国际化KEY +function getModuleI18nData (modulePath, fileContent) { + const regI18n = new RegExp(REG_I18N, 'g') + if (!i18nData[modulePath]) { + i18nData[modulePath] = [] + } + while ((tmpRegData.matches = regI18n.exec(fileContent))) { + i18nData[modulePath].push(tmpRegData.matches[1]) + } +} + +// 删除重复的key,并排序方便git比对 +function normalizeI18nData () { + const moduleKeys = Object.keys(i18nData) + moduleKeys.forEach(key => { + i18nData[key] = uniq(i18nData[key]).sort() + }) +} + +// 根据旧数据,生成新数据 +async function makeNewData (key, lang, originData) { + const newData = filterObjByKeyRules(originData, KEEP_KEY_RULES) // 根据配置保留一些keys值,保证即使在项目中不被引用也能保存下来 -// let newAddDataArr = [] // 新增的数据,即在旧的翻译文件中没有出现 + let newAddDataArr = [] // 新增的数据,即在旧的翻译文件中没有出现 -// i18nData[key].forEach(key => { -// if (originData.hasOwnProperty(key)) { -// newData[key] = originData[key] -// } else { -// newData[key] = key -// newAddDataArr.push(key) -// } -// }) - -// // 中文的不翻译 -// if (program.translate && lang !== 'zh') { -// let tranlateRst = {} - -// // 如果强制翻译,则翻译所有的key -// if (program.forceTranslate) { -// newAddDataArr= Object.keys(newData) -// } - -// // 配合--translate使用,需要翻译的语言,目前支持en、ko,多个用逗号分割,默认全部 -// if (!program.translateLanguage) { -// tranlateRst = await tranlateArr(lang, newAddDataArr) -// } else if (program.translateLanguage.includes(lang)) { -// tranlateRst = await tranlateArr(lang, newAddDataArr) -// } -// Object.assign(newData, tranlateRst) -// } -// return newData -// } - -// // 保存国际化文件 -// async function saveI18nFile({ -// dirPath, -// } = {}) { -// const i18nTypes = program.language ? I18N_TYPES.filter(item => program.language.includes(item)) : I18N_TYPES - -// for (let i = 0; i < i18nTypes.length; i++) { -// const item = i18nTypes[i] -// const i18nDir = path.resolve(dirPath, 'i18n') -// if (!fsExistsSync(i18nDir)) { -// fs.mkdirSync(i18nDir); -// } - -// // 模块下i18n/index.js文件不存在才拷贝index.js,或者forceCopyIndex=true强制拷贝 -// const i18nIndexFile = path.resolve(i18nDir, 'index.js') -// if (!fsExistsSync(i18nIndexFile) || program.forceCopyIndex) { -// copyFile(pathResolve.root('config/i18n/res/index.js'), i18nIndexFile) -// } - -// // 没有对应语言的国际化文件,就创建一个 -// const langFilePath = path.resolve(i18nDir, item + '.json') -// if (!fsExistsSync(langFilePath)) { -// jsonfile.writeFileSync(langFilePath, {}, { spaces: 2, EOL: '\n' }) -// } - -// // 读取原有的国际化文件信息,重新与新收集的国际化信息合并 -// const originData = jsonfile.readFileSync(langFilePath) || {} -// const newData = await makeNewData(dirPath, item, originData) - -// // 写文件 -// jsonfile.writeFile(langFilePath, newData, { spaces: 2, EOL: '\n' }, err => { -// if (err) return console.log('提取失败' + langFilePath + '\n' + err) -// console.log('提取完成' + langFilePath) -// }) -// } -// } - -// // 保存模块的I18n文件 -// function saveModuleI18nFile() { -// const moduleKeys = Object.keys(i18nData) -// moduleKeys.forEach(key => { -// saveI18nFile({ dirPath: key }) -// }) -// } - -// // pathResolve.root('**/eweb-demo/**/pro.properties'), -// // 默认所有模块,如果有传module参数,就只处理某个模块 -// let moduleProPath = pathResolve.root('**/eweb-**/**/pro.properties') -// if (program.module && typeof (program.module) === 'string') { -// moduleProPath = pathResolve.root('**/' + program.module + '/**/pro.properties') -// } - -// vfs.src([ -// moduleProPath -// ], { -// dot: false -// }).pipe(map((file, cb) => { -// const modulePath = path.resolve(file.path, '..') -// vfs.src([ -// path.resolve(modulePath, '**/*.+(vue|js)') // -// ], {dot: false}).pipe(map((file, cb) => { -// const contents = file.contents.toString() -// getModuleI18nData(modulePath, contents) -// cb(null) -// })).on('end', () => { -// cb(null) -// }) -// })).on('end', () => { -// normalizeI18nData() -// saveModuleI18nFile() -// }) + i18nData[key].forEach(key => { + if (originData.hasOwnProperty(key)) { + newData[key] = originData[key] + } else { + newData[key] = key + newAddDataArr.push(key) + } + }) + + // 基础语言不翻译(默认中文),因为由中文翻译成其他语言 + if (config.translate && lang !== TRANSLATE_FORM_LANG) { + let tranlateRst = {} + + // 如果强制翻译,则翻译所有的key + if (config.forceTranslate) { + newAddDataArr= Object.keys(newData) + } + + // 配合--translate使用,需要翻译的语言,目前支持en、ko,多个用逗号分割,默认全部 + if (!config.translateLanguage) { + tranlateRst = await tranlateArr(TRANSLATE_FORM_LANG, lang, newAddDataArr) + } else if (config.translateLanguage.includes(lang)) { + tranlateRst = await tranlateArr(TRANSLATE_FORM_LANG, lang, newAddDataArr) + } + Object.assign(newData, tranlateRst) + } + return newData +} + +// 保存国际化文件 +async function saveI18nFile({ + dirPath, +} = {}) { + const i18nLanguages = I18N_LANGUAGES + + for (let i = 0; i < i18nLanguages.length; i++) { + const item = i18nLanguages[i] + const i18nDir = path.resolve(dirPath, OUT_DIR) + if (!fsExistsSync(i18nDir)) { + fs.mkdirSync(i18nDir); + } + + // 模块下i18n/index.js文件不存在才拷贝index.js,或者forceCopyIndex=true强制拷贝 + const i18nIndexFile = path.resolve(i18nDir, 'index.js') + if ((config.copyIndex && !fsExistsSync(i18nIndexFile)) || config.forceCopyIndex) { + copyFile(path.resolve(__dirname, 'res/index.js'), i18nIndexFile) + } + + // 没有对应语言的国际化文件,就创建一个 + const langFilePath = path.resolve(i18nDir, item + '.json') + if (!fsExistsSync(langFilePath)) { + jsonfile.writeFileSync(langFilePath, {}, { spaces: 2, EOL: '\n' }) + } + + // 读取原有的国际化文件信息,重新与新收集的国际化信息合并 + const originData = jsonfile.readFileSync(langFilePath) || {} + const newData = await makeNewData(dirPath, item, originData) + + // 写文件 + jsonfile.writeFile(langFilePath, newData, { spaces: 2, EOL: '\n' }, err => { + if (err) return console.log('提取失败' + langFilePath + '\n' + err) + console.log('提取完成' + langFilePath) + }) + } +} + +// 保存模块的I18n文件 +function saveModuleI18nFile() { + const moduleKeys = Object.keys(i18nData) + moduleKeys.forEach(key => { + saveI18nFile({ dirPath: key }) + }) +} + +vfs.src( + MODULE_FILES.map(item => path.resolve(absoluteRootDir, item)), +{ + dot: false +}).pipe(map((file, cb) => { + const modulePath = path.resolve(file.path, '..') + vfs.src(I18N_FILE_RULES.map( + item => path.resolve(modulePath, item)), + { dot: false} ) + .pipe(map((file, cb) => { + const contents = file.contents.toString() + getModuleI18nData(modulePath, contents) + cb(null) + })).on('end', () => { + cb(null) + }) +})).on('end', () => { + normalizeI18nData() + saveModuleI18nFile() +}) diff --git a/i18n/res/index.js b/i18n/res/index.js index 1ace85d..bf025c9 100644 --- a/i18n/res/index.js +++ b/i18n/res/index.js @@ -1,9 +1,7 @@ import zh from './zh.json' import en from './en.json' -import ko from './ko.json' export default { zh, - en, - ko + en } diff --git a/i18n/translation.js b/i18n/translation.js index f615901..aae6117 100644 --- a/i18n/translation.js +++ b/i18n/translation.js @@ -5,11 +5,13 @@ const { google, baidu, youdao } = require('translation.js') * https://github.com/Selection-Translator/translation.js * youdao, baidu, google */ -function tranlate (lang, word) { +function tranlate (fromLang, lang, word) { + const from = fromLang === 'zh' ? 'zh-CN' : fromLang + // 默认使用Baidu return baidu.translate({ text: word, - from: 'zh-CN', + from, to: lang }).then(result => { return (result.result[0] || '') @@ -23,11 +25,11 @@ exports.tranlate = tranlate * 如果其中一个翻译错误,跳过 * 顺序执行,防止同时开太多进程,程序异常 */ -async function tranlateArr (lang, wordArr) { +async function tranlateArr (fromLang, lang, wordArr) { const result = [] for (let i = 0; i < wordArr.length; i++) { const word = wordArr[i] - const p = tranlate(lang, word).then(res => { + const p = tranlate(fromLang, lang, word).then(res => { console.log(word, '\t' + res) result[word] = res }).catch(err => { @@ -40,6 +42,6 @@ async function tranlateArr (lang, wordArr) { exports.tranlateArr = tranlateArr -// tranlateArr('en', ['您好', '哈哈']).then(res => { +// tranlateArr('zh', 'en', ['您好', '哈哈']).then(res => { // console.log(res) // }) diff --git a/i18n/utils.js b/i18n/utils.js index 7e581c2..467c392 100644 --- a/i18n/utils.js +++ b/i18n/utils.js @@ -2,16 +2,6 @@ const path = require('path') const fs = require('fs') -const rootPathResolve = function () { - return path.resolve.apply(path.resolve, [__dirname, '../../'].concat(Array.prototype.slice.call(arguments))) -} - -const pathResolve = { - root: rootPathResolve, -} - -exports.pathResolve = pathResolve - // 判断文件是否存在 function fsExistsSync(path) { try{ From 2003385487d58bb0ceb2d54633d62521da81c067 Mon Sep 17 00:00:00 2001 From: nianqin Date: Thu, 3 Oct 2019 11:05:49 +0800 Subject: [PATCH 3/7] update --- i18n/index.js | 127 ++++++++++++++++++++++---------------------- i18n/translation.js | 12 ++--- 2 files changed, 70 insertions(+), 69 deletions(-) diff --git a/i18n/index.js b/i18n/index.js index e487424..a200737 100644 --- a/i18n/index.js +++ b/i18n/index.js @@ -33,70 +33,71 @@ function commaSeparatedList(value, split = ',') { program .version('1.0.0') - .option('--root-dir ', '提交国际化文本的根目录') - .option('--files ', '文件规则,指定哪些文件中的规则化文本需要被提取') - .option('--regulars ', '国际化文本的正则表达式,正则中第一个捕获对象当做国际化文本', commaSeparatedList) + .option('--cwd ', '工作目录') + .option('--root-dir ', '国际文本所在的根目录') + .option('--i18n-file-rules ', '匹配含有国际化文本的文件规则', commaSeparatedList) + .option('--i18n-text-rules ', '国际化文本的正则表达式,正则中第一个捕获对象当做国际化文本', commaSeparatedList) + .option('--keep-key-rules ', '模块的国际化的json文件需要被保留下的key,即使这些组件在项目中没有被引用', commaSeparatedList) .option('--out-dir ', '生成的国际化资源包的输出目录') - .option('--languages ', '需要生成的国际化语言文件,目前支持zh、en多个用逗号分割,默认全部', commaSeparatedList) - .option('--config ', '配置文件路径') - .option('--no-config', '忽略rc配置文件') - .option('-t, --translate', '是否使用翻译,默认翻译新增的文本,只能再bash环境下,window环境需要再git bash环境下执行') - .option('--force-translate', '强制翻译所有的内容') - .option('--translate-language ', '配合--translate使用,需要翻译的语言,目前支持en、ko,多个用逗号分割,默认全部', commaSeparatedList) + .option('--i18n-languages ', '需要生成的国际化语言文件,目前支持zh、en多个用逗号分割,默认全部', commaSeparatedList) + .option('--config ', '配置文件的路径,没有配置,默认路径是在${cwd}/vve-i18n-cli.config.js') + .option('--no-config', '是否取配置文件') + .option('-t, --translate', '是否翻译') + .option('--translate-from-lang', '翻译的基础语言,默认是用中文翻译') + .option('--force-translate', '是否强制翻译,即已翻译修改的内容,也重新用翻译生成') + .option('--translate-language ', '翻译的语言', commaSeparatedList) .option('--copy-index', '模块下${outDir}/index.js文件不存在才拷贝index.js') .option('--force-copy-index', '是否强制拷贝最新index.js') .parse(process.argv); -// 模块的国际化的json文件需要被保留下的key,即使这些组件在项目中没有被引用 -// key可以是一个字符串,正则,或者是函数 -const KEEP_KEY_RULES = [ - /^G\/+/ // G/开头的会被保留 -] - -const REG_I18N = /(?:\$)?t\(['"](.+?)['"]/g - -const I18N_LANGUAGES = [ - 'zh', // 中文 - 'en', // 英文 -] - -// 翻译的基础语言,默认是用中文翻译 -const TRANSLATE_FORM_LANG = 'zh' - -// 默认所有模块,如果有传module参数,就只处理某个模块 -// let moduleProPath = path.resolve(absoluteRootDir, '**/eweb-**/**/pro.properties') -const MODULE_INDEX_RULES = [ - 'main.js' -] - -const I18N_FILE_RULES = [ - '**/*.+(vue|js)' -] - -const OUT_DIR = 'lang' - const config = { + // 工作目录 cwd: '.', + // 根目录,国际文本所在的根目录 rootDir: 'src', - files: [], - regulars: [], - outDir: 'lang', - // 国际化的语言 - i18nLanguages: [ - 'zh', // 中文 - 'en', // 英文 + // 默认所有模块,如果有传module参数,就只处理某个模块 + // '**/module-**/**/index.js' + moduleIndexRules: [ + 'main.js' + ], + // 匹配含有国际化文本的文件规则 + i18nFileRules: [ + '**/*.+(vue|js)' + ], + // 国际化文本的正则表达式,正则中第一个捕获对象当做国际化文本 + i18nTextRules: [ + /(?:\$)?t\(['"](.+?)['"]/g, ], // 模块的国际化的json文件需要被保留下的key,即使这些组件在项目中没有被引用 // key可以是一个字符串,正则,或者是函数 keepKeyRules: [ /^G\/+/ // G/开头的会被保留 ], - config: '', + // 生成的国际化资源包的输出目录 + outDir: 'lang', + // 生成的国际化的语言 + i18nLanguages: [ + 'zh', // 中文 + 'en', // 英文 + ], + // 配置文件的路径,没有配置,默认路径是在${cwd}/vve-i18n-cli.config.js + config: undefined, + // 是否取配置文件 noConfig: false, + // 是否翻译 translate: false, + // 翻译的基础语言,默认是用中文翻译 + translateFromLang: 'zh', + // 是否强制翻译,即已翻译修改的内容,也重新用翻译生成 forceTranslate: false, - translateLanguage: [], + // 翻译的语言 + translateLanguage: [ + 'zh', + 'en' + ], + // 模块下${outDir}/index.js文件不存在才拷贝index.js copyIndex: false, + // 是否强制拷贝最新index.js forceCopyIndex: false, } @@ -125,21 +126,21 @@ const absoluteRootDir = path.resolve(absoluteCwd, config.rootDir); const fsExistsSync = utils.fsExistsSync const copyFile= utils.copyFile const filterObjByKeyRules = utils.filterObjByKeyRules -const tranlateArr = trans.tranlateArr - - +const translateArr = trans.translateArr const i18nData = {} const tmpRegData = {} // 从文件中提取模块的的国际化KEY function getModuleI18nData (modulePath, fileContent) { - const regI18n = new RegExp(REG_I18N, 'g') if (!i18nData[modulePath]) { i18nData[modulePath] = [] } - while ((tmpRegData.matches = regI18n.exec(fileContent))) { - i18nData[modulePath].push(tmpRegData.matches[1]) + for (let i = 0; i < config.i18nTextRules; i++) { + const regI18n = new RegExp(config.i18nTextRules[i], 'g') + while ((tmpRegData.matches = regI18n.exec(fileContent))) { + i18nData[modulePath].push(tmpRegData.matches[1]) + } } } @@ -153,7 +154,7 @@ function normalizeI18nData () { // 根据旧数据,生成新数据 async function makeNewData (key, lang, originData) { - const newData = filterObjByKeyRules(originData, KEEP_KEY_RULES) // 根据配置保留一些keys值,保证即使在项目中不被引用也能保存下来 + const newData = filterObjByKeyRules(originData, config.keepKeyRules) // 根据配置保留一些keys值,保证即使在项目中不被引用也能保存下来 let newAddDataArr = [] // 新增的数据,即在旧的翻译文件中没有出现 @@ -167,8 +168,8 @@ async function makeNewData (key, lang, originData) { }) // 基础语言不翻译(默认中文),因为由中文翻译成其他语言 - if (config.translate && lang !== TRANSLATE_FORM_LANG) { - let tranlateRst = {} + if (config.translate && lang !== config.translateFromLang) { + let translateRst = {} // 如果强制翻译,则翻译所有的key if (config.forceTranslate) { @@ -177,11 +178,11 @@ async function makeNewData (key, lang, originData) { // 配合--translate使用,需要翻译的语言,目前支持en、ko,多个用逗号分割,默认全部 if (!config.translateLanguage) { - tranlateRst = await tranlateArr(TRANSLATE_FORM_LANG, lang, newAddDataArr) + translateRst = await translateArr(config.translateFromLang, lang, newAddDataArr) } else if (config.translateLanguage.includes(lang)) { - tranlateRst = await tranlateArr(TRANSLATE_FORM_LANG, lang, newAddDataArr) + translateRst = await translateArr(config.translateFromLang, lang, newAddDataArr) } - Object.assign(newData, tranlateRst) + Object.assign(newData, translateRst) } return newData } @@ -190,11 +191,11 @@ async function makeNewData (key, lang, originData) { async function saveI18nFile({ dirPath, } = {}) { - const i18nLanguages = I18N_LANGUAGES + const i18nLanguages = config.i18nLanguages for (let i = 0; i < i18nLanguages.length; i++) { const item = i18nLanguages[i] - const i18nDir = path.resolve(dirPath, OUT_DIR) + const i18nDir = path.resolve(dirPath, config.outDir) if (!fsExistsSync(i18nDir)) { fs.mkdirSync(i18nDir); } @@ -232,12 +233,12 @@ function saveModuleI18nFile() { } vfs.src( - MODULE_FILES.map(item => path.resolve(absoluteRootDir, item)), + config.moduleIndexRules.map(item => path.resolve(absoluteRootDir, item)), { dot: false }).pipe(map((file, cb) => { - const modulePath = path.resolve(file.path, '..') - vfs.src(I18N_FILE_RULES.map( + const modulePath = path.dirname(file.path) + vfs.src(config.i18nFileRules.map( item => path.resolve(modulePath, item)), { dot: false} ) .pipe(map((file, cb) => { diff --git a/i18n/translation.js b/i18n/translation.js index aae6117..79a8d6c 100644 --- a/i18n/translation.js +++ b/i18n/translation.js @@ -5,7 +5,7 @@ const { google, baidu, youdao } = require('translation.js') * https://github.com/Selection-Translator/translation.js * youdao, baidu, google */ -function tranlate (fromLang, lang, word) { +function translate (fromLang, lang, word) { const from = fromLang === 'zh' ? 'zh-CN' : fromLang // 默认使用Baidu @@ -18,18 +18,18 @@ function tranlate (fromLang, lang, word) { }) } -exports.tranlate = tranlate +exports.translate = translate /** * 翻译列表 * 如果其中一个翻译错误,跳过 * 顺序执行,防止同时开太多进程,程序异常 */ -async function tranlateArr (fromLang, lang, wordArr) { +async function translateArr (fromLang, lang, wordArr) { const result = [] for (let i = 0; i < wordArr.length; i++) { const word = wordArr[i] - const p = tranlate(fromLang, lang, word).then(res => { + const p = translate(fromLang, lang, word).then(res => { console.log(word, '\t' + res) result[word] = res }).catch(err => { @@ -40,8 +40,8 @@ async function tranlateArr (fromLang, lang, wordArr) { return result } -exports.tranlateArr = tranlateArr +exports.translateArr = translateArr -// tranlateArr('zh', 'en', ['您好', '哈哈']).then(res => { +// translateArr('zh', 'en', ['您好', '哈哈']).then(res => { // console.log(res) // }) From 7e3b790e3ec4da174f4a631824dc4f7f55b66376 Mon Sep 17 00:00:00 2001 From: nianqin Date: Thu, 3 Oct 2019 11:50:11 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=9B=BD=E9=99=85?= =?UTF-8?q?=E5=8C=96=E7=BF=BB=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- i18n/index.js | 7 ++++--- vve-i18n-cli.config.js | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/i18n/index.js b/i18n/index.js index a200737..bfbcdaa 100644 --- a/i18n/index.js +++ b/i18n/index.js @@ -66,7 +66,7 @@ const config = { ], // 国际化文本的正则表达式,正则中第一个捕获对象当做国际化文本 i18nTextRules: [ - /(?:\$)?t\(['"](.+?)['"]/g, + /(?:[\$.])t\(['"](.+?)['"]/g, ], // 模块的国际化的json文件需要被保留下的key,即使这些组件在项目中没有被引用 // key可以是一个字符串,正则,或者是函数 @@ -136,9 +136,10 @@ function getModuleI18nData (modulePath, fileContent) { if (!i18nData[modulePath]) { i18nData[modulePath] = [] } - for (let i = 0; i < config.i18nTextRules; i++) { + for (let i = 0; i < config.i18nTextRules.length; i++) { const regI18n = new RegExp(config.i18nTextRules[i], 'g') while ((tmpRegData.matches = regI18n.exec(fileContent))) { + console.log(tmpRegData.matches) i18nData[modulePath].push(tmpRegData.matches[1]) } } @@ -231,13 +232,13 @@ function saveModuleI18nFile() { saveI18nFile({ dirPath: key }) }) } - vfs.src( config.moduleIndexRules.map(item => path.resolve(absoluteRootDir, item)), { dot: false }).pipe(map((file, cb) => { const modulePath = path.dirname(file.path) + vfs.src(config.i18nFileRules.map( item => path.resolve(modulePath, item)), { dot: false} ) diff --git a/vve-i18n-cli.config.js b/vve-i18n-cli.config.js index 101812b..055a233 100644 --- a/vve-i18n-cli.config.js +++ b/vve-i18n-cli.config.js @@ -1,3 +1,3 @@ module.exports = { - outDir: 'aaaaa' + outDir: 'lang' } \ No newline at end of file From 49ea57373dd47fd3eb813317c3fc9a7670d21322 Mon Sep 17 00:00:00 2001 From: nianqin Date: Thu, 3 Oct 2019 16:59:00 +0800 Subject: [PATCH 5/7] update --- i18n/index.js | 5 ++--- i18n/res/index.js | 25 ++++++++++++++++++++----- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/i18n/index.js b/i18n/index.js index bfbcdaa..a48799d 100644 --- a/i18n/index.js +++ b/i18n/index.js @@ -39,7 +39,7 @@ program .option('--i18n-text-rules ', '国际化文本的正则表达式,正则中第一个捕获对象当做国际化文本', commaSeparatedList) .option('--keep-key-rules ', '模块的国际化的json文件需要被保留下的key,即使这些组件在项目中没有被引用', commaSeparatedList) .option('--out-dir ', '生成的国际化资源包的输出目录') - .option('--i18n-languages ', '需要生成的国际化语言文件,目前支持zh、en多个用逗号分割,默认全部', commaSeparatedList) + .option('-l, --i18n-languages ', '需要生成的国际化语言文件,目前支持zh、en多个用逗号分割,默认全部', commaSeparatedList) .option('--config ', '配置文件的路径,没有配置,默认路径是在${cwd}/vve-i18n-cli.config.js') .option('--no-config', '是否取配置文件') .option('-t, --translate', '是否翻译') @@ -124,7 +124,6 @@ if (!config.noConfig) { const absoluteRootDir = path.resolve(absoluteCwd, config.rootDir); const fsExistsSync = utils.fsExistsSync -const copyFile= utils.copyFile const filterObjByKeyRules = utils.filterObjByKeyRules const translateArr = trans.translateArr @@ -204,7 +203,7 @@ async function saveI18nFile({ // 模块下i18n/index.js文件不存在才拷贝index.js,或者forceCopyIndex=true强制拷贝 const i18nIndexFile = path.resolve(i18nDir, 'index.js') if ((config.copyIndex && !fsExistsSync(i18nIndexFile)) || config.forceCopyIndex) { - copyFile(path.resolve(__dirname, 'res/index.js'), i18nIndexFile) + fs.writeFileSync(i18nIndexFile, require('./res/index.js')(i18nLanguages)) } // 没有对应语言的国际化文件,就创建一个 diff --git a/i18n/res/index.js b/i18n/res/index.js index bf025c9..ff15212 100644 --- a/i18n/res/index.js +++ b/i18n/res/index.js @@ -1,7 +1,22 @@ -import zh from './zh.json' -import en from './en.json' -export default { - zh, - en + +const camelizeRE = /[-_](\w)/g +function camelize (str) { + return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '') +} + +module.exports = function makeIndex (langArr = []) { + const importArr = langArr.map((lang, index) => { + return `import ${camelize(lang)} from './${lang}.json'` + }) + + const exportArr = langArr.map((lang, index) => { + const cLang = camelize(lang) + const langItem = cLang === lang + ? ` ${lang}` + : ` '${lang}': ${cLang}` + return langItem + }) + + return `${importArr.join('\n')}${importArr.length ? '\n\n' : ''}export default {${exportArr.length ? '\n' : ''}${exportArr.join(',\n')}${exportArr.length ? '\n' : ''}}\n` } From 94d09c4b6acdc45b85ba37049c67f4213d163b2e Mon Sep 17 00:00:00 2001 From: nianqin Date: Thu, 3 Oct 2019 17:11:01 +0800 Subject: [PATCH 6/7] update --- i18n/index.js | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/i18n/index.js b/i18n/index.js index a48799d..893f1df 100644 --- a/i18n/index.js +++ b/i18n/index.js @@ -1,20 +1,3 @@ -/** - * 根据配置的根文件夹(rootDir) - * 根据配置的规则数组,文件匹配规则 - * 根据配置的提出国际化的正则数组,比如:/(?:\$)?t\(['"](.+?)['"]/g - * 根据配置的输出目录,输出国际化 - * 根据配置的输出国际化语言列表 - * 配置文件目录 - * 配置文件不生效 - */ -/** - * 示例命令 - * 【抽取全部模块】npm run i18n - * 【抽离某个模块】npm run i18n -- -m eweb-setting-safe - * 【翻译】npm run i18n -- -m eweb-setting-safe -t - * 【强制翻译】npm run i18n -- -m eweb-setting-safe -t -F - * 【强制翻译某种语言】npm run i18n -- -m eweb-setting-safe -t -F -L en - */ 'use strict' const program = require('commander'); const jsonfile = require('jsonfile') @@ -138,7 +121,6 @@ function getModuleI18nData (modulePath, fileContent) { for (let i = 0; i < config.i18nTextRules.length; i++) { const regI18n = new RegExp(config.i18nTextRules[i], 'g') while ((tmpRegData.matches = regI18n.exec(fileContent))) { - console.log(tmpRegData.matches) i18nData[modulePath].push(tmpRegData.matches[1]) } } From c07e9388b72afeeec21a13e65946349396f1c694 Mon Sep 17 00:00:00 2001 From: nianqin Date: Thu, 3 Oct 2019 17:13:09 +0800 Subject: [PATCH 7/7] update --- i18n/configuration.js | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/i18n/configuration.js b/i18n/configuration.js index 80e315f..395c4db 100644 --- a/i18n/configuration.js +++ b/i18n/configuration.js @@ -1,33 +1,8 @@ const buildDebug = require("debug"); const path = require("path"); -const fs = require("fs"); const debug = buildDebug("files:configuration"); -const CONFIG_JS_FILENAME = "vve-i18n-cli.config.js"; - -function findConfigUpwards() { - let dirname = rootDir; - while (true) { - if (fs.existsSync(path.join(dirname, CONFIG_JS_FILENAME))) { - return dirname; - } - const nextDir = path.dirname(dirname); - if (dirname === nextDir) break; - dirname = nextDir; - } - return null; -} - -function findConfig (dirname) { - const filepath = path.resolve(dirname, CONFIG_JS_FILENAME); - const conf = readConfig(filepath); - if (conf) { - debug("Found root config %o in %o.", CONFIG_JS_FILENAME, dirname); - } - return conf; -} - function loadConfig (filepath) { try { const conf = readConfig(filepath) @@ -57,7 +32,6 @@ function readConfig(filepath) { } module.exports = { - findConfigUpwards, loadConfig, readConfig, } \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy