useI18n and the translator
| Name | Type / default | Description |
|---|---|---|
useI18n()
|
I18n
| Gets the instance provided by the plugin. Auto-imported in components. |
locale
|
readonly string
| Language of the document, read from the URL prefix. |
defaultLocale · locales · dir
|
readonly
| Language without a prefix, configured languages and direction of the document. |
t(key, values?)
|
string
| Key and parameters correlated with the generated registry. |
number(value, options?)
|
string
| Intl.NumberFormat format. |
date(value, options?)
|
string
| Intl.DateTimeFormat format. |
has(key, { locale }?)
|
boolean
| Whether the key has a message in the loaded catalogs, for a key that comes from data. |
localePath(path, locale?)
|
string
| Internal route in a language: without a prefix in the default language, with /en/… in the others. |
switchLocalePath(locale)
|
string
| Equivalent route of the current page in another language, with search and fragment. Reactive. |
pageLocales()
|
readonly string[]
| Languages that have a version of the current page. Reactive. |
localeName(locale, displayLocale?)
|
string
| Name of the language, by default in that language (English, Español). |
directionOf(locale)
|
ltr | rtl
| Direction of a configured language. |
suggestedLocale()
|
string | undefined
| Language of the cookie or the browser if it is not the current one. Only in the browser; it never redirects. |
rememberLocale(locale)
|
void
| Stores the language in the cookie, for example when a suggestion is dismissed. |
switchLocale(locale)
|
void
| Stores the language and navigates to the equivalent route (a new document). |
Module options
| Name | Type / default | Description |
|---|---|---|
defaultLocale
|
string
| Required. Language for URLs without a prefix. |
catalogs
|
Record<string, string | Record<string, string>>
| Required. JSON file or map of namespaces per language. Requires common. |
routes
|
Record<string, readonly string[]>
| Additional namespaces per exact route ID. |
global
|
readonly string[]
| Namespaces loaded on every route together with common, such as the catalog of a shared shell. |
directions
|
Record<string, ltr | rtl>
| Direction per language; the others use ltr. |
strategy
|
prefix_except_default
| The default. The default language without a prefix and the others with one, with routes generated from the same pages. |
exclude
|
readonly string[]
| Route IDs, with wildcards, without versions in other languages or hreflang. |
partial
|
Record<string, readonly string[]>
| For a language with a partial translation, the only routes that have a version in it. |
cookie
|
{ name, domain?, maxAge? } | false
| Cookie of the chosen language, only to suggest. meeko-locale by default. |
hreflang
|
boolean
| link rel=alternate per language and x-default in the head. true by default. |
sitemap
|
boolean
| xhtml:link alternates in Meeko's sitemap.xml. true by default. |
Low-level integrations
createI18n(options)
receives the language, the compiled messages, the languages and the current URL. For Meeko applications the module prepares that catalog and its context.
import { createI18n } from '@meekojs/i18n'
// Compiled catalog: text fragments and arguments.
const translator = createI18n({
locale: 'es',
catalog: { greeting: ['Hola ', { arg: 'name' }] },
locales: ['es', 'en'],
defaultLocale: 'es',
path: () => window.location.pathname + window.location.search + window.location.hash,
navigate: path => window.location.assign(path),
})
translator.t('greeting', { name: 'Meeko' })
translator.switchLocalePath('en') // '/en/…'
i18nContext
is the public key to provide an instance from your own integration.
localeOf,
localizedPath,
stripLocale
and
negotiateLocale
are documented in
languages and routes. The
I18n,
I18nValue
and
I18nMessageValues
types are imported from the root of the package.