forked from TencentBlueKing/bk-user
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 国际化 & eslint (TencentBlueKing#1691)
- Loading branch information
Showing
39 changed files
with
7,839 additions
and
7,783 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const { resolve } = require('path'); | ||
const fs = require('fs'); | ||
const { extractI18NReport, readVueFiles, extractI18NItemsFromVueFiles, readLanguageFiles } = require('vue-i18n-extract'); | ||
const Dot = require('dot-object'); | ||
|
||
const dot = new Dot('.'); | ||
const config = { | ||
filePath: './src/**/*.?(js|vue|ts|tsx|jsx|html)', | ||
i18nPath: './src/language/**/*.?(json)' | ||
} | ||
|
||
const vueFiles = readVueFiles(resolve(process.cwd(), config.filePath)); | ||
const I18NItems = extractI18NItemsFromVueFiles(vueFiles); | ||
|
||
const languageFiles = readLanguageFiles(resolve(process.cwd(), config.i18nPath)); | ||
const I18NLanguage = languageFiles.reduce((pre, item) => { | ||
const content = JSON.parse(item.content) | ||
const flatContent = dot.dot(content); | ||
// const lang = item.fileName.replace(/^.*[\\\/]/, '') | ||
pre[item.fileName] = Object.keys(flatContent).map(key => ({ | ||
file: item.fileName, | ||
path: key | ||
})) | ||
return pre | ||
}, {}); | ||
|
||
const report = extractI18NReport(I18NItems, I18NLanguage); | ||
|
||
// 添加未翻译文案 | ||
languageFiles.forEach(languageFile => { | ||
const content = JSON.parse(languageFile.content); | ||
report.missingKeys.forEach(item => { | ||
if (item.language !== languageFile.fileName) return | ||
|
||
const defaultValue = languageFile.fileName.indexOf('zh') > -1 ? item.path : '' | ||
dot.str(item.path, defaultValue, content) | ||
}) | ||
languageFile.content = JSON.stringify(content) // 保留结果 | ||
|
||
fs.writeFileSync(languageFile.path, JSON.stringify(content, null, 2)) | ||
}); | ||
// 删除未使用文案 | ||
languageFiles.forEach(languageFile => { | ||
const content = JSON.parse(languageFile.content); | ||
report.unusedKeys.forEach(item => { | ||
if (item.language !== languageFile.fileName) return | ||
|
||
dot.delete(item.path, content) | ||
}) | ||
languageFile.content = JSON.stringify(content) // 保留结果 | ||
fs.writeFileSync(languageFile.path, JSON.stringify(content, null, 2)) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.