refactor i18next-scanner config

This commit is contained in:
Jon Eugster
2024-03-25 22:44:26 +01:00
parent 3ac8fdace7
commit 19d2ea363a
+76 -85
View File
@@ -4,98 +4,89 @@ const fs = require('fs');
const chalk = require('chalk'); const chalk = require('chalk');
module.exports = { module.exports = {
input: [ input: [
'client/src/**/*.{tsx,ts}', 'client/src/**/*.{tsx,ts}',
// Use ! to filter out files or directories // Use ! to filter out files or directories
'!client/i18n/**', '!client/i18n/**',
'!**/node_modules/**', '!**/node_modules/**',
], ],
output: './client/public/locales', output: './client/public/locales',
options: { options: {
debug: true, debug: true,
func: { func: {
list: ['i18next.t', 'i18n.t', 't'], list: ['i18next.t', 'i18n.t', 't'],
extensions: ['.js', '.jsx'] // not .ts or .tsx since we use i18next-scanner-typescript! extensions: ['.js', '.jsx'] // not .ts or .tsx since we use i18next-scanner-typescript!
}, },
trans: { trans: {
component: 'Trans', component: 'Trans',
i18nKey: 'i18nKey', i18nKey: 'i18nKey',
defaultsKey: 'defaults', defaultsKey: 'defaults',
extensions: ['.js', '.jsx'], // not .ts or .tsx since we use i18next-scanner-typescript! extensions: ['.js', '.jsx'], // not .ts or .tsx since we use i18next-scanner-typescript!
fallbackKey: (ns, value) => {return value}, fallbackKey: (ns, value) => {return value},
// https://react.i18next.com/latest/trans-component#usage-with-simple-html-elements-like-less-than-br-greater-than-and-others-v10.4.0 // https://react.i18next.com/latest/trans-component#usage-with-simple-html-elements-like-less-than-br-greater-than-and-others-v10.4.0
supportBasicHtmlNodes: true, // Enables keeping the name of simple nodes (e.g. <br/>) in translations instead of indexed keys. supportBasicHtmlNodes: true, // Enables keeping the name of simple nodes (e.g. <br/>) in translations instead of indexed keys.
keepBasicHtmlNodesFor: ['br', 'strong', 'i', 'p'], // Which nodes are allowed to be kept in translations during defaultValue generation of <Trans>. keepBasicHtmlNodesFor: ['br', 'strong', 'i', 'p'], // Which nodes are allowed to be kept in translations during defaultValue generation of <Trans>.
// // https://github.com/acornjs/acorn/tree/master/acorn#interface // // https://github.com/acornjs/acorn/tree/master/acorn#interface
// acorn: { // acorn: {
// ecmaVersion: 2020, // ecmaVersion: 2020,
// sourceType: 'module', // defaults to 'module' // sourceType: 'module', // defaults to 'module'
// } // }
}, },
lngs: ['en','de'], lngs: ['en','de'],
ns: [], ns: [],
defaultLng: 'en-GB', defaultLng: 'en-GB',
defaultNs: 'translation', defaultNs: 'translation',
defaultValue: (lng, ns, key) => { defaultValue: (lng, ns, key) => {
if (lng === 'en') { if (lng === 'en') {
return key; // Use key as value for base language return key; // Use key as value for base language
} }
return ''; // Return empty string for other languages return ''; // Return empty string for other languages
}, },
resource: { resource: {
loadPath: './{{lng}}/{{ns}}.json', loadPath: './{{lng}}/{{ns}}.json',
savePath: './{{lng}}/{{ns}}.json', savePath: './{{lng}}/{{ns}}.json',
jsonIndent: 2, jsonIndent: 2,
lineEnding: '\n' lineEnding: '\n'
}, },
nsSeparator: false, // namespace separator nsSeparator: false, // namespace separator
keySeparator: false, // key separator keySeparator: false, // key separator
plurals: false, plurals: false,
interpolation: { interpolation: {
prefix: '{{', prefix: '{{',
suffix: '}}' suffix: '}}'
}, },
metadata: {}, metadata: {},
allowDynamicKeys: false, allowDynamicKeys: false,
},
transform: typescriptTransform(
// options
{
// default value for extensions
extensions: [".ts", ".tsx"],
// optional ts configuration
tsOptions: {
target: "es2017",
},
}, },
transform: typescriptTransform( function(outputText, file, enc, done) {
// options 'use strict';
{ const parser = this.parser;
// default value for extensions
extensions: [".ts", ".tsx"],
// optional ts configuration
tsOptions: {
target: "es2017",
},
},
function(outputText, file, enc, done) { let count = 0;
'use strict';
const parser = this.parser;
parser.parseTransFromString(outputText); parser.parseTransFromString(outputText);
parser.parseFuncFromString(outputText); parser.parseFuncFromString(outputText);
// const content = fs.readFileSync(file.path, enc); if (count > 0) {
// let count = 0; console.log(`[i18next-scanner] transform: count=${chalk.cyan(count)}, file=${chalk.yellow(JSON.stringify(file.relative))}`);
// parser.parseFuncFromString(content, { list: ['i18n._', 'i18n.__'] }, (key, options) => {
// parser.set(key, Object.assign({}, options, {
// nsSeparator: false,
// keySeparator: false
// }));
// ++count;
// });
// if (count > 0) {
// console.log(`[i18next-scanner] transform: count=${chalk.cyan(count)}, file=${chalk.yellow(JSON.stringify(file.relative))}`);
// }
done();
} }
),
done();
}
),
}; };