Core / locale: LocaleControls
Functions, types, and exports for Core / locale: LocaleControls.
Page outline
import type { LocaleControls } from "@palamedes/core/locale"
type LocaleControls<TLocale extends string> = {
readonly locales: readonly TLocale[];
readonly defaultLocale: TLocale;
readonly labels: Record<TLocale, string>;
isLocale(value: unknown): value is TLocale;
normalizeLocale(value: unknown): TLocale;
label(locale: TLocale): string;
preferredLocale(acceptLanguageHeader: string | null | undefined): TLocale;
/** Resolve the active locale for a request, per strategy. */
resolve(options: {
strategy: "cookie" | "route" | "domain" | "subdomain" | "tld";
acceptLanguageHeader?: string | null;
cookieHeader?: string | null;
routeLocale?: string | null;
requestHost?: string | null;
}): {
locale: TLocale;
source: LocaleSource;
};
/** The deliberate-choice cookie value, or null when unset/invalid. */
readChoice(cookieHeader: string | null | undefined): TLocale | null;
/** A `document.cookie` string recording a deliberate choice. */
serializeChoice(locale: TLocale): string;
/**
* Suggest the browser's Accept-Language locale unless a recorded choice
* confirms the current locale. Earlier choices for another locale do not
* change the browser preference. Host mismatches are independent of intent.
*/
suggest(options: {
currentLocale: TLocale;
acceptLanguageHeader?: string | null;
cookieHeader?: string | null;
pathname: string;
requestHost?: string | null;
search?: string | null;
}): LocaleSuggestion<TLocale> | null;
canonicalUrl(options: {
locale: TLocale;
pathname: string;
requestHost?: string | null;
search?: string | null;
}): string;
/** Canonical URL, configured language alternates, and an optional x-default. */
seoLinks(options: {
locale: TLocale;
pathname: string;
requestHost?: string | null;
search?: string | null;
/** Defaults to defaultLocale; false omits the x-default link. */
xDefaultLocale?: TLocale | false;
}): Array<LocaleSeoLink<TLocale>>;
replaceLocaleInPath(pathname: string, locale: TLocale): string;
extractLocaleFromPath(pathname: string): TLocale | null;
switchItems(options: {
currentLocale: TLocale;
testIdPrefix?: string;
}): Array<LocaleSwitchItem<TLocale>>;
};
Properties
| Name | Type | Required | Description |
|---|---|---|---|
locales | readonly TLocale[] | Yes | Readonly. |
defaultLocale | TLocale | Yes | Readonly. |
labels | Record<TLocale, string> | Yes | Readonly. |
isLocale
isLocale(value: unknown): value is TLocale
Parameters
| Name | Type | Required |
|---|---|---|
value | unknown | Yes |
Returns
value is TLocale
normalizeLocale
normalizeLocale(value: unknown): TLocale
Parameters
| Name | Type | Required |
|---|---|---|
value | unknown | Yes |
Returns
TLocale
label
label(locale: TLocale): string
Parameters
| Name | Type | Required |
|---|---|---|
locale | TLocale | Yes |
Returns
string
preferredLocale
preferredLocale(acceptLanguageHeader: string | null | undefined): TLocale
Parameters
| Name | Type | Required |
|---|---|---|
acceptLanguageHeader | string | null | undefined | Yes |
Returns
TLocale
resolve
Resolve the active locale for a request, per strategy.
resolve(options: {
strategy: "cookie" | "route" | "domain" | "subdomain" | "tld";
acceptLanguageHeader?: string | null;
cookieHeader?: string | null;
routeLocale?: string | null;
requestHost?: string | null;
}): {
locale: TLocale;
source: LocaleSource;
}
Parameters
| Name | Type | Required |
|---|---|---|
options | { strategy: "cookie" | "route" | "domain" | "subdomain" | "tld"; acceptLanguageHeader?: string | null; cookieHeader?: string | null; routeLocale?: string | null; requestHost?: string | null } | Yes |
Returns
{ locale: TLocale; source: LocaleSource }
readChoice
The deliberate-choice cookie value, or null when unset/invalid.
readChoice(cookieHeader: string | null | undefined): TLocale | null
Parameters
| Name | Type | Required |
|---|---|---|
cookieHeader | string | null | undefined | Yes |
Returns
TLocale | null
serializeChoice
A document.cookie string recording a deliberate choice.
serializeChoice(locale: TLocale): string
Parameters
| Name | Type | Required |
|---|---|---|
locale | TLocale | Yes |
Returns
string
suggest
Suggest the browser's Accept-Language locale unless a recorded choice confirms the current locale. Earlier choices for another locale do not change the browser preference. Host mismatches are independent of intent.
suggest(options: {
currentLocale: TLocale;
acceptLanguageHeader?: string | null;
cookieHeader?: string | null;
pathname: string;
requestHost?: string | null;
search?: string | null;
}): LocaleSuggestion<TLocale> | null
Parameters
| Name | Type | Required |
|---|---|---|
options | { currentLocale: TLocale; acceptLanguageHeader?: string | null; cookieHeader?: string | null; pathname: string; requestHost?: string | null; search?: string | null } | Yes |
Returns
LocaleSuggestion<TLocale> | null
canonicalUrl
canonicalUrl(options: {
locale: TLocale;
pathname: string;
requestHost?: string | null;
search?: string | null;
}): string
Parameters
| Name | Type | Required |
|---|---|---|
options | { locale: TLocale; pathname: string; requestHost?: string | null; search?: string | null } | Yes |
Returns
string
seoLinks
Canonical URL, configured language alternates, and an optional x-default.
seoLinks(options: {
locale: TLocale;
pathname: string;
requestHost?: string | null;
search?: string | null;
/** Defaults to defaultLocale; false omits the x-default link. */
xDefaultLocale?: TLocale | false;
}): Array<LocaleSeoLink<TLocale>>
Parameters
| Name | Type | Required |
|---|---|---|
options | { locale: TLocale; pathname: string; requestHost?: string | null; search?: string | null; xDefaultLocale?: TLocale | false } | Yes |
Returns
Array<LocaleSeoLink<TLocale>>
replaceLocaleInPath
replaceLocaleInPath(pathname: string, locale: TLocale): string
Parameters
| Name | Type | Required |
|---|---|---|
pathname | string | Yes |
locale | TLocale | Yes |
Returns
string
extractLocaleFromPath
extractLocaleFromPath(pathname: string): TLocale | null
Parameters
| Name | Type | Required |
|---|---|---|
pathname | string | Yes |
Returns
TLocale | null
switchItems
switchItems(options: {
currentLocale: TLocale;
testIdPrefix?: string;
}): Array<LocaleSwitchItem<TLocale>>
Parameters
| Name | Type | Required |
|---|---|---|
options | { currentLocale: TLocale; testIdPrefix?: string } | Yes |
Returns
Array<LocaleSwitchItem<TLocale>>
Last updated: November 10, 2026