Lightweight (~100 lines, <1KB) translation (i18n) module
Note
This module itself does not support loading translation files from the file system, the network, etc. Handle this on your own.
npm install @auioc/i18nimport I18n from '@auioc/i18n';
const i18n = new I18n();<script src="https://unpkg.com/@auioc/i18n/dist/browser/i18n.js"></script>
<script>
const i18n = new I18n();
</script><script type="module">
import I18n from "https://esm.sh/@auioc/i18n";
// import I18n from "https://unpkg.com/@auioc/i18n/dist/index.js";
const i18n = new I18n();
</script>// en.json
{
"hello": "Hello",
// Positional parameters
"greeting_1": "Hello, {0}",
// Named parameters
"greeting_2": "Hello, {name}",
// Singular or plural form
"item": { "one": "Item", "other": "Items" },
// The following two are equivalent
"user.profile.title": "Profile",
"user": { "profile": { "title": "Profile" } }
}import I18n from '@auioc/i18n';
// This module itself does not support loading translation files
// from the file system, the network, etc.
// Handle this on your own.
import en from './i18n/en.json' with { type: 'json' };
const i18n = new I18n('en', en);
// Recommended: Use a convenient function object
const t = i18n.t();
t('hello'); // "Hello"
t('greeting_1', 'John'); // "Hello, John"
t.t('greeting_1', 'John'); // "Hello, John"
t.k('greeting_2', { name: 'John' }); // "Hello, John"
t.n('item', 5); // "Items"
t.nk('item', 5); // "Items"
// Use the I18n instance methods directly
i18n.translate('hello'); // "Hello"
i18n.translate('greeting', { name: 'John' }); // "Hello, John"
i18n.translatePlural('item', 1); // "Item"
i18n.translatePlural('item', 5); // "Items"- Improve docs
Package @auioc/i18n is licensed under the MIT License.
The full license is in the LICENSE file.