UNPKG

@inlang/paraglide-js

Version:

[![Inlang-ecosystem compatibility badge](https://cdn.jsdelivr.net/gh/opral/monorepo@main/inlang/assets/md-badges/inlang.svg)](https://inlang.com)

32 lines (31 loc) 857 B
import { isLocale } from "./is-locale.js"; /** * Negotiates a preferred language from navigator.languages. * * Use the function on the client to extract the locale * from the navigator.languages array. * * @example * const locale = extractLocaleFromNavigator(); * * @type {() => Locale | undefined} * @returns {string | undefined} */ export function extractLocaleFromNavigator() { if (!navigator?.languages?.length) { return undefined; } const languages = navigator.languages.map((lang) => ({ fullTag: lang.toLowerCase(), baseTag: lang.split("-")[0]?.toLowerCase(), })); for (const lang of languages) { if (isLocale(lang.fullTag)) { return lang.fullTag; } else if (isLocale(lang.baseTag)) { return lang.baseTag; } } return undefined; }