fix(i18n): hand Intl and the document the real language tag for br and gr

TREK's codes 'br' (Brazilian Portuguese) and 'gr' (Greek) went straight
into <html lang> and into ten Intl calls. 'br' is Breton, so Brazilian
users got Breton month names and screen readers announced the page as
Breton; 'gr' is no language at all, so Greek users got the default
locale. getIntlLanguage maps both, and every one of those sites goes
through it.
This commit is contained in:
Maurice
2026-10-07 16:34:48 +02:00
parent 461a63fc86
commit 8f02337d45
7 changed files with 46 additions and 13 deletions
+23
View File
@@ -0,0 +1,23 @@
import { getIntlLanguage, SUPPORTED_LANGUAGES } from './languages';
import { describe, expect, it } from 'vitest';
describe('getIntlLanguage', () => {
it('maps the two codes that are not the BCP-47 tag of their language', () => {
expect(getIntlLanguage('br')).toBe('pt-BR');
expect(getIntlLanguage('gr')).toBe('el');
});
it('passes every other supported code through and falls back to English', () => {
expect(getIntlLanguage('de')).toBe('de');
expect(getIntlLanguage('zh-TW')).toBe('zh-TW');
expect(getIntlLanguage('xx')).toBe('en');
});
it('gives Intl a tag it formats in the right language for every supported code', () => {
for (const { value, locale } of SUPPORTED_LANGUAGES) {
const primary = locale.split('-')[0];
expect(new Intl.Locale(getIntlLanguage(value)).language, value).toBe(primary);
}
});
});
+7 -2
View File
@@ -43,9 +43,14 @@ export function getLocaleForLanguage(language: string): string {
return LOCALES[language] ?? LOCALES['en'] ?? 'en-US';
}
// Returns a BCP-47 tag suitable for Intl APIs.
// Two codes TREK uses are not the BCP-47 tag of their language: 'br' is Breton
// and 'gr' is no language at all.
const INTL_LANGUAGE: Partial<Record<string, string>> = { br: 'pt-BR', gr: 'el' };
// Returns a BCP-47 tag suitable for Intl APIs and the document's lang attribute.
export function getIntlLanguage(language: string): string {
if (language === 'br') return 'pt-BR';
const tag = INTL_LANGUAGE[language];
if (tag) return tag;
return SUPPORTED_LANGUAGE_CODES.includes(language) ? language : 'en';
}