From 7c8ee06c7e3b33f2cf8c41c3d3eb9d85d5899b75 Mon Sep 17 00:00:00 2001 From: amsyarasyiq <82711525+amsyarasyiq@users.noreply.github.com> Date: Wed, 8 May 2024 18:28:32 +0800 Subject: [PATCH] fix(i18n): outdated locale was used instead --- src/core/i18n/index.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/core/i18n/index.ts b/src/core/i18n/index.ts index eeb7366..aa9f1e6 100644 --- a/src/core/i18n/index.ts +++ b/src/core/i18n/index.ts @@ -8,14 +8,16 @@ const IntlMessageFormat = findByName("MessageFormat") as typeof import("intl-mes type I18nKey = keyof typeof langDefault; -let currentLocale: string | null; +let _currentLocale: string | null = null; +let _lastSetLocale: string | null = null; + const _loadedLocale = new Set(); const _loadedStrings = {} as Record; export const Strings = new Proxy({}, { get: (_t, prop: keyof typeof langDefault) => { - if (currentLocale && _loadedStrings[currentLocale]?.[prop]) { - return _loadedStrings[currentLocale]?.[prop]; + if (_currentLocale && _loadedStrings[_currentLocale]?.[prop]) { + return _loadedStrings[_currentLocale]?.[prop]; } return langDefault[prop]; } @@ -33,9 +35,10 @@ export function initFetchI18nStrings() { "sv-SE": "sv" } as Record; - const resolvedLocale = languageMap[locale] ?? locale; + const resolvedLocale = _lastSetLocale = languageMap[locale] ?? locale; + if (resolvedLocale.startsWith("en-")) { - currentLocale = null; + _currentLocale = null; return; } @@ -45,10 +48,10 @@ export function initFetchI18nStrings() { fetch(`https://raw.githubusercontent.com/pyoncord/i18n/main/resources/${resolvedLocale}/bunny.json`) .then(r => r.json()) .then(strings => _loadedStrings[resolvedLocale] = strings) - .then(() => currentLocale = resolvedLocale) + .then(() => resolvedLocale === _lastSetLocale && (_currentLocale = resolvedLocale)) .catch(e => console.error(`An error occured while fetching strings for ${resolvedLocale}: ${e}`)); } else { - currentLocale = resolvedLocale; + _currentLocale = resolvedLocale; } };