From 3ca7d3d61535855dd947265f2ee1d1234f6ef762 Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Mon, 1 Apr 2024 23:18:50 -0400 Subject: [PATCH] [U] Update i18n interface --- AquaNet/src/libs/i18n.ts | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/AquaNet/src/libs/i18n.ts b/AquaNet/src/libs/i18n.ts index df45710f..77fce7d0 100644 --- a/AquaNet/src/libs/i18n.ts +++ b/AquaNet/src/libs/i18n.ts @@ -17,15 +17,34 @@ if (navigator.language.startsWith('zh')) { lang = 'zh' } +export function ts(key: string, variables?: { [index: string]: any }) { + return t(key as keyof LocalizedMessages, variables) +} + +/** + * Load the translation for the given key + * + * TODO: Check for translation completion on build + * + * @param key + * @param variables + */ export function t(key: keyof LocalizedMessages, variables?: { [index: string]: any }) { - if (!msgs[lang][key]) { - console.warn(`Missing translation for ${key}`) - return key + // Check if the key exists + let msg = msgs[lang][key] + if (!msg) { + // Check if the key exists in English + if (!(msg = msgs.en[key])) { + msg = key + console.error(`ERROR!! Missing translation reference entry (English) for ${key}`) + } + else console.warn(`Missing translation for ${key} in ${lang}`) } + // Replace variables if (variables) { - return msgs[lang][key].replace(/\${(.*?)}/g, (_: string, v: string | number) => variables[v] + "") + return msg.replace(/\${(.*?)}/g, (_: string, v: string | number) => variables[v] + "") } - return msgs[lang][key] + return msg } Object.assign(window, { t })