lemmy-ui/generate_translations.js

109 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-03-01 20:00:43 +00:00
const fs = require("fs");
2021-03-01 20:00:43 +00:00
const translationDir = "lemmy-translations/translations/";
const outDir = "src/shared/translations/";
fs.mkdirSync(outDir, { recursive: true });
2021-01-06 21:06:13 +00:00
fs.readdir(translationDir, (_err, files) => {
files.forEach(filename => {
2021-03-01 20:00:43 +00:00
const lang = filename.split(".")[0];
try {
const json = JSON.parse(
2021-03-01 20:00:43 +00:00
fs.readFileSync(translationDir + filename, "utf8")
);
2021-03-01 20:00:43 +00:00
let data = `export const ${lang} = {\n translation: {`;
for (const key in json) {
if (key in json) {
const value = json[key].replace(/"/g, '\\"');
2021-03-01 20:00:43 +00:00
data += `\n ${key}: "${value}",`;
}
}
2021-03-01 20:00:43 +00:00
data += "\n },\n};";
const target = outDir + lang + ".ts";
fs.writeFileSync(target, data);
} catch (err) {
console.error(err);
}
});
});
2021-03-01 20:00:43 +00:00
// 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 = " ";
2021-03-01 20:00:43 +00:00
2021-03-01 20:33:57 +00:00
const data = `import { i18n } from "i18next";
2021-03-01 20:00:43 +00:00
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);
2021-03-01 20:33:57 +00:00
export interface TFunctionTyped {
// Translation requires options
2021-03-01 20:00:43 +00:00
<
TKey extends OptionI18nKeys | OptionI18nKeys[],
2021-03-01 20:00:43 +00:00
TResult extends TFunctionResult = string,
TInterpolationMap extends TTypedOptions<TKey> = StringMap
> (
key: TKey,
options: TOptions<TInterpolationMap> | string
2021-03-01 20:00:43 +00:00
): TResult;
// Translation does not require options
2021-03-01 20:00:43 +00:00
<
TResult extends TFunctionResult = string,
2021-03-01 20:33:57 +00:00
TInterpolationMap extends Record<string, unknown> = StringMap
> (
key: NoOptionI18nKeys | NoOptionI18nKeys[],
2021-03-01 20:00:43 +00:00
options?: TOptions<TInterpolationMap> | string
): TResult;
}
2021-03-01 20:33:57 +00:00
export interface i18nTyped extends i18n {
t: TFunctionTyped;
}
2021-03-01 20:00:43 +00:00
}
`;
fs.writeFileSync(`${outDir}i18next.d.ts`, data);
});