Simple i18n library with type defs and some in-string type syntax.
This package started as a module in several projects,
becoming a standalone more out of a desire to not reproduce code as much as possible!
This module is a PureESM package,
desiring to stay as current as possible in addition to maintaining maximum coverage & code exercise.
This is an implementation and refactor of this project.s
There are 2 parameters:
What this lets you do is feed in all the values you use, maybe from a shareable json, shared config, or some sort of dynamically imported list.
Some things we support:
Some goals:
Some things I want to add:
Some examples are in the src/test/i18n.spec.js
file.
Example:
import use from 'i18n-string-templates';
const locales = {
en: {
sample: 'sample',
'{0} debates {1}': '{0} debattes {1}',
},
zh: {
sample: '样本',
'{0} debates {1}': '{0}与{1}辩论',
},
};
// with english
let i18n = use(locales, 'en');
console.log(i18n`sample`); // outputs: sample
console.log(i18n`${'joe'} debates ${'tom'}`); // outputs: joe debattes tom
console.log(i18n`john needs ${241/12}:n(3) units of blood`) //outputs: john needs 20.083 units of blood
// with something else, like Simplified Chinese
i18n = use(locales, 'zh');
console.log(i18n`sample`); // outputs: 样本
console.log(i18n`${'约翰'} debates ${'刘易斯'}`); // outputs: 约翰与刘易斯辩论
console.log(i18n`john needs ${241/12}:n(3) units of blood`) //outputs: john needs 20.083 units of blood
// with a warnings object that you can access
const warnings = { untranslated: {} };
i18n = use(locales, 'en', { warnings });
console.log(i18n`non-extant`); // outputs: non-extant
console.log(JSON.stringify(warnings.untranslated)); //outputs: {"non-extant":"non-extant"}
We include a rudimentary “type” forcing syntax for forcing a type on a template parameter.
Suffix | Type |
---|---|
:s |
String |
:n(x) |
Number |
require()
this package.