@intl-schematic/plugin-arrays
Allows to use arrays as values in translation documents, adds many features to intl-schematic
:
- 💬 Define complex translations: use array elements as separate lines or join by a custom delimiter;
- 📑 Reference other keys: combine and compose multiple keys to save space in the translation document;
- ♻ Reuse pluginable keys: pass type-schecked parameters to referenced keys that use plugins;
- ⚙ Reuse parameters from referenced keys: reference parameters of other referenced keys to display them directly;
- 📃 JSON-validation using a JSON-schema: intellisense and popup hints right in the translation document;
- 🚫 No string-interpolation: translation strings will never be processed or mangled by-default, so all unicode symbols are safe to use;
npm i -s @intl-schematic/plugin-arrays
Usage
As an example of another key that uses a plugin, plugin-processors
will be used with default processors.
Define a translation document factory
const getDocument = () => ({
price: {
'intl/number': {
style: "currency",
currency: "USD",
currencyDisplay: "symbol",
trailingZeroDisplay: "stripIfInteger"
},
input: 0
},
birthday: {
'intl/date': {
year: "numeric",
month: "short",
day: "2-digit"
}
},
'for price': [
"for",
{ "price": [0] },
],
'until birthday': [
"until",
{ "birthday": [] }
],
gift: [
"Buy this birthday gift",
"for price",
"until birthday"
],
gifts: {
'intl/plural': {
one: 'gift',
other: 'gifts',
many: 'gifts',
}
},
'gifts amount': [
'0:gifts',
{ 'gifts': 0 }
]
});
Create a translator function (t()
)
import { createTranslator } from 'intl-schematic';
import { ArraysPlugin } from '@intl-schematic/plugin-arrays';
import { ProcessorsPlugin } from '@intl-schematic/plugin-processors';
import { defaultProcessors } from '@intl-schematic/plugin-processors/default';
const t = createTranslator(getDocument, [
ArraysPlugin(' '),
ProcessorsPlugin(defaultProcessors)
]);
Use the translator function
t('for price', {
price: [123]
});
t('gifts amount', {
gifts: [41]
});
t('gifts amount', {
gifts: [42]
});
t('for price', { price: [123, { currency: 'EUR' }] });
t('for price', { price: [123] }, ' - ');
t('gift', {
'for price': [{ price: [123] }],
'until birthday': [{ birthday: [new Date(2023, 7, 9)] }]
})
t('gift', {
'for price': [{ price: [123] }, ' just '],
'until birthday': [{ birthday: new Date(2023, 7, 9) }, ' - ']
}, (lines) => lines.join('\n'));
t('gift', new Date());
t('gift', { });
t('for price', { price: [new Date(), { currency: 'EUR' }] });
t('gift');
JSON-schema
To use this plugins' property json schema, simply follow this instruction and reference it using the unpkg link:
https://unpkg.com/@intl-schematic/plugin-arrays/property.schema.json