Files
365devnet/src/utils/language.ts
becarta ba2fb9f248
Some checks failed
GitHub Actions / build (18) (push) Has been cancelled
GitHub Actions / build (20) (push) Has been cancelled
GitHub Actions / build (22) (push) Has been cancelled
GitHub Actions / check (push) Has been cancelled
Made English the default site so that people dont see a redirect page first
2025-05-01 21:38:11 +02:00

20 lines
750 B
TypeScript

import { supportedLanguages } from '~/i18n/translations';
// Define the type for supported languages
type SupportedLanguage = (typeof supportedLanguages)[number];
export function detectPreferredLanguage(request: Request): SupportedLanguage {
// Check for language preference in cookies (set by client-side JS)
const cookies = request.headers.get('cookie') || '';
const cookieMatch = cookies.match(/preferredLanguage=([^;]+)/);
const cookieLanguage = cookieMatch ? cookieMatch[1] : null;
// Only use browser language if there's an explicit cookie preference
if (cookieLanguage && supportedLanguages.some((lang) => lang === cookieLanguage)) {
return cookieLanguage as SupportedLanguage;
}
// Default to English
return 'en';
}