I18n

I18n - Modern internationalization utility for multi-language applications

Features:

  • Translation management with namespace support
  • Named and numbered parameter interpolation
  • Plural forms support
  • Locale-aware number and date formatting
  • Browser language detection with fallback chains
  • Async translation loading
  • Event-driven language switching

Constructor

new I18n(appInstance, options)

Constructor

Parameters:
NameTypeDescription
appInstanceApp

InfrontJS App reference

optionsObject

Configuration options

Examples
// Basic usage
const i18n = new I18n(app);
i18n.addTranslation('en', { 'hello': 'Hello', 'welcome': 'Welcome {name}!' });
i18n.addTranslation('es', { 'hello': 'Hola', 'welcome': '¡Bienvenido {name}!' });

console.log(i18n.t('hello')); // 'Hello'
console.log(i18n.t('welcome', { name: 'John' })); // 'Welcome John!'
// Namespace support
i18n.addTranslation('en', { 
  'common.save': 'Save',
  'user.profile.title': 'User Profile'
});
// Plural forms
i18n.addTranslation('en', {
  'items': {
    one: '{count} item',
    other: '{count} items'
  }
});
console.log(i18n.t('items', { count: 1 })); // '1 item'
console.log(i18n.t('items', { count: 5 })); // '5 items'

Methods

addTranslation(langCode, translationObject, namespace)

Add translations for a language (merge with existing)

Parameters:
NameTypeDescription
langCodestring

Language code

translationObjectObject

Translations to add

namespacestring

Optional namespace prefix

clearAll()

Clear all translations

d(date, options) → {string}

Get formatted date/time

Parameters:
NameTypeDefaultDescription
dateDate

Date to format

optionsObjectnull

Intl.DateTimeFormat options

Returns:

Formatted date

Type: 
string

detectBrowserLanguage() → {string|null}

Detect browser language with improved locale support

Returns:

Detected language code or null

Type: 
string | null

exists(key, langCode) → {boolean}

Check if translation exists

Parameters:
NameTypeDefaultDescription
keystring

Translation key

langCodestringnull

Language code (optional, uses current if not provided)

Returns:

True if translation exists

Type: 
boolean

expose()

Expose functions to global scope (optional)

getAvailableLanguages() → {Array}

Get all available languages

Returns:

Array of language codes

Type: 
Array

getCurrentLanguage() → {string}

Get current language

Returns:

Current language code

Type: 
string

(async) loadTranslations(langCode, source) → {Promise}

Load translations asynchronously

Parameters:
NameTypeDescription
langCodestring

Language code

sourcestring | function

URL string or function returning translations

Returns:

Loading promise

Type: 
Promise

n(num, options) → {string}

Get formatted number

Parameters:
NameTypeDefaultDescription
numnumber0

Number to format

optionsObjectnull

Intl.NumberFormat options

Returns:

Formatted number

Type: 
string

removeLanguage(langCode)

Remove translations for a language

Parameters:
NameTypeDescription
langCodestring

Language code

setCurrentLanguage(langCode)

Set current language with validation and events

Parameters:
NameTypeDescription
langCodestring

Language code (e.g., 'en', 'en-US')

Throws:

Invalid language code

Type
Error

setDictionary(langCode, translations)

Set entire dictionary for a language

Parameters:
NameTypeDescription
langCodestring

Language code

translationsObject

Translation object

t(key, params) → {string}

Get translation with interpolation and pluralization

Parameters:
NameTypeDescription
keystring

Translation key (supports namespaces with dots)

paramsObject | Array

Parameters for interpolation

Returns:

Translated text

Type: 
string