i18next-client is a comprehensive JavaScript internationalization library that provides translation, pluralization, and localization features for web applications. This is the deprecated client-side version (v1.11.5) that has been superseded by the unified i18next v2+ library that works across both client and server environments.
npm install i18next-clienti18nextGlobal browser usage:
// Automatically available as window.i18n when loaded via script tag
<script src="path/to/i18next.js"></script>CommonJS:
const i18n = require('i18next-client');AMD:
define(['i18next'], function(i18n) {
// use i18n
});jQuery integration:
// When jQuery is available, adds $.t() and $.fn.i18n() methods
<script src="path/to/jquery.js"></script>
<script src="path/to/i18next.js"></script>// Initialize with options
i18n.init({
lng: 'en-US',
fallbackLng: 'en',
resGetPath: 'locales/__lng__/__ns__.json',
ns: {
namespaces: ['translation'],
defaultNs: 'translation'
}
}, function(t) {
// Library is ready
console.log(i18n.t('welcome')); // "Welcome"
// Translation with interpolation
console.log(i18n.t('greeting', { name: 'John' })); // "Hello John"
// Pluralization
console.log(i18n.t('item', { count: 0 })); // "no items"
console.log(i18n.t('item', { count: 1 })); // "one item"
console.log(i18n.t('item', { count: 5 })); // "5 items"
});
// jQuery usage (when available)
$('#myElement').i18n(); // Translates elements with data-i18n attributes
console.log($.t('welcome')); // jQuery shortcut for translationi18next-client is built around several key components:
Main translation functionality with interpolation, pluralization, and context support. Handles variable replacement, plural forms, and contextual translations.
// Main translation function (both aliases available)
function t(key, options): string;
function translate(key, options): string;
// Check if translation exists
function exists(key, options): boolean;Library setup, configuration management, and initialization lifecycle. Handles resource loading, language detection, and feature configuration.
// Initialize the library
function init(options, callback): void;
// Check initialization status
function isInitialized(): boolean;Initialization and Configuration
Language detection, switching, and text direction handling. Manages current language state and supports automatic language detection.
// Set current language
function setLng(lng, options, callback): void;
// Get current language
function lng(): string;
// Get text direction for current language
function dir(): string; // 'ltr' or 'rtl'
// Detect browser language
function detectLanguage(): string;Dynamic loading, caching, and management of translation resources. Supports namespace organization and resource bundle operations.
// Add resource bundle
function addResourceBundle(lng, ns, resources, deep, overwrite): void;
// Check if resource bundle exists
function hasResourceBundle(lng, ns): boolean;
// Get resource bundle
function getResourceBundle(lng, ns): object;
// Remove resource bundle
function removeResourceBundle(lng, ns): void;DOM localization features and jQuery plugin methods. Automatically translates elements with data attributes and provides jQuery shortcuts.
// jQuery translation shortcut (when jQuery available)
$.t(key, options): string;
// jQuery plugin for DOM localization
$.fn.i18n(options): jQuery;Post-processing, utility functions, and advanced configuration access.
// Add custom post-processor
function addPostProcessor(name, processor): void;
// Apply variable replacement in strings
function applyReplacement(str, replacementHash, nestedKey, options): string;
// Restore previous global i18n reference
function noConflict(): object;
// Access to configuration options
const options: InitOptions;
// Access to plural extensions system
const pluralExtensions: {
rules: object;
addRule(lng, obj): void;
setCurrentLng(lng): void;
needsPlural(lng, count): boolean;
get(lng, count): number;
};
// Access to synchronization system
const sync: {
load(lngs, options, callback): void;
postMissing(lng, ns, key, defaultValue, lngs): void;
reload(callback): void;
};
// Access to utility functions
const functions: {
extend(target, source): object;
deepExtend(target, source, overwrite): object;
each(object, callback, args): object;
escape(data): string;
detectLanguage(): string;
toLanguages(lng, fallbackLng): string[];
applyReplacement(str, replacementHash, nestedKey, options): string;
};// Translation options interface
interface TranslationOptions {
// Language override
lng?: string;
// Fallback language(s)
fallbackLng?: string | string[];
// Namespace override
ns?: string;
// Variable interpolation values
[key: string]: any;
// Pluralization count
count?: number;
// Context for contextual translations
context?: string;
// Default value if translation not found
defaultValue?: string;
// Return nested objects as trees
returnObjectTrees?: boolean;
// Post-processing options
postProcess?: string | string[];
// Interpolation prefix/suffix overrides
interpolationPrefix?: string;
interpolationSuffix?: string;
}
// Initialization options interface
interface InitOptions {
// Target language
lng?: string;
// Fallback language(s)
fallbackLng?: string | string[];
// Languages to preload
preload?: string[];
// Namespace configuration
ns?: string | {
namespaces: string[];
defaultNs: string;
};
// Resource loading path
resGetPath?: string;
// Resource posting path
resPostPath?: string;
// Pre-loaded resource store
resStore?: object;
// Use local storage for caching
useLocalStorage?: boolean;
// Local storage expiration time
localStorageExpirationTime?: number;
// Interpolation settings
interpolationPrefix?: string;
interpolationSuffix?: string;
// Debug mode
debug?: boolean;
// Enable jQuery extensions
setJqueryExt?: boolean;
// Additional configuration options...
}