Skip to main content

Core

Core i18n functionality.

define(config)

Defines an i18n instance with automatic type inference.

Parameters:

ParameterTypeRequiredDescription
localesreadonly string[]Array of supported locales (optional, inferred from messages)
defaultLocalestringDefault locale (optional, defaults to the first locale)
messagesRecord<Locale, Messages>RequiredMessage objects keyed by locale

Return Value (Properties of i18n)

define returns an object (e.g., i18n) containing the following properties:

i18n.getMessages(locale)

Returns the message object for the specified locale.

Parameters:

ParameterTypeRequiredDescription
localestring | undefinedRequiredThe locale to retrieve messages for

Returns:

  • Messages: The message object for the given locale.

Example:

const messages = getMessages('en')
console.log(messages.common.title)

i18n.getTranslations(locale, namespace?)

Returns a translation function t for the specified locale.

Parameters:

ParameterTypeRequiredDescription
localestring | undefinedRequiredThe locale to use for translations
namespacestringOptional namespace to scope the keys

Returns:

  • t(key, vars?): The translation function.
    • key: The message key.
    • vars: Interpolation variables (optional).
    • Returns: string (The translated string).

Example:

const t = getTranslations('en', 'common')
const title = t('title')

i18n.locales

The configured (or inferred) locales array.

i18n.defaultLocale

The configured (or inferred) defaultLocale string.

DefineConfig (type)

The type of the configuration object passed to define(). Can be imported directly from @i18n-tiny/astro.