Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): show Portuguese outside Brazil in Portuguese

Yudhi Armyndharise85f8c00e0a1
diff --git a/dashboard/src/i18n/detect-language-portuguese.test.ts b/dashboard/src/i18n/detect-language-portuguese.test.ts
new file mode 100644
index 000000000..af451751e
--- /dev/null
+++ b/dashboard/src/i18n/detect-language-portuguese.test.ts
@@ -0,0 +1,34 @@
+// First-visit detection for Portuguese outside Brazil. The browser's preference list is read once, while the module
+// initializes, so this lives in its own file (see detect-language.test.ts).
+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'];
+let resolveSupportedLanguage: I18nModule['resolveSupportedLanguage'];
+
+before(async () => {
+ const { installJsdomGlobals } = (await import('../test-helpers/jsdom.ts')) as {
+ installJsdomGlobals: typeof installJsdomGlobalsFn;
+ };
+ await installJsdomGlobals();
+ Object.defineProperty(navigator, 'languages', { value: ['pt-PT', 'pt', 'en-US'], configurable: true });
+ const module = (await import('./index.ts')) as I18nModule;
+ i18n = module.default;
+ resolveSupportedLanguage = module.resolveSupportedLanguage;
+ await module.i18nReady;
+});
+
+test('a Portuguese visitor outside Brazil gets the Portuguese catalogue, not English', () => {
+ assert.equal(i18n.language, 'pt-BR');
+ assert.equal(localStorage.getItem('openwa_language'), 'pt-BR');
+});
+
+test('every Portuguese tag resolves to the one Portuguese catalogue', () => {
+ for (const tag of ['pt', 'pt-PT', 'pt-pt', 'pt-AO', 'pt-BR', 'PT-br']) {
+ assert.equal(resolveSupportedLanguage(tag), 'pt-BR', tag);
+ }
+});
diff --git a/dashboard/src/i18n/index.ts b/dashboard/src/i18n/index.ts
index 1a62587f1..4b8699a05 100644
--- a/dashboard/src/i18n/index.ts
+++ b/dashboard/src/i18n/index.ts
@@ -49,6 +49,8 @@ export function resolveSupportedLanguage(lang?: string): SupportedLanguage {
const parts = value.toLowerCase().split('-');
const base = parts[0];
+ // The only Portuguese catalogue is the Brazilian one; a bare or European tag would otherwise fall through to English.
+ if (base === 'pt') return 'pt-BR';
if (base === 'zh') {
const subtags = new Set(parts.slice(1));
if (subtags.has('hant') || subtags.has('hk') || subtags.has('mo') || subtags.has('tw')) return 'zh-HK';