X-Git-Url: http://these/git/?a=blobdiff_plain;f=generate_translations.js;h=f9cde87f4cb97a2f05709dc08ad8247453a380c9;hb=61e0241d8930badc4a77cd973c3cbb64bd13c49e;hp=594f574c3433778551159c28cc8ae5d32369ee6c;hpb=cc215fe99f66257373b7ad178cec0457d1214ecd;p=lemmy-ui.git diff --git a/generate_translations.js b/generate_translations.js index 594f574..f9cde87 100644 --- a/generate_translations.js +++ b/generate_translations.js @@ -8,12 +8,12 @@ fs.readdir(translationDir, (_err, files) => { const lang = filename.split(".")[0]; try { const json = JSON.parse( - fs.readFileSync(translationDir + filename, "utf8") + 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, '\\"'); + const value = json[key].replace(/"/g, '\\"').replace("\n", "\\n"); data += `\n ${key}: "${value}",`; } } @@ -30,30 +30,70 @@ fs.readdir(translationDir, (_err, files) => { const baseLanguage = "en"; fs.readFile(`${translationDir}${baseLanguage}.json`, "utf8", (_, fileStr) => { - const keys = Object.keys(JSON.parse(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 I18nKeys = -${keys.map(key => ` | "${key}"`).join("\n")}; - + 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 =${Array.from( + optionMap.entries(), + ).reduce( + (acc, [key, options]) => + `${acc} TKey extends \"${key}\" ? ${ + options.reduce((acc, cur) => acc + `${cur}: string | number; `, "{ ") + + "}" + } :\n${indent}`, + "", + )} (Record | string); + export interface TFunctionTyped { - // basic usage + // Translation requires options < + TKey extends OptionI18nKeys | OptionI18nKeys[], TResult extends TFunctionResult = string, - TInterpolationMap extends Record = StringMap - >( - key: I18nKeys | I18nKeys[], - options?: TOptions | string + TInterpolationMap extends TTypedOptions = StringMap + > ( + key: TKey, + options: TOptions | string ): TResult; - // overloaded usage + + // Translation does not require options < TResult extends TFunctionResult = string, TInterpolationMap extends Record = StringMap - >( - key: I18nKeys | I18nKeys[], - defaultValue?: string, + > ( + key: NoOptionI18nKeys | NoOptionI18nKeys[], options?: TOptions | string ): TResult; }