From 2e60db80df5c157969d6e48b8e7d0ceaa91de8da Mon Sep 17 00:00:00 2001 From: icarus Date: Thu, 23 Oct 2025 12:43:45 +0800 Subject: [PATCH] feat(i18n): add i18next scanner configuration and script Add i18next-scanner configuration file to automate translation key extraction Include new 'i18n:scan' script in package.json to run the scanner Update tsconfig and oxlintrc to include the new config file --- i18next-scanner.config.js | 61 +++++++++++++++++++++++++++++++++++++++ package.json | 1 + 2 files changed, 62 insertions(+) create mode 100644 i18next-scanner.config.js diff --git a/i18next-scanner.config.js b/i18next-scanner.config.js new file mode 100644 index 0000000000..ed88681fe7 --- /dev/null +++ b/i18next-scanner.config.js @@ -0,0 +1,61 @@ +const fs = require('fs') +const chalk = require('chalk') + +module.exports = { + compatibilityJSON: 'v4', + input: [ + 'src/**/*.{ts,tsx}', + // Use ! to filter out files or directories + '!**/node_modules/**' + ], + output: './', + options: { + debug: true, + func: { + list: ['i18next.t', 'i18n.t'], + extensions: ['.ts', '.tsx'] + }, + lngs: ['en-us', 'zh-cn', 'zh-tw'], + ns: ['locale'], + defaultLng: 'en-us', + defaultNs: 'locale', + defaultValue: '__STRING_NOT_TRANSLATED__', + resource: { + loadPath: 'src/renderer/src/i18n/locales/{{lng}}.json', + savePath: 'src/renderer/src/i18n/locales/{{lng}}.json', + jsonIndent: 2, + lineEnding: '\n' + }, + nsSeparator: false, // namespace separator + keySeparator: '.', // key separator + interpolation: { + prefix: '{{', + suffix: '}}' + }, + metadata: {}, + allowDynamicKeys: false + }, + transform: function customTransform(file, enc, done) { + 'use strict' + const parser = this.parser + const content = fs.readFileSync(file.path, enc) + let count = 0 + + parser.parseFuncFromString(content, { list: ['i18next._', 'i18next.__'] }, (key, options) => { + parser.set( + key, + Object.assign({}, options, { + nsSeparator: false, + keySeparator: false + }) + ) + ++count + }) + + if (count > 0) { + console.log(`i18next-scanner: count=${chalk.cyan(count)}, file=${chalk.yellow(JSON.stringify(file.relative))}`) + } + + done() + } +} diff --git a/package.json b/package.json index d6935b3d60..ed61b523c1 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "i18n:check": "dotenv -e .env -- tsx scripts/check-i18n.ts", "i18n:sync": "dotenv -e .env -- tsx scripts/sync-i18n.ts", "i18n:auto": "dotenv -e .env -- tsx scripts/auto-translate-i18n.ts", + "i18n:scan": "i18next-scanner && yarn format", "update:languages": "tsx scripts/update-languages.ts", "test": "vitest run --silent", "test:main": "vitest run --project main",