AdonisJS API
    Preparing search index...

    I18n exposes the APIs to format values and translate messages for a given locale.

    Under the hood it uses the I18nManager to load translations data

    const i18n = new I18n('en', emitter, manager)
    const message = i18n.t('hello.world', { name: 'John' })
    console.log(message) // "Hello John"

    Hierarchy (View Summary)

    Index
    • Creates a new I18n instance for a specific locale

      Parameters

      • locale: string

        The locale to use for this I18n instance

      • emitter: Emitter<any>

        Event emitter for missing translation notifications

      • i18nManager: I18nManager

        The I18nManager instance containing translations and configuration

      Returns I18n

    localeTranslations: Record<string, string>

    Translations for the selected locale Contains key-value pairs of translation identifiers and their messages

    fallbackTranslations: Record<string, string>

    Translations for the fallback locale. The fallback translations are used when the selected locale translations are missing Contains key-value pairs of translation identifiers and their messages

    fallbackLocale: string

    The fallback locale for the current instance. Used when translations are missing in the primary locale

    • Format a numeric value for a given style

      Parameters

      • value: string | number | bigint

        The numeric value to format (string, number, or bigint)

      • Optionaloptions: NumberFormatOptions

        Optional formatting options (style, currency, etc.)

      Returns string

      Formatted number string according to the current locale

      formatter.formatNumber(1234.56) // "1,234.56" (for en locale)
      formatter.formatNumber(1234.56, { style: 'percent' }) // "123,456%"
    • Format a numeric value to a currency

      Parameters

      • value: string | number | bigint

        The numeric value to format as currency

      • options: CurrencyFormatOptions

        Currency formatting options including currency code

      Returns string

      Formatted currency string

      formatter.formatCurrency(1234.56, { currency: 'USD' }) // "$1,234.56"
      formatter.formatCurrency(1000, { currency: 'EUR' }) // "€1,000.00"
    • Format value as a date. The method accepts the following data types:

      • Date instance
      • Luxon DateTime instance
      • Number representing a unix timestamp
      • String representing an ISO date/time value

      Parameters

      • value: string | number | Date | DateTime<boolean>

        The date value to format

      • Optionaloptions: DateTimeFormatOptions

        Optional date formatting options

      Returns string

      Formatted date string according to the current locale

      formatter.formatDate(new Date()) // "12/25/2023" (for en-US locale)
      formatter.formatDate('2023-12-25T10:00:00Z', { dateStyle: 'full' })
      // "Monday, December 25, 2023"
    • Format value as time. The method accepts the following data types:

      • Date instance
      • Luxon DateTime instance
      • Number representing a unix timestamp
      • String representing an ISO date/time value

      Parameters

      • value: string | number | Date | DateTime<boolean>

        The time value to format

      • Optionaloptions: TimeFormatOptions

        Optional time formatting options

      Returns string

      Formatted time string according to the current locale

      formatter.formatTime(new Date()) // "10:30:00 AM" (for en locale)
      formatter.formatTime('2023-12-25T14:30:00Z', { timeStyle: 'short' }) // "2:30 PM"
    • Format value as relative diff between the current time and the given value. The following data types are allowed:

      • Date instance
      • Luxon DateTime instance
      • Number representing the diff value in provided units. If the "unit" is auto, the number will be considered as a diff in milliseconds
      • String representing an ISO date/time value

      Parameters

      • value: string | number | Date | DateTime<boolean>

        The date/time value to format relatively

      • unit: "auto" | RelativeTimeFormatUnit

        The unit for relative formatting or 'auto' for automatic unit selection

      • Optionaloptions: RelativeTimeFormatOptions

        Optional relative time formatting options

      Returns string

      Formatted relative time string

      formatter.formatRelativeTime(-1, 'day') // "1 day ago"
      formatter.formatRelativeTime(new Date(Date.now() + 3600000), 'auto') // "in 1 hour"
      formatter.formatRelativeTime('2023-12-25T10:00:00Z', 'auto') // "2 days ago"
    • Format the value to its plural counterpart using locale-specific plural rules

      Parameters

      • value: string | number

        The numeric value to determine plural form for

      • Optionaloptions: PluralRulesOptions

        Optional plural rules options

      Returns string

      Plural category string ("zero", "one", "two", "few", "many", or "other")

      formatter.formatPlural(1) // "one"
      formatter.formatPlural(2) // "other" (for English)
      formatter.formatPlural(0) // "other" (for English)
    • Format an array of strings to a sentence using locale-appropriate conjunctions

      Parameters

      • list: Iterable<string>

        Iterable of strings to format into a list

      • Optionaloptions: ListFormatOptions

        Optional list formatting options (type, style)

      Returns string

      Formatted list string

      formatter.formatList(['apple', 'banana', 'orange']) // "apple, banana, and orange"
      formatter.formatList(['red', 'blue'], { type: 'disjunction' }) // "red or blue"
    • Format region, currency, language codes to their display names

      Parameters

      • code: string

        The code to get display name for (language, region, currency, etc.)

      • options: DisplayNamesOptions

        Display names options specifying the type of code

      Returns string | undefined

      Localized display name for the code

      formatter.formatDisplayNames('US', { type: 'region' }) // "United States"
      formatter.formatDisplayNames('USD', { type: 'currency' }) // "US Dollar"
      formatter.formatDisplayNames('en', { type: 'language' }) // "English"
    • Creates a messages provider for VineJS validation framework

      Parameters

      • prefix: string = 'validator.shared'

        The prefix to use when looking up validation messages

      Returns I18nMessagesProvider

      A new I18nMessagesProvider instance

      const i18n = new I18n('en', emitter, manager)
      const provider = i18n.createMessagesProvider('validation.messages')
    • Returns the message for a given identifier

      Parameters

      • identifier: string

        The translation identifier to resolve

      Returns { message: string; isFallback: boolean } | null

      Object with message and fallback status, or null if not found

      const result = i18n.resolveIdentifier('hello.world')
      if (result) {
      console.log(result.message) // "Hello World"
      console.log(result.isFallback) // false
      }
    • Returns a boolean identifying if the message for a given identifier exists or not

      Parameters

      • identifier: string

        The translation identifier to check

      Returns boolean

      True if the message exists in the current locale

      if (i18n.hasMessage('hello.world')) {
      console.log('Message exists!')
      }
    • Returns a boolean identifying if a fallback message for a given identifier exists or not

      Parameters

      • identifier: string

        The translation identifier to check

      Returns boolean

      True if the message exists in the fallback locale

      if (i18n.hasFallbackMessage('hello.world')) {
      console.log('Fallback message exists!')
      }
    • Switch locale for the current instance Updates all locale-related properties to the new locale

      Parameters

      • locale: string

        The new locale to switch to

      Returns void

      i18n.switchLocale('fr')
      console.log(i18n.locale) // "fr"
    • Formats a message using the messages formatter

      Parameters

      • identifier: string

        The translation identifier to format

      • Optionaldata: Record<string, any>

        Optional data object for message interpolation

      • OptionalfallbackMessage: string

        Optional fallback message when translation is missing

      Returns string

      The formatted message string

      const message = i18n.formatMessage('hello.user', { name: 'John' })
      console.log(message) // "Hello John"

      // With fallback
      const messageWithFallback = i18n.formatMessage('missing.key', {}, 'Default message')
    • Shorthand method for formatMessage - the primary translation method

      Parameters

      • identifier: string

        The translation identifier to format

      • Optionaldata: Record<string, any>

        Optional data object for message interpolation

      • OptionalfallbackMessage: string

        Optional fallback message when translation is missing

      Returns string

      The formatted message string

      const message = i18n.t('welcome.message', { user: 'John' })
      console.log(message) // "Welcome John"
    • Formats a raw message string using the configured message formatter

      Parameters

      • message: string

        The raw message string to format

      • Optionaldata: Record<string, any>

        Optional data object for message interpolation

      Returns string

      The formatted message string

      const formatted = i18n.formatRawMessage('Hello {name}!', { name: 'John' })
      console.log(formatted) // "Hello John!"