From 1098adc03d6eccc56398bb0b55e072f14a3ab916 Mon Sep 17 00:00:00 2001 From: Oscar Hinton Date: Fri, 21 May 2021 13:07:33 +0200 Subject: [PATCH] Correctly handle dash in locale, and add a fallback to en. (#988) --- src/connectors/webauthn-fallback.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/connectors/webauthn-fallback.ts b/src/connectors/webauthn-fallback.ts index 8d6dcb3e93..72f920e4fc 100644 --- a/src/connectors/webauthn-fallback.ts +++ b/src/connectors/webauthn-fallback.ts @@ -11,11 +11,14 @@ let sentSuccess = false; let locales: any = {}; document.addEventListener('DOMContentLoaded', async () => { - const locale = getQsParam('locale'); - const filePath = `locales/${locale}/messages.json?cache=${process.env.CACHE_TAG}`; - const localesResult = await fetch(filePath); - locales = await localesResult.json(); + const locale = getQsParam('locale').replace('-', '_'); + try { + locales = await loadLocales(locale); + } catch { + console.error('Failed to load the locale', locale); + locales = await loadLocales('en') + } document.getElementById('msg').innerText = translate('webAuthnFallbackMsg'); document.getElementById('remember-label').innerText = translate('rememberMe'); @@ -30,6 +33,12 @@ document.addEventListener('DOMContentLoaded', async () => { content.classList.remove('d-none'); }); +async function loadLocales(locale: string) { + const filePath = `locales/${locale}/messages.json?cache=${process.env.CACHE_TAG}`; + const localesResult = await fetch(filePath); + return await localesResult.json(); +} + function translate(id: string) { return locales[id]?.message || ''; }