Skip to content
Sveltier
Mode
API

useLocale

Returns messages and cached Intl formatters for the app's locale.

Signature

Signature
import { useLocale, type LocaleInstance } from 'sveltier';

function useLocale(): LocaleInstance;

Call it during component initialisation. The functions read the current settings on every call, so they stay correct when the settings change, and they keep working when destructured.

Returns

NameTypeDefaultDescription
localestring'en-US'The BCP 47 locale used for formatting.
currencystring'USD'The ISO 4217 currency of formatCurrency().
timeZonestring | undefinedruntime zoneThe IANA time zone of formatDate().
t(key, values?)(key: MessageKey, values?: Record<string, string | number>) => string–A message, with {name} placeholders filled from values.
formatNumber(value, options?)(value: number | bigint, options?: Intl.NumberFormatOptions) => string–A number in the locale.
formatCurrency(value, currency?, options?)(value: number | bigint, currency?: string, options?) => string–Money, with the fraction digits of the currency (USD 2, JPY 0).
formatPercent(value, options?)(value: number, options?: Intl.NumberFormatOptions) => string–A ratio as a percentage: 0.0184 is 2%, or 1.84% with two fraction digits.
formatDate(value, options?)(value: Date | number, options?: Intl.DateTimeFormatOptions) => string–A date in the time zone setting, unless options.timeZone says otherwise.
formatRelative(value, now?)(value: Date | number, now?: Date | number) => string–Time from now in the largest unit that fits: "5 minutes ago", "tomorrow".

See Locale for examples and server rendering.