fis3-parser-react-i18n
Advanced tools
Comparing version 0.0.1 to 0.0.2
60
index.js
@@ -15,5 +15,32 @@ var parse = require('@babel/parser').parse; | ||
var zhExt = /[\u4e00-\u9fa5]+/; | ||
var baseLocale = null; | ||
function noLocale(value, id, relativePath) { | ||
if (!baseLocale) { | ||
return false; | ||
} | ||
if (!baseLocale[id]) { | ||
console.log('\x1B[31m%s\x1B[0m', "\n\u3010i18n\u3011\u5728\u8BED\u6599\u5305\u4E2D\u672A\u53D1\u73B0\u4EE5\u4E0B\u5B57\u6BB5 ".concat(relativePath, "\uFF1A").concat(value, " \u8BF7\u6267\u884Ci18n update\n")); | ||
return true; | ||
} | ||
return false; | ||
} | ||
function readChinese(languages) { | ||
var chinesePath = languages.find(function (language) { return language.name === 'zh-CN'; }).path; | ||
var code = fs.readFileSync(p.resolve(process.cwd(), chinesePath, 'zh-CN.ts'), 'utf8'); | ||
var ast = parse(code, { | ||
sourceType: 'module', | ||
plugins: ['typescript'] | ||
}); | ||
traverse(ast, { | ||
ObjectProperty: function (path) { | ||
var key = path.node.key.value || path.node.key.name; | ||
var value = path.node.value.value || path.node.value.name; | ||
baseLocale[key] = value; | ||
} | ||
}); | ||
} | ||
module.exports = function (content, file, options) { | ||
var filePath = file.realpath; | ||
var _a = options.ignore || {}, sameGit = _a.sameGit, list = _a.list; | ||
var languages = options.languages; | ||
var ignoreList = list || []; | ||
@@ -49,2 +76,6 @@ if (sameGit) { | ||
var i18nFnName = importLocal; | ||
if (!baseLocale) { | ||
baseLocale = {}; | ||
readChinese(languages); | ||
} | ||
var visitor = { | ||
@@ -83,2 +114,5 @@ Program: { | ||
var id = hash(value); | ||
if (noLocale(value, id, relativePath)) { | ||
return; | ||
} | ||
var origininalValue = path.node.value; | ||
@@ -135,2 +169,5 @@ var trimmedValue = origininalValue.trim(); | ||
var id = hash(value); | ||
if (noLocale(value, id, relativePath)) { | ||
return; | ||
} | ||
path.replaceWith(t.callExpression(t.identifier(importLocal), [ | ||
@@ -157,2 +194,5 @@ t.stringLiteral(id), | ||
var id = hash(value); | ||
if (noLocale(value, id, relativePath)) { | ||
return; | ||
} | ||
if (t.isJSXAttribute(path.parent)) { | ||
@@ -169,10 +209,14 @@ path.replaceWith(t.jsxExpressionContainer(t.callExpression(t.identifier(importLocal), [t.stringLiteral(id)]))); | ||
traverse(ast, visitor); | ||
var codeRes = generate(ast, { | ||
jsescOption: { | ||
minimal: true | ||
}, | ||
decoratorsBeforeExport: true | ||
}, content); | ||
console.log("i18n: ".concat(relativePath)); | ||
return codeRes.code; | ||
if (needI18n) { | ||
var codeRes = generate(ast, { | ||
jsescOption: { | ||
minimal: true | ||
}, | ||
decoratorsBeforeExport: true | ||
}, content); | ||
return codeRes.code; | ||
} | ||
else { | ||
return content; | ||
} | ||
}; |
{ | ||
"name": "fis3-parser-react-i18n", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "fis3处理react国际化插件", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
9059
216