Detection API
detectLocaleFromAcceptLanguage function
Detects the user's preferred locale from the Accept-Language header.
Signature
typescriptfunction detectLocaleFromAcceptLanguage( acceptLanguageHeader: string, options: Partial<Config> ): Intl.LocaleParameters
acceptLanguageHeader: The Accept-Language header value from the HTTP request.options: Optional object containing partial configuration overrides (for Psitta configuration).
Returns
An
Intl.Localeobject representing the detected locale orundefinedif no match is found.Details
This function relies on the Psitta configuration (
localesproperty) to determine the available locales supported by the application. Languages are prioritized based on the quality values (q values) provided in theAccept-Languageheader. Higher q values indicate a stronger preference. The function iterates through the preferred languages, checking if they are included in thelocaleslist. If a match is found, the corresponding locale tag is returned as anIntl.Localeobject. If no matching locale is found, the function returnsundefined.
detectLocaleFromCookie function
Detects the user's preferred locale from a cookie value.
Signature
typescriptfunction detectLocaleFromCookie( localeCookie: string, options: Partial<Config> ): LocaleParameters
localeCookie: The value of the cookie containing the user's preferred locale (or undefined if not set).options: Optional object containing partial configuration overrides (for Psitta configuration).
Returns
The detected locale code (string) or
nullif no match is found or the cookie value is invalid.Details
This function relies on the Psitta configuration (
localesproperty) to determine the available locales supported by the application. It checks if thelocaleCookievalue exists and is included in the configuredlocaleslist. If a match is found, the cookie value (which is assumed to be a valid locale code) is returned. Otherwise, the function returnsnull.
detectLocaleFromNavigator function
Detects the user's preferred locale from the browser's navigator object.
Signature
typescriptfunction detectLocaleFromNavigator(options: Partial<Config>): Intl.LocaleParameters
options: Optional object containing partial configuration overrides (for Psitta configuration).
Returns
An
Intl.Localeobject representing the detected locale ornullif no match is found.Details
This function uses the browser's
navigator.languageproperty to get the user's preferred language tag. It then attempts to create anIntl.Localeobject from this tag. The function relies on the Psitta configuration (localesproperty) to determine the available locales supported by the application. If the language retrieved from the navigator is not included in thelocaleslist, the function returnsnull. Otherwise, it returns the createdIntl.Localeobject which can be used for formatting based on the user's preferred language
detectLocaleFromPathname function
Detects the user's preferred locale from the URL pathname.
Signature
typescriptfunction detectLocaleFromPathname( pathname: string, options: Partial<Config> ): ObjectParameters
pathname: The URL pathname string.options: Optional object containing partial configuration overrides (for Psitta configuration).
Returns
An object containing the detected locale code and the URL pathname without the locale segment.
Details
This function assumes that the first path segment in the URL might represent the user's preferred locale. It checks if this segment matches any of the supported locales defined in the Psitta configuration (
localesproperty). The function returns an object containing two properties:locale: The detected locale code as a string, ornullif no match is found.urlWithoutLocale: A modified URL pathname where the first path segment (assumed to be the locale) has been removed.
Examples
typescriptconst pathname = '/en/about-us'; const { locale, urlWithoutLocale } = detectLocaleFromPathname(pathname, { locales: ['en', 'fr'] }); if (locale) { console.log('Detected locale:', locale); // Output: 'en' console.log('URL without locale:', urlWithoutLocale); // Output: '/about-us' } else { console.log('No matching locale found in pathname.'); }