A lightweight, zero-dependency library for translating web content: define a dictionary, create a translator, and translate a string, an element or a whole DOM tree in one call.
pnpm add eo-translatorjs
# or: npm install eo-translatorjs / yarn add eo-translatorjsThe package is also published to GitHub Packages as @eoussama/eo-translatorjs, and ships browser bundles for a plain <script> tag:
<script src="https://cdn.jsdelivr.net/npm/eo-translatorjs@3/dist/index.iife.js"></script>
<script>
const translator = new EOTranslator({ en: { greeting: "Hello" } }, "en");
</script>- One dictionary: keep every language in a plain object and switch the active language at any time.
- Nested keys: organise translations into namespaces and reach them with keys such as
nav.home. - Interpolation: fill
{placeholder}tokens from parameters, in code or from aneo-translator-paramsattribute. - DOM translation: mark elements with
eo-translatorattributes and translate one element or a whole container, as text or as HTML. - Safe fallbacks: missing keys and languages fall back to the key or to a fallback you choose.
- TypeScript-first: typed API, ESM and CommonJS builds with type declarations.
import EOTranslator from "eo-translatorjs";
const translator = new EOTranslator({
en: { greeting: "Hello, {name}!" },
fr: { greeting: "Bonjour, {name}!" },
}, "en");
translator.translate("greeting", { params: { name: "Luffy" } }); // "Hello, Luffy!"
translator.translate("greeting", { lang: "fr", params: { name: "Luffy" } }); // "Bonjour, Luffy!"
// Translates every [eo-translator] element on the page
translator.translateDOM();Full documentation, guides, and the API reference are available on the docs site.
- Docs site
- Getting started
- The translator
- Translating
- Translating the DOM
- Migration guide
- API reference
Try the library live in the demos: translate a string, a single element, or a whole container as you switch languages.
- Unit and integration tests (
pnpm test): Vitest, with a 100% coverage threshold. - Smoke tests (
pnpm test:smoke): import the built ESM and CommonJS packages. - End-to-end tests (
pnpm test:e2e): Playwright, covering a browser harness of translation scenarios and the built bundles, the docs site and every demo. Build the library and the docs first withpnpm buildandpnpm docs:build. - Mutation tests (
pnpm test:mutation): Stryker, failing below a 95% mutation score.
Requires Node.js 24 or later and pnpm 12.15.0. Recent pnpm versions switch to the pinned version from package.json on their own; npm is refused.
pnpm install
pnpm dev # rebuild the library on change
pnpm build # build the library
pnpm typecheck # type check with TypeScript 7
pnpm lint # lint
pnpm test # run unit and integration tests with coverage
pnpm docs:dev # start the docs site, demos included (/demos)
pnpm check # run every check CI runsIcon made by Freepik from Flaticon and is licensed by Creative Commons BY 3.0.
MIT