UNPKG

1.6 kBJavaScriptView Raw
1const fs = require("fs"),
2 path = require("path"),
3 shell = require("shelljs");
4
5const locales = process.env.CANON_LANGUAGES || "en";
6const {name} = JSON.parse(shell.cat("package.json"));
7
8const localePath = path.join(__dirname, "../src/i18n/canon.js");
9
10const defaultLocale = JSON.parse(shell.cat(localePath).replace(/^[^{]+/g, "").slice(0, -2));
11
12module.exports = {
13 options: {
14 debug: false,
15 defaultNs: name,
16 func: {
17 extensions: [".js", ".jsx"],
18 list: ["i18next.t", "i18n.t", "t"]
19 },
20 interpolation: {
21 prefix: "{{",
22 suffix: "}}"
23 },
24 lngs: locales.split(","),
25 ns: [name],
26 removeUnusedKeys: false,
27 resource: {
28 loadPath: "locales/{{lng}}/{{ns}}.json",
29 savePath: "locales/{{lng}}/{{ns}}.json"
30 },
31 trans: {
32 extensions: [".js", ".jsx"],
33 fallbackKey: (ns, value) => value
34 },
35 sort: true
36 },
37 transform: function customTransform(file, enc, done) {
38
39 const parser = this.parser;
40 const content = fs.readFileSync(file.path, enc);
41 let count = 0;
42
43 function customParser(key) {
44 const x = key.split(".");
45 const defaultValue = defaultLocale[x[0]] ? x.reduce((o, i) => o[i], defaultLocale) : key;
46 parser.set(key, {defaultValue});
47 count++;
48 }
49
50 parser.parseFuncFromString(content, customParser);
51 parser.parseTransFromString(content, customParser);
52 parser.parseAttrFromString(content, customParser);
53
54 if (count > 0) {
55 shell.echo(`${` ${count}`.slice(-3)} translations found in ${JSON.stringify(file.relative)}`);
56 }
57
58 done();
59 }
60};