Constructor
new I18n(appInstance, options)
Constructor
Name | Type | Description |
---|---|---|
appInstance | App | InfrontJS App reference |
options | Object | Configuration options |
- Source
// 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)
Name | Type | Description |
---|---|---|
langCode | string | Language code |
translationObject | Object | Translations to add |
namespace | string | Optional namespace prefix |
- Source
clearAll()
Clear all translations
- Source
d(date, options) → {string}
Get formatted date/time
Name | Type | Default | Description |
---|---|---|---|
date | Date | Date to format | |
options | Object | null | Intl.DateTimeFormat options |
- Source
Formatted date
- Type:
- string
detectBrowserLanguage() → {string|null}
Detect browser language with improved locale support
- Source
Detected language code or null
- Type:
- string |
null
exists(key, langCode) → {boolean}
Check if translation exists
Name | Type | Default | Description |
---|---|---|---|
key | string | Translation key | |
langCode | string | null | Language code (optional, uses current if not provided) |
- Source
True if translation exists
- Type:
- boolean
expose()
Expose functions to global scope (optional)
- Source
getAvailableLanguages() → {Array}
Get all available languages
- Source
Array of language codes
- Type:
- Array
getCurrentLanguage() → {string}
Get current language
- Source
Current language code
- Type:
- string
(async) loadTranslations(langCode, source) → {Promise}
Load translations asynchronously
Name | Type | Description |
---|---|---|
langCode | string | Language code |
source | string | | URL string or function returning translations |
- Source
Loading promise
- Type:
- Promise
n(num, options) → {string}
Get formatted number
Name | Type | Default | Description |
---|---|---|---|
num | number | 0 | Number to format |
options | Object | null | Intl.NumberFormat options |
- Source
Formatted number
- Type:
- string
removeLanguage(langCode)
Remove translations for a language
Name | Type | Description |
---|---|---|
langCode | string | Language code |
- Source
setCurrentLanguage(langCode)
Set current language with validation and events
Name | Type | Description |
---|---|---|
langCode | string | Language code (e.g., 'en', 'en-US') |
- Source
Invalid language code
- Type
- Error
setDictionary(langCode, translations)
Set entire dictionary for a language
Name | Type | Description |
---|---|---|
langCode | string | Language code |
translations | Object | Translation object |
- Source
t(key, params) → {string}
Get translation with interpolation and pluralization
Name | Type | Description |
---|---|---|
key | string | Translation key (supports namespaces with dots) |
params | Object | | Parameters for interpolation |
- Source
Translated text
- Type:
- string