Composables

defineI18nLocale

The defineI18nLocale() composable is used to define a function to dynamically load locale messages used for lazy-loading translations.

The defineI18nLocale() composable is used to define a function to dynamically load locale messages used for lazy-loading translations.

The loader function needs to return a Promise that resolves a messages object.

Type

declare function defineI18nLocale<Messages = LocaleMessages<DefineLocaleMessage>, Locales = Locale>(
  loader: (locale: Locales) => Messages | Promise<Messages>
): (locale: Locales) => Messages | Promise<Messages>

Parameters

loader

A function that is the dynamic locale messages loading, that has the following parameters:

  • locale
    Type: Locale
    A target locale that is passed from nuxt i18n module. That is passed when the locale is switched in the following cases:
    • when you switch the locale with setLocale().
    • when the locale is switched with <NuxtLink>. for example, the route path resolved by useSwitchLocalePath() or $switchLocalePath().

Usage

An example of a loader function using a fetch request to load locale messages:

export default defineI18nLocale(locale => {
  return $fetch(`https://your-company-product/api/${locale}`)
})

Copyright © 2024