i18n-t

1.0.1 · abandoned · verified Wed Apr 22

i18n-t is a minimalist internationalization (i18n) utility for JavaScript, offering a straightforward API to manage translations. Its current stable version is 1.0.1, last released in June 2016. The library focuses on simplicity, allowing developers to set a default locale, load translation messages from local files or pre-loaded JavaScript objects, and perform basic string translation with variable interpolation. Unlike more comprehensive and actively maintained solutions such as `i18next` or `i18n-js`, `i18n-t` explicitly avoids framework-specific integrations or advanced features like pluralization rules, context-sensitive translations, or backend loading mechanisms. Its key differentiator was its 'dumb' and easy-to-use nature, making it suitable for very basic i18n needs without external dependencies or complex configurations. However, due to its long-term abandonment, it lacks modern features like ESM support, TypeScript typings, and ongoing security updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `i18n-t`, load locales from a directory and pre-loaded objects, and then translate strings using the `t` method, including interpolation for variables and indexed placeholders.

const I18n = require('i18n-t');
const path = require('path');
const fs = require('fs');

// Create a dummy 'locales' directory and files for demonstration
const localesDir = path.join(__dirname, 'temp_locales');
if (!fs.existsSync(localesDir)) {
  fs.mkdirSync(localesDir);
}
fs.writeFileSync(path.join(localesDir, 'en.js'), 'module.exports = { HELLO: "Hello {{name}}", CATS: "{{0}} cats", DOGS: "No dogs" };');
fs.writeFileSync(path.join(localesDir, 'fr.js'), 'module.exports = { HELLO: "Bonjour {{name}}", CATS: "{{0}} chats" };');

const i18n = new I18n({
    defaultLocale: 'en'
});

// Load locales from the dummy directory
i18n.load(localesDir);

// Or using a pre-loaded object (can be used alongside or instead of load)
i18n.set({
    es: {
        HELLO: 'Hola {{name}}'
    }
});

// Translate sentences
console.log(i18n.t('en', 'HELLO', { name: 'Samy' }));
console.log(i18n.t('en', 'CATS', 10));
console.log(i18n.t('fr', 'HELLO', { name: 'Marie' }));
console.log(i18n.t('fr', 'CATS', 5));
console.log(i18n.t('es', 'HELLO', { name: 'Pedro' }));
console.log(i18n.t('en', 'DOGS'));
console.log(i18n.t('fr', 'DOGS')); // Will return key if not found in 'fr'

// Clean up dummy directory (optional, for a runnable example)
fs.unlinkSync(path.join(localesDir, 'en.js'));
fs.unlinkSync(path.join(localesDir, 'fr.js'));
fs.rmdirSync(localesDir);

view raw JSON →