mirror of
https://github.com/LemmyNet/lemmy-ui.git
synced 2024-11-22 12:21:13 +00:00
Generate typescript i18n types
This commit is contained in:
parent
1916483ce9
commit
19c0223b79
1 changed files with 47 additions and 10 deletions
|
@ -1,27 +1,64 @@
|
||||||
fs = require('fs');
|
const fs = require("fs");
|
||||||
|
|
||||||
let translationDir = 'lemmy-translations/translations/';
|
const translationDir = "lemmy-translations/translations/";
|
||||||
let outDir = 'src/shared/translations/';
|
const outDir = "src/shared/translations/";
|
||||||
fs.mkdirSync(outDir, { recursive: true });
|
fs.mkdirSync(outDir, { recursive: true });
|
||||||
fs.readdir(translationDir, (_err, files) => {
|
fs.readdir(translationDir, (_err, files) => {
|
||||||
files.forEach(filename => {
|
files.forEach(filename => {
|
||||||
const lang = filename.split('.')[0];
|
const lang = filename.split(".")[0];
|
||||||
try {
|
try {
|
||||||
const json = JSON.parse(
|
const json = JSON.parse(
|
||||||
fs.readFileSync(translationDir + filename, 'utf8')
|
fs.readFileSync(translationDir + filename, "utf8")
|
||||||
);
|
);
|
||||||
var data = `export const ${lang} = {\n translation: {`;
|
let data = `export const ${lang} = {\n translation: {`;
|
||||||
for (var key in json) {
|
for (const key in json) {
|
||||||
if (key in json) {
|
if (key in json) {
|
||||||
const value = json[key].replace(/"/g, '\\"');
|
const value = json[key].replace(/"/g, '\\"');
|
||||||
data = `${data}\n ${key}: "${value}",`;
|
data += `\n ${key}: "${value}",`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
data += '\n },\n};';
|
data += "\n },\n};";
|
||||||
const target = outDir + lang + '.ts';
|
const target = outDir + lang + ".ts";
|
||||||
fs.writeFileSync(target, data);
|
fs.writeFileSync(target, data);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// generate types for i18n keys
|
||||||
|
const baseLanguage = "en";
|
||||||
|
|
||||||
|
fs.readFile(`${translationDir}${baseLanguage}.json`, "utf8", (_, fileStr) => {
|
||||||
|
const keys = Object.keys(JSON.parse(fileStr));
|
||||||
|
|
||||||
|
const data = `import * as i18n from "i18next";
|
||||||
|
|
||||||
|
type I18nKeys =
|
||||||
|
${keys.map(key => ` | "${key}"`).join("\n")};
|
||||||
|
|
||||||
|
declare module "i18next" {
|
||||||
|
export interface TFunction {
|
||||||
|
// basic usage
|
||||||
|
<
|
||||||
|
TResult extends TFunctionResult = string,
|
||||||
|
TInterpolationMap extends object = StringMap
|
||||||
|
>(
|
||||||
|
key: I18nKeys | I18nKeys[],
|
||||||
|
options?: TOptions<TInterpolationMap> | string
|
||||||
|
): TResult;
|
||||||
|
// overloaded usage
|
||||||
|
<
|
||||||
|
TResult extends TFunctionResult = string,
|
||||||
|
TInterpolationMap extends object = StringMap
|
||||||
|
>(
|
||||||
|
key: I18nKeys | I18nKeys[],
|
||||||
|
defaultValue?: string,
|
||||||
|
options?: TOptions<TInterpolationMap> | string
|
||||||
|
): TResult;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
fs.writeFileSync(`${outDir}i18next.d.ts`, data);
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue