-
-
Notifications
You must be signed in to change notification settings - Fork 641
/
i18next-scanner.config.cjs
92 lines (83 loc) · 2.68 KB
/
i18next-scanner.config.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const fs = require('fs');
const path = require('path');
const typescript = require('typescript');
module.exports = {
input: ['src/app/**/*.{js,jsx,ts,tsx,cjs,mjs,cts,mts}', 'src/browsercheck.js'],
output: './',
options: {
debug: false,
removeUnusedKeys: true,
sort: true,
func: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
},
lngs: ['en'],
ns: ['translation'],
defaultLng: 'en',
resource: {
loadPath: 'config/i18n.json',
savePath: 'src/locale/en.json',
jsonIndent: 2,
lineEnding: '\n',
},
context: true,
contextFallback: true,
contextDefaultValues: ['male', 'female'],
allowDynamicKeys: true,
},
transform: function customTransform(file, enc, done) {
'use strict';
const tsExts = ['.ts', '.tsx'];
const parser = this.parser;
const { base, ext } = path.parse(file.path);
let content = fs.readFileSync(file.path, enc);
const isTs = tsExts.includes(ext) && !base.includes('.d.ts');
if (isTs) {
const { outputText } = typescript.transpileModule(content, {
compilerOptions: {
target: 'es2018',
jsx: 'preserve',
},
fileName: path.basename(file.path),
});
content = outputText;
}
// prettier-ignore
const contexts = {
compact: ['compact'],
max: ['Max'],
};
// prettier-ignore
const keys = {
buckets: { list: ['General', 'Inventory', 'Postmaster', 'Progress', 'Unknown'] },
cooldowns: { list: ['Grenade', 'Melee', 'Super'] },
difficulty: { list: ['Normal', 'Hard'] },
progress: { list: ['Bounties', 'Items', 'Quests'] },
sockets: { list: ['Mod', 'Ability', 'Shader', 'Ornament', 'Fragment', 'Aspect', 'Projection', 'Transmat', 'Super'] }
};
const dimTransformer = (key, options) => {
if (options.metadata?.context) {
// Add context based on metadata
delete options.context;
const context = contexts[options.metadata?.context];
parser.set(key, options);
for (let i = 0; i < context?.length; i++) {
parser.set(`${key}${parser.options.contextSeparator}${context[i]}`, options);
}
}
if (options.metadata?.keys) {
// Add keys based on metadata (dynamic or otherwise)
const list = keys[options.metadata?.keys].list;
for (let i = 0; i < list?.length; i++) {
parser.set(`${key}${list[i]}`, options);
}
}
// Add all other non-metadata related keys w/ default options
if (!options.metadata) {
parser.set(key, options);
}
};
parser.parseFuncFromString(content, { list: ['t', 'tl', 'DimError'] }, dimTransformer);
done();
},
};