Skip to content

Instantly share code, notes, and snippets.

@rmtuckerphx
Last active August 6, 2021 02:25
Show Gist options
  • Save rmtuckerphx/4fb3a64fe3376c7f481c848666f36919 to your computer and use it in GitHub Desktop.
Save rmtuckerphx/4fb3a64fe3376c7f481c848666f36919 to your computer and use it in GitHub Desktop.
Using i18next with nesting, interval post processing, and array access
const i18next = require("i18next");
const intervalPlural = require("i18next-intervalplural-postprocessor");
const resources = {
en: {
translation: {
"whatCanIBuyInterval": "(0)[$t(nest0)];(1)[$t(nest1)];(2)[$t(nest2)];(3-inf)[$t(nest3)];",
"nest0": "Check back later.",
"nest1": "You should try $t(listItems1).",
"nest2": "Learn more about $t(listItems2, {'conj': 'or'}).",
"nest3": "Try $t(listItems3, {'conj': 'or'}).",
"listItems1": "{{items.0.name}}",
"listItems2": "{{items.0.name}} {{conj}} {{items.1.name}}",
"listItems3": "{{items.0.name}}, {{items.1.name}}, {{conj}} {{items.2.name}}"
}
}
}
i18next.use(intervalPlural).init({
lng: 'en',
resources
});
// const items = [];
// const items = [
// {name: "Item A"},
// ];
// const items = [
// {name: "Item A"},
// {name: "Item B"},
// ];
// const items = [
// {name: "Item A"},
// {name: "Item B"},
// {name: "Item C"},
// ];
const items = [{
name: "Item A"
},
{
name: "Item B"
},
{
name: "Item C"
},
{
name: "Item D"
}
];
const text = i18next.t("whatCanIBuyInterval", {
postProcess: "interval",
count: items.length,
items
});
console.log(text);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment