123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- var fs = require('fs');
- var path = require('path');
- var Parser = require('i18next-scanner').Parser;
- const zhExclude = require('./i18n_exclude_zh.json');
- const enExclude = require('./i18n_exclude_en.json');
- const ruExclude = require('./i18n_exclude_ru.json');
- const esExclude = require('./i18n_exclude_es.json');
- const existedEn = require('../src/i18n/en.json');
- const existedRu = require('../src/i18n/ru.json');
- const existedEs = require('../src/i18n/es.json');
- var directoryPaths = ['src/pages', 'src/components'];
- var existedEnTranslation = existedEn.translation;
- var existedRuTranslation = existedRu.translation;
- var existedEsTranslation = existedEs.translation;
- var zhHandler = function (key, parser) {
- // 读取特殊规则,如果有特殊规则,就从特殊规则取值
- if (zhExclude[key]) {
- parser.set(key, zhExclude[key]);
- return;
- }
- parser.set(key, key);
- };
- var otherLanguageHandler = function (key, parser, excludeValue, existedValue) {
- // 读取特殊规则,如果有特殊规则,就从特殊规则取值
- if (excludeValue[key]) {
- parser.set(key, excludeValue[key]);
- return;
- }
- // 如果已经有翻译,就从已有翻译取值
- if (existedValue[key]) {
- parser.set(key, existedValue[key]);
- return;
- }
- var defaultValue = '__TRANSLATION__';
- parser.set(key, defaultValue);
- };
- var zhParser = new Parser();
- var enParser = new Parser();
- var ruParser = new Parser();
- var esParser = new Parser();
- function processDirectory(directoryPath) {
- const files = fs.readdirSync(directoryPath);
- for (let file of files) {
- let fullPath = path.join(directoryPath, file);
- if (fs.statSync(fullPath).isDirectory()) {
- processDirectory(fullPath);
- } else {
- let content = fs.readFileSync(fullPath, 'utf-8');
- zhParser.parseFuncFromString(content, { list: ['t'] }, (key) =>
- zhHandler(key, zhParser),
- );
- enParser.parseFuncFromString(content, { list: ['t'] }, (key) =>
- otherLanguageHandler(key, enParser, enExclude, existedEnTranslation),
- );
- ruParser.parseFuncFromString(content, { list: ['t'] }, (key) =>
- otherLanguageHandler(key, ruParser, ruExclude, existedRuTranslation),
- );
- esParser.parseFuncFromString(content, { list: ['t'] }, (key) =>
- otherLanguageHandler(key, esParser, esExclude, existedEsTranslation),
- );
- }
- }
- }
- // 处理所有目录
- for (let directoryPath of directoryPaths) {
- processDirectory(directoryPath);
- }
- var zhTranslations = zhParser.get({ sort: true });
- var enTranslations = enParser.get({ sort: true });
- var ruTranslations = ruParser.get({ sort: true });
- var esTranslations = esParser.get({ sort: true });
- // 写入翻译到目标json文件
- fs.writeFileSync(
- 'src/i18n/zh.json',
- JSON.stringify(zhTranslations.en, null, 2),
- );
- fs.writeFileSync(
- 'src/i18n/en.json',
- JSON.stringify(enTranslations.en, null, 2),
- );
- fs.writeFileSync(
- 'src/i18n/ru.json',
- JSON.stringify(ruTranslations.en, null, 2),
- );
- fs.writeFileSync(
- 'src/i18n/es.json',
- JSON.stringify(esTranslations.en, null, 2),
- );
- var fileTranslations = Object.keys(zhTranslations.en.translation).length;
- console.log('✅ 翻译查找完成,共找到 ' + fileTranslations + ' 个翻译项');
|