This commit is contained in:
2026-04-25 16:36:34 +08:00
commit db90e7579b
1876 changed files with 189777 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
const fs = require('fs-extra');
const path = require('path');
const { crc32 } = require('crc');
const esbuild = require('esbuild');
console.log('Scanning Translation in src folder...');
// 扫描语言
const lngs = ['zh-CN', 'en-US'];
module.exports = {
input: [
'web/**/*.{ts,tsx}',
'shared/**/*.{ts,tsx}',
// 'src/shared/i18n/__internal__/__scan__.ts',
// Use ! to filter out files or directories
'!src/**/*.spec.{js,jsx,ts,tsx}',
'!web/e2e/**/*.test.{ts,tsx}',
// '!src/shared/i18n/**',
'!**/node_modules/**',
],
output: './', //输出目录
options: {
debug: false,
sort: true,
func: false,
trans: false,
lngs,
defaultLng: 'zh-CN',
resource: {
loadPath: './src/shared/i18n/langs/{{lng}}/{{ns}}.json', //输入路径
savePath: './src/shared/i18n/langs/{{lng}}/{{ns}}.json', //输出路径
jsonIndent: 2,
lineEnding: '\n',
endWithEmptyTrans: true,
},
removeUnusedKeys: true,
nsSeparator: false, // namespace separator
keySeparator: false, // key separator
interpolation: {
prefix: '{{',
suffix: '}}',
},
},
transform: async function customTransform(file, enc, done) {
//自己通过该函数来加工key或value
'use strict';
const parser = this.parser;
const content = await fs.readFile(file.path, enc);
parser.parseFuncFromString(
content,
{ list: ['lang', 't'] },
(key, options) => {
options.defaultValue = key;
const hashKey = `k${crc32(key).toString(16)}`;
parser.set(hashKey, options);
}
);
// 如果是 tsx 文件则使用esbuild转换成jsx后再输入
if (path.extname(file.path) === '.tsx') {
const { code } = await esbuild.transform(content, {
jsx: 'preserve',
loader: 'tsx',
});
parser.parseTransFromString(
code,
{ component: 'Trans', i18nKey: 'i18nKey' },
(key, options) => {
/**
* 处理scanner与react-i18next算法不一致导致的问题
* Reference: https://github.com/i18next/i18next-scanner/issues/125
*/
let sentence = options.defaultValue;
// remove <Tag> surrounding interopations to match i18next simpilied result
// @see https://github.com/i18next/react-i18next/blob/master/CHANGELOG.md#800
sentence = sentence.replace(/<(\d+)>{{(\w+)}}<\/\1>/g, '{{$2}}');
sentence = sentence.replace(/\s+/g, ' ');
options.defaultValue = sentence;
const hashKey = `k${crc32(key || sentence).toString(16)}`;
parser.set(hashKey, options);
}
);
}
done();
},
};

View File

@@ -0,0 +1,56 @@
/**
* 将翻译文件集合到dist目录
*/
const path = require('path');
const fs = require('fs-extra');
const scannerConfig = require('../config/i18next-scanner.config');
// const utils = require('./utils');
const langs = scannerConfig.options.lngs;
const distDir = path.resolve(__dirname, '../../');
// const plugins = utils.getPluginDirs();
const filepath = [
path.resolve(__dirname, '../../shared/i18n/langs/{{lang}}/translation.json'),
// ...plugins.map((plugin) =>
// path.resolve(
// __dirname,
// `../../src/plugins/${plugin}/i18n/{{lang}}/translation.json`
// )
// ),
];
console.log('Build locales:', langs);
for (const lang of langs) {
Promise.all(
filepath
.map((p) => {
return p.replace('{{lang}}', lang);
})
.map((p) => fs.readJSON(p))
)
.then((jsons) => {
let res = {};
for (const json of jsons) {
res = {
...res,
...json,
};
}
return res;
})
.then((trans) => {
const filePath = path.resolve(
distDir,
`./locales/${lang}/translation.json`
);
return fs.ensureFile(filePath).then(() => {
fs.writeJSON(filePath, trans, {
spaces: 2,
});
});
})
.then(() => {
console.log(`Build Translation [${lang}] Success!`);
});
}

View File

@@ -0,0 +1,107 @@
const vfs = require('vinyl-fs');
const fs = require('fs-extra');
const sort = require('gulp-sort');
const path = require('path');
const scanner = require('i18next-scanner');
// const utils = require('./utils');
const _ = require('lodash');
const { crc32 } = require('crc');
const scannerConfig = require('../config/i18next-scanner.config');
const output = path.resolve(__dirname, '../../');
const originJson = fs.readJsonSync(
path.resolve(output, './shared/i18n/langs/zh-CN/translation.json')
);
// For main
const mainstream = vfs
.src([
...scannerConfig.input,
// '!src/plugins/**'
])
.pipe(sort()) // Sort files in stream by path
.pipe(
scanner(
{
...scannerConfig.options,
resource: {
...scannerConfig.options.resource,
loadPath: './shared/i18n/langs/{{lng}}/{{ns}}.json', //输入路径
savePath: './shared/i18n/langs/{{lng}}/{{ns}}.json', //输出路径
},
},
scannerConfig.transform
)
)
.pipe(vfs.dest(path.resolve(__dirname, output)));
mainstream.on('finish', () => {
// 主流完毕后进行插件生成
// console.log('主项目翻译生成完毕, 开始进行子项目翻译生成...');
const transJson = fs.readJsonSync(
path.resolve(output, './shared/i18n/langs/zh-CN/translation.json')
);
const originKeys = Object.keys(originJson);
const transKeys = Object.keys(transJson);
const addedNum = _.without(transKeys, ...originKeys).length;
const deletededNum = _.without(originKeys, ...transKeys).length;
console.log(
`主项目翻译生成完毕! 新增翻译 ${addedNum} 条, 移除翻译 ${deletededNum}`
);
// For plugins
// utils.getPluginDirs().forEach((plugin) => {
// const stream = vfs
// .src([`src/plugins/${plugin}/**/*.{ts,tsx}`])
// .pipe(sort()) // Sort files in stream by path
// .pipe(
// scanner(
// {
// ...scannerConfig.options,
// resource: {
// ...scannerConfig.options.resource,
// loadPath: `./src/plugins/${plugin}/i18n/{{lng}}/{{ns}}.json`, //输入路径
// savePath: `./src/plugins/${plugin}/i18n/{{lng}}/{{ns}}.json`, //输出路径
// },
// },
// scannerConfig.transform
// )
// )
// .pipe(vfs.dest(path.resolve(__dirname, output)));
// stream.on('finish', () => {
// let sharedKeyNum = 0;
// scannerConfig.options.lngs.forEach((lang) => {
// const mainTrans = fs.readJSONSync(
// path.resolve(
// output,
// `./src/shared/i18n/langs/${lang}/translation.json`
// )
// );
// const pluginTransPath = path.resolve(
// output,
// `./src/plugins/${plugin}/i18n/${lang}/translation.json`
// );
// const pluginTrans = fs.readJSONSync(pluginTransPath);
// const sharedTransKey = _.intersection(
// Object.keys(pluginTrans),
// Object.keys(mainTrans)
// );
// sharedKeyNum = sharedTransKey.length;
// sharedTransKey.forEach((key) => {
// delete pluginTrans[key];
// });
// fs.writeJsonSync(pluginTransPath, pluginTrans, {
// spaces: 2,
// });
// });
// console.log(
// `子项目 [${plugin}] 翻译生成完毕, 自动移除与主项目共享的翻译 ${sharedKeyNum} 条`
// );
// });
// });
});