The target locale code (e.g. "pl").
A promise that resolves once the locale has been applied (or dropped).
await setLocale("pl"); // UI re-renders in Polish
export async function setLocale(locale: string): Promise<void> {
const next = availableLocales.includes(locale) ? locale : DEFAULT_LOCALE;
requestedLocale = next;
if (!messageTables[next]) {
try {
messageTables[next] = await localeLoaders[next]();
} catch (error) {
// A locale that can't load leaves the UI in its current language.
logger.warn(`Failed to load locale "${next}": ${getErrorMessage(error)}`, { error });
return;
}
}
// A newer setLocale call superseded this one while the table was loading.
if (requestedLocale !== next) return;
if (next === currentLocale) return;
currentLocale = next;
for (const listener of listeners) listener();
}
Switches the active UI locale and notifies subscribers so the React tree re-renders with the new language. A locale that doesn't ship a
messages.jsonfalls back to the default; a no-op when the locale is unchanged. Non-default locales are loaded on demand, so the switch happens once the table has loaded; if anothersetLocalecall arrives meanwhile, the older one is dropped.