mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 09:54:14 +00:00
36ac81322a
wiki/Languages.md has the account's language first, then the browser's. The browser step only ran on the login page, so opening a trip with a live session in a new browser (or after the site data was cleared) showed English to anyone whose account had no language of its own. The app now starts in the browser's language when nothing was chosen in this browser, and keeps it when the account has none. Nothing is written to the account; a language set in the account or picked in the app still wins. The detection moved to its own module so the settings store can use it at startup. Refs #2604
32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
import { SUPPORTED_LANGUAGES } from '@trek/shared';
|
|
|
|
// Its own module so the settings store can run it at startup without importing
|
|
// the translation context, which imports the store.
|
|
|
|
// Detects the user's preferred language from browser/OS settings.
|
|
// Returns null if no supported language matches.
|
|
export function detectBrowserLanguage(): string | null {
|
|
if (typeof navigator === 'undefined') return null;
|
|
const browserLangs = navigator.languages?.length
|
|
? navigator.languages
|
|
: navigator.language
|
|
? [navigator.language]
|
|
: [];
|
|
const supported = SUPPORTED_LANGUAGES.map((l) => l.value);
|
|
|
|
for (const lang of browserLangs) {
|
|
const exactMatch = supported.find((s) => s.toLowerCase() === lang.toLowerCase());
|
|
if (exactMatch) return exactMatch;
|
|
|
|
// pt-BR has no exact match (our code is 'br'), so map it explicitly.
|
|
// pt-PT and bare 'pt' are NOT mapped — they fall through to null.
|
|
if (lang.toLowerCase() === 'pt-br') return 'br';
|
|
|
|
const prefix = lang.split('-')[0]?.toLowerCase();
|
|
const prefixMatch = supported.find((s) => s.toLowerCase() === prefix);
|
|
if (prefixMatch) return prefixMatch;
|
|
}
|
|
|
|
return null;
|
|
}
|