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; } };