@serg.io/i18n

Internationalization using tagged template literals.

Usage no npm install needed!

<script type="module">
  import sergIoI18n from 'https://cdn.skypack.dev/@serg.io/i18n';
</script>

README

Introduction

This library allows you to use tagged template literals in your source code to generate content for different languages. This library provides the following functions:

  • load: This function can be used to load definitions.
  • i18n: Template literals can be tagged with i18n to retrieve definitions that have been previously loaded using the load function.

Installation

npm install @serg.io/i18n

Example

You must first load the text definitions:

import i18n, { load } from 'path/to/src/i18n.js';

// Load text definitions.
load({
    user: {
        firstName: 'First name',
        lastName: 'Last name',
    },
    submit: 'Submit',
});

Once your text definitions have been loaded, you can tag template literals using i18n. For instance, the following example will change the innerText of the button with the string "Submit":

import i18n, { load } from 'path/to/src/i18n.js';

// Find the submit button.
const button = document.querySelector('button[type="submit"]');

// Change its text using i18n.
button.innerText = i18n`submit`;

License

MIT.