Skip to content

Commit

Permalink
fix(i18n): outdated locale was used instead
Browse files Browse the repository at this point in the history
  • Loading branch information
pylixonly committed May 8, 2024
1 parent 2560e9a commit 7c8ee06
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/core/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
const _loadedStrings = {} as Record<string, typeof langDefault>;

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];
}
Expand All @@ -33,9 +35,10 @@ export function initFetchI18nStrings() {
"sv-SE": "sv"
} as Record<string, string>;

const resolvedLocale = languageMap[locale] ?? locale;
const resolvedLocale = _lastSetLocale = languageMap[locale] ?? locale;

if (resolvedLocale.startsWith("en-")) {
currentLocale = null;
_currentLocale = null;
return;
}

Expand All @@ -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;
}
};

Expand Down

0 comments on commit 7c8ee06

Please sign in to comment.