innoboxrr-i18n
i18n
A tiny translation runtime for JavaScript. No dependencies, no build step.
Load translations, pick a locale, resolve keys. That is the whole surface.
import t, { addTranslations, setLocale } from 'innoboxrr-i18n';
addTranslations({
en: { 'Hello :name': 'Hello :name' },
es: { 'Hello :name': 'Hola :name' },
});
setLocale('es-MX');
t('Hello :name', { name: 'Homero' }); // → Hola Homero
API
| Export | What it does |
|---|---|
t(key, replace?) (default) |
The translation of key in the current locale, or key itself when there is none. :name placeholders are replaced from replace, every occurrence. |
addTranslations(source) |
Adds translations to the ones already loaded. A key loaded later wins. |
setTranslations(source) |
Replaces everything loaded so far. |
setLocale(locale) / getLocale() |
The current locale. An empty value is ignored. |
hasTranslation(key) |
Whether key has a non-empty translation in the current locale. |
source is either an object keyed by locale, or Vite's eager glob over JSON files:
addTranslations(import.meta.glob('/resources/locales/*.json', { eager: true }));
Several sources
A module can ship its own translations and the application can load its own on top. Load the module's first, so the application has the last word:
import { translations as catalog } from 'acme-catalog';
addTranslations(catalog);
addTranslations(import.meta.glob('/resources/locales/*.json', { eager: true }));
setLocale(document.documentElement.lang);
An empty translation ("Create": "", which is what locale-gen writes for a new key) means not translated yet: it never overrides a translation loaded before it, and with nothing else t() shows the key instead of a blank.
Locale matching
- The locale is the file name:
/resources/locales/es.jsonises. The rest of the path is ignored. es-MXuses theestranslations and overrides them withes-MXwhere both exist.esfalls back to a regional variant (es-MX) when there is no plaines.- Case and
_do not matter:es_MX.jsonmatchessetLocale('es-mx').
| Zero dependencies | Plain ES modules |
| Framework agnostic | Works anywhere — Vue, React, vanilla |
| Parameter replacement | :name placeholders, same convention as Laravel |
Install
npm install innoboxrr-i18n
Pairs naturally with innoboxrr-locale-generator, which collects the t('…') keys of a project into these JSON files.
Part of Innobox R&R — 52 open-source packages extracted from production work. innobox.systems
Related Packages
Elyra extension for localization -- find hardcoded strings, extract translations...
Version History
| Version | Released | License |
|---|---|---|
| 1.2.0 | MIT | |
| 1.1.0 | MIT | |
| 1.0.0 | MIT |