Commit a71fe96f authored by Marcus's avatar Marcus

翻译器

parent 5ea56b38
const fs = require('fs'); const fs = require('fs');
const root = "./src/views" let root
let allFiles = [] let allFiles = []
function getFiles(dir){ function getFiles(dir){
let files = fs.readdirSync(dir) if(fs.lstatSync(dir).isDirectory()){
files.forEach(file => { let files = fs.readdirSync(dir)
let path = dir + '/' + file files.forEach(file => {
if(fs.lstatSync(path).isDirectory()){ let path = dir + '/' + file
getFiles(path) if(fs.lstatSync(path).isDirectory()){
}else if(path.split('.').reverse()[0] === 'vue') { getFiles(path)
allFiles.push(path) }else if(path.split('.').reverse()[0] === 'vue') {
} allFiles.push(path)
}) }
})
} else {
allFiles.push(dir)
}
}
root = process.argv.slice(2)[0]
if (!root) {
return console.log('需要带上文件或目录地址,例如:node .\\translator.js .\\src\\views\\ecw\\order\\warehousing\\')
} }
getFiles(root) getFiles(root)
// allFiles = [
// // 'src/views/system/user/index.vue', console.log('' + allFiles.length + '个文件')
// 'src/views/bpm/model/index.vue'
// // 'src/views/ecw/financial/receiptDetail.vue'
// ]
console.log(allFiles)
allFiles.forEach(file => { allFiles.forEach(file => {
let count = 0
setTimeout(() => {
let data = fs.readFileSync(file, {encoding: 'utf-8'}) let data = fs.readFileSync(file, {encoding: 'utf-8'})
console.log('开始翻译:' + file)
// 找到 <template>([\w\W]*)<\/template> // 找到 <template>([\w\W]*)<\/template>
data = data.replace(/<template>([\w\W]*)<\/template>/g, (template) => { data = data.replace(/<template>([\w\W]*)<\/template>/g, (template) => {
// 匹配标签属性 placeholder="请输入部门名称" 并替换为 :placeholder="$t('请输入部门名称')" // 匹配标签属性 placeholder="请输入部门名称" 并替换为 :placeholder="$t('请输入部门名称')"
return template.replace(/ \w+="[^\x00-\xff]+"/g, (word) => { return template.replace(/ \w+="[^\x00-\xff]+"/g, (word) => {
count++
const wordList = word.split('=') const wordList = word.split('=')
return ' :' + wordList[0].substring(1) + '="$t(\'' + wordList[1].substring(1, wordList[1].length - 1) + '\')"' return ' :' + wordList[0].substring(1) + '="$t(\'' + wordList[1].substring(1, wordList[1].length - 1) + '\')"'
}) })
// 匹配标签值 <el-button>取 消</el-button> 并替换为 <el-button>{{ $t('取 消') }}</el-button> // 匹配标签值 <el-button>取 消</el-button> 并替换为 <el-button>{{ $t('取 消') }}</el-button>
.replace(/>(\s*[^\x00-\xff]+\s*)+</g, (word) => { .replace(/>(\s*[^\x00-\xff]+\s*)+</g, (word) => {
count++
return '>{{ $t(\'' + word.substring(1, word.length - 1).trim() + '\') }}<' return '>{{ $t(\'' + word.substring(1, word.length - 1).trim() + '\') }}<'
}) })
}) })
...@@ -50,27 +56,25 @@ allFiles.forEach(file => { ...@@ -50,27 +56,25 @@ allFiles.forEach(file => {
// 单引号字符串 \'(\s*[^\x00-\xff]+\s*)+\' ,其中(?<!([(=]))和(?!\))表示前后没有括号和前=用于防止重复转换 // 单引号字符串 \'(\s*[^\x00-\xff]+\s*)+\' ,其中(?<!([(=]))和(?!\))表示前后没有括号和前=用于防止重复转换
let s = script.replace(/(?<!([(=]))(?<!(\$t\())'[^\x00-\xff]+'(?!\))/g, s => { let s = script.replace(/(?<!([(=]))(?<!(\$t\())'[^\x00-\xff]+'(?!\))/g, s => {
count++
return 'this.$t(' + s + ')' return 'this.$t(' + s + ')'
}) })
// 双引号字符串 \"(\s*[^\x00-\xff]+\s*)+\" // 双引号字符串 \"(\s*[^\x00-\xff]+\s*)+\"
s = s.replace(/(?<!([(=]))"[^\x00-\xff]+"(?!\))/g, s => { s = s.replace(/(?<!([(=]))"[^\x00-\xff]+"(?!\))/g, s => {
count++
return 'this.$t(' + s + ')' return 'this.$t(' + s + ')'
}) })
// `字符串 \`(\s*[^\x00-\xff]+\s*)+\` // `字符串 \`(\s*[^\x00-\xff]+\s*)+\`
s = s.replace(/(?<!([(=]))`[^\x00-\xff]+`(?!\))/g, s => { s = s.replace(/(?<!([(=]))`[^\x00-\xff]+`(?!\))/g, s => {
count++
return 'this.$t(' + s + ')' return 'this.$t(' + s + ')'
}) })
return s return s
}) })
console.log('准备保存:' + file)
fs.writeFileSync(file, data) fs.writeFileSync(file, data)
console.log('已翻译:' + file) console.log('[' + file + ']修改' + count + '')
}, 0)
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment