forked from LemmyNet/lemmy-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_translations.js
108 lines (93 loc) · 2.86 KB
/
generate_translations.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
const fs = require("fs");
const translationDir = "lemmy-translations/translations/";
const outDir = "src/shared/translations/";
fs.mkdirSync(outDir, { recursive: true });
fs.readdir(translationDir, (_err, files) => {
files.forEach(filename => {
const lang = filename.split(".")[0];
try {
const json = JSON.parse(
fs.readFileSync(translationDir + filename, "utf8")
);
let data = `export const ${lang} = {\n translation: {`;
for (const key in json) {
if (key in json) {
const value = json[key].replace(/"/g, '\\"');
data += `\n ${key}: "${value}",`;
}
}
data += "\n },\n};";
const target = outDir + lang + ".ts";
fs.writeFileSync(target, data);
} catch (err) {
console.error(err);
}
});
});
// generate types for i18n keys
const baseLanguage = "en";
fs.readFile(`${translationDir}${baseLanguage}.json`, "utf8", (_, fileStr) => {
const noOptionKeys = [];
const optionKeys = [];
const optionRegex = /\{\{(.+?)\}\}/g;
const optionMap = new Map();
for (const [key, val] of Object.entries(JSON.parse(fileStr))) {
const options = [];
for (
let match = optionRegex.exec(val);
match;
match = optionRegex.exec(val)
) {
options.push(match[1]);
}
if (options.length > 0) {
optionMap.set(key, options);
optionKeys.push(key);
} else {
noOptionKeys.push(key);
}
}
const indent = " ";
const data = `import { i18n } from "i18next";
declare module "i18next" {
export type NoOptionI18nKeys =
${noOptionKeys.map(key => `${indent}| "${key}"`).join("\n")};
export type OptionI18nKeys =
${optionKeys.map(key => `${indent}| "${key}"`).join("\n")};
export type I18nKeys = NoOptionI18nKeys | OptionI18nKeys;
export type TTypedOptions<TKey extends OptionI18nKeys> =${Array.from(
optionMap.entries()
).reduce(
(acc, [key, options]) =>
`${acc} TKey extends \"${key}\" ? ${
options.reduce((acc, cur) => acc + `${cur}: string | number; `, "{ ") +
"}"
} :\n${indent}`,
""
)} (Record<string, unknown> | string);
export interface TFunctionTyped {
// Translation requires options
<
TKey extends OptionI18nKeys | OptionI18nKeys[],
TResult extends TFunctionResult = string,
TInterpolationMap extends TTypedOptions<TKey> = StringMap
> (
key: TKey,
options: TOptions<TInterpolationMap> | string
): TResult;
// Translation does not require options
<
TResult extends TFunctionResult = string,
TInterpolationMap extends Record<string, unknown> = StringMap
> (
key: NoOptionI18nKeys | NoOptionI18nKeys[],
options?: TOptions<TInterpolationMap> | string
): TResult;
}
export interface i18nTyped extends i18n {
t: TFunctionTyped;
}
}
`;
fs.writeFileSync(`${outDir}i18next.d.ts`, data);
});