From 872e352ea582a77a27c810c3d3461413c5b37dd7 Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Wed, 21 Feb 2024 16:51:28 -0500 Subject: Added a fallback when there is empty strings --- Utilities/i18n.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Utilities/i18n.js b/Utilities/i18n.js index 9b431a1..e9614ea 100644 --- a/Utilities/i18n.js +++ b/Utilities/i18n.js @@ -22,11 +22,12 @@ import i18next from 'i18next'; import fsBackend from 'i18next-fs-backend'; import { readFileSync } from 'node:fs'; const { locale } = JSON.parse(readFileSync('./config.json', 'utf-8')); +const fallbackLanguage = 'en'; i18next.use(fsBackend).init({ lng: locale, // if you're using a language detector, do not define the lng option debug: false, - fallbackLng: 'en', + fallbackLng: fallbackLanguage, backend: { loadPath: './Locales/{{lng}}/{{ns}}.json' } @@ -34,7 +35,15 @@ i18next.use(fsBackend).init({ export default { i18next, - t(key, option) { - return i18next.t(key, option); + t(key, options) { + let translation = i18next.t(key, options); + if (translation === '') { + // Change language to fallback language, translate the key, then change back to original language + const originalLanguage = i18next.language; + i18next.changeLanguage(fallbackLanguage); + translation = i18next.t(key, options); + i18next.changeLanguage(originalLanguage); + } + return translation; } }; -- cgit v1.2.3