Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): let an unsupported browser language defer to the next

Yudhi Armyndharis3cb96d064583
diff --git a/dashboard/src/i18n/detect-language.test.ts b/dashboard/src/i18n/detect-language.test.ts
new file mode 100644
index 000000000..a08b10b5f
--- /dev/null
+++ b/dashboard/src/i18n/detect-language.test.ts
@@ -0,0 +1,28 @@
+// First-visit language detection against the real i18n module. The browser's preference list is read
+// once, while the module initializes, so this lives in its own file: node:test gives every file its own
+// process, and nothing else here may import i18n before the preferences below are in place.
+import '../test-helpers/register-hooks.ts';
+import { test, before } from 'node:test';
+import assert from 'node:assert/strict';
+import type { installJsdomGlobals as installJsdomGlobalsFn } from '../test-helpers/jsdom.ts';
+
+type I18nModule = typeof import('./index.ts');
+
+let i18n: I18nModule['default'];
+
+before(async () => {
+ const { installJsdomGlobals } = (await import('../test-helpers/jsdom.ts')) as {
+ installJsdomGlobals: typeof installJsdomGlobalsFn;
+ };
+ await installJsdomGlobals();
+ // A visitor whose first preference is a language the dashboard does not ship.
+ Object.defineProperty(navigator, 'languages', { value: ['ja-JP', 'id-ID'], configurable: true });
+ const module = (await import('./index.ts')) as I18nModule;
+ i18n = module.default;
+ await module.i18nReady;
+});
+
+test('an unsupported first preference yields to the next supported one, not to English', () => {
+ assert.equal(i18n.language, 'id');
+ assert.equal(localStorage.getItem('openwa_language'), 'id');
+});
diff --git a/dashboard/src/i18n/index.ts b/dashboard/src/i18n/index.ts
index 0c5223dd1..1a62587f1 100644
--- a/dashboard/src/i18n/index.ts
+++ b/dashboard/src/i18n/index.ts
@@ -115,7 +115,13 @@ export const i18nReady = i18n
order: ['localStorage', 'navigator'],
lookupLocalStorage: 'openwa_language',
caches: ['localStorage'],
- convertDetectedLanguage: (lang: string) => resolveSupportedLanguage(lang),
+ // Map a code only when it matches a shipped language. An unmatched one passes through unchanged,
+ // so i18next skips it and tries the visitor's next preference; mapping it to 'en' would end the
+ // search on an English they never asked for. fallbackLng still covers a list with no match.
+ convertDetectedLanguage: (lang: string) => {
+ const resolved = resolveSupportedLanguage(lang);
+ return resolved === 'en' && lang.toLowerCase().split('-')[0] !== 'en' ? lang : resolved;
+ },
},
react: { useSuspense: false },
});