AdonisJS API
    Preparing search index...

    Class I18nManager

    I18nManager manages translation loading, locale detection, and I18n instance creation. It handles multiple loaders, fallback locales, and caching of translations.

    const manager = new I18nManager(emitter, config)
    await manager.loadTranslations()
    const i18n = manager.locale('en')
    Index

    Configuration object for the I18n manager Contains default locale, supported locales, loaders, and formatter settings

    • get defaultLocale(): string

      Reference to the default locale defined inside the config file This is the primary locale used when no specific locale is requested

      Returns string

    • get hasCachedTranslations(): boolean

      Check if the translations has been cached or not. Use "reloadTranslations" method to re-fetch translations

      Returns boolean

      True if translations have been loaded and cached

    • Returns an array of locales supported by the application.

      The method returns locales by inspecting the translations, when no explicit supportedLocales are defined inside the config file.

      Returns string[]

      Array of supported locale strings

      const locales = manager.supportedLocales()
      console.log(locales) // ['en', 'fr', 'es']
    • Returns an object of cached translations. The object is shared by reference and hence mutations will mutate the original copy

      Returns { [lang: string]: Record<string, string> }

      Object containing all loaded translations by locale

      const translations = manager.getTranslations()
      console.log(translations.en['hello.world']) // "Hello World"
    • Returns an object of translations for a given locale

      Parameters

      • locale: string

        The locale to get translations for

      Returns Record<string, string>

      Object containing translations for the specified locale

      const enTranslations = manager.getTranslationsFor('en')
      console.log(enTranslations['hello.world']) // "Hello World"
    • Returns an instance of the translations formatter for the active formatter. Lazily instantiates the formatter on first access.

      Returns TranslationsFormatterContract

      The configured translations formatter instance

      const formatter = manager.getFormatter()
      const formatted = formatter.format('Hello {name}', 'en', { name: 'John' })
    • Load translations using all the configured loaders.

      The loaded translations are cached forever and you must use "reloadTranslations" method to reload them.

      Returns Promise<void>

      await manager.loadTranslations()
      console.log('Translations loaded successfully')
    • Reload translations from the registered loaders Clears existing cache and loads fresh translations from all loaders

      Returns Promise<void>

      await manager.reloadTranslations()
      console.log('Translations reloaded successfully')
    • Returns the most appropriate supported locale based upon the user languages using content negotiation

      Parameters

      • userLanguage: string | string[]

        Single language string or array of preferred languages

      Returns string | null

      The best matching supported locale or null if no match found

      const locale = manager.getSupportedLocaleFor('en-US,fr;q=0.9')
      console.log(locale) // 'en' (if supported)

      const localeArray = manager.getSupportedLocaleFor(['fr', 'en'])
      console.log(localeArray) // 'fr' (if supported)
    • Returns the fallback locale for a given locale. Returns the default locale when no fallback is defined or no close match is found

      Parameters

      • locale: string

        The locale to find a fallback for

      Returns string

      The fallback locale string

      const fallback = manager.getFallbackLocaleFor('en-CA')
      console.log(fallback) // 'en' (closest match) or defaultLocale
    • Returns an instance of I18n for a given locale Uses the default locale if none is provided

      Parameters

      • Optionallocale: string

        Optional locale string, defaults to defaultLocale

      Returns I18n

      New I18n instance for the specified locale

      const i18n = manager.locale('fr')
      const message = i18n.t('hello.world')

      // Uses default locale
      const defaultI18n = manager.locale()
    • Returns the fallback message for an identifier and locale when the "config.fallback" function is defined.

      Parameters

      • identifier: string

        The translation identifier

      • locale: string

        The locale to get fallback message for

      Returns string | undefined

      Fallback message string or undefined if not configured

      const fallback = manager.getFallbackMessage('missing.key', 'en')
      console.log(fallback) // Custom fallback or undefined