i18n-t
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
-
TypeError: I18n is not a constructor
cause Attempting to call `I18n()` as a function instead of using `new I18n()`.fixInstantiate the I18n class using the `new` keyword: `const i18n = new I18n({ defaultLocale: 'en' });` -
Error: require() of ES Module [...] not supported.
cause Trying to `require()` an ES Module or `import` a CommonJS module in an incompatible environment or without proper transpilation.fixThis package is CommonJS. Ensure you are using `const I18n = require('i18n-t');` and that your project is configured for CommonJS, or migrate to an i18n library that supports ESM. -
Cannot find module 'i18n-t' or similar module resolution error
cause The package is not installed or the `node_modules` directory is not correctly resolved, or trying to `import` in a pure ESM context that doesn't understand CJS.fixRun `npm install i18n-t` to ensure the package is installed. If in an ESM project, consider using a different i18n library or a CJS-to-ESM wrapper if absolutely necessary, though migration is recommended.
Warnings
- breaking The `i18n-t` package has been abandoned since its last update in June 2016 (version 1.0.1). It receives no further development, bug fixes, or security updates. This poses significant risks for production applications.
- gotcha This library is CommonJS-only and does not provide ES Module (ESM) exports. Attempting to use `import` syntax will result in module resolution errors in modern JavaScript environments.
- gotcha The package does not ship with TypeScript type definitions. Integrating it into a TypeScript project will require manually creating declaration files (e.g., `i18n-t.d.ts`) to avoid type errors.
- gotcha When a translation key is not found for a given locale, `i18n-t` defaults to returning the translation key itself. This behavior might be unexpected compared to other libraries that offer fallback locales or explicit `missingTranslationHandler` functions.
Install
-
npm install i18n-t -
yarn add i18n-t -
pnpm add i18n-t
Imports
- I18n
import I18n from 'i18n-t';
const I18n = require('i18n-t'); - i18n.t
const i18n = new I18n({ defaultLocale: 'en' }); i18n.t('en', 'HELLO', { name: 'World' }); - i18n.load
const i18n = new I18n({ defaultLocale: 'en' }); i18n.load('./locales');
Quickstart
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);