-
Notifications
You must be signed in to change notification settings - Fork 1
/
i18next-scanner.config.js
136 lines (128 loc) · 3.47 KB
/
i18next-scanner.config.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const fs = require("fs");
const chalk = require("chalk");
const Parser = require("i18next-scanner").Parser;
module.exports = {
input: [
"src/**/*.{js,jsx}",
// Use ! to filter out files or directories
"!src/**/*.test.{js,jsx}",
"!**/node_modules/**",
],
output: "./",
options: {
debug: false,
removeUnusedKeys: false,
sort: false,
attr: false,
func: false,
trans: false,
lngs: ["en", "it", "es", "fr", "de", "pt", "nl"],
ns: ["translation","anniversary"],
defaultLng: "en",
defaultNs: "translation",
defaultValue: "",
resource: {
loadPath: "public/locales/{{lng}}/{{ns}}.json",
savePath: "public/locales/{{lng}}/{{ns}}.json",
jsonIndent: 4,
lineEnding: "\n",
},
nsSeparator: ":",
keySeparator: ".",
pluralSeparator: "_",
contextSeparator: "_",
contextDefaultValues: [],
interpolation: {
prefix: "{{",
suffix: "}}",
},
metadata: {},
allowDynamicKeys: false,
compatibilityJSON: 'v4'
},
transform: function customTransform(file, enc, done) {
const parser = this.parser;
const transParser = new Parser({
defaultValue: (lng, ns, key) => {
if (lng === "en") {
return key;
} else {
return "";
}
},
lngs: ["en", "it", "es", "fr", "de", "pt", "nl"],
resource: {
loadPath: "public/locales/{{lng}}/{{ns}}.json",
savePath: "public/locales/{{lng}}/{{ns}}.json",
jsonIndent: 4,
lineEnding: "\n",
},
keySeparator: ".",
});
const content = fs.readFileSync(file.path, enc);
let count = 0;
let transCount = 0;
parser.parseFuncFromString(
content,
{
list: ["i18next.t", "i18n.t", "t"],
extensions: [".js", ".jsx"],
},
(key, options) => {
parser.set(key, options);
++count;
}
);
let keysObj = {};
transParser.parseTransFromString(
content,
{
component: "Trans",
i18nKey: "i18nKey",
defaultsKey: "defaults",
extensions: [".js", ".jsx"],
fallbackKey: false,
// 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.
keepBasicHtmlNodesFor: [
"br",
"strong",
"i",
"p",
"a",
"kbd",
"code"
], // Which nodes are allowed to be kept in translations during defaultValue generation of <Trans>.
// https://github.com/acornjs/acorn/tree/master/acorn#interface
acorn: {
ecmaVersion: 2020,
sourceType: "module", // defaults to 'module'
},
},
(key, options) => {
//if(false === key in keysObj) {
keysObj[key] = options;
parser.set(key, options);
++transCount;
//}
}
);
if (count > 0 || transCount > 0) {
console.log(
`i18next-scanner: t() count=${chalk.cyan(
count
)}, Trans count=${chalk.cyan(transCount)}, file=${chalk.yellow(
JSON.stringify(file.relative)
)}`
);
if (transCount > 0) {
console.log(
`file=${chalk.yellow(JSON.stringify(file.relative))}, ${chalk.cyan(
JSON.stringify(keysObj)
)}`
);
}
}
done();
},
};