description and source-codefunction transformPhrase(phrase, substitutions, locale) {
if (typeof phrase !== 'string') {
throw new TypeError('Polyglot.transformPhrase expects argument #1 to be string');
}
if (substitutions == null) {
return phrase;
}
var result = phrase;
// allow number as a pluralization shortcut
var options = typeof substitutions === 'number' ? { smart_count: substitutions } : substitutions;
// Select plural form: based on a phrase text that contains `n`
// plural forms separated by `delimeter`, a `locale`, and a `substitutions.smart_count`,
// choose the correct plural form. This is only done if `count` is set.
if (options.smart_count != null && result) {
var texts = split.call(result, delimeter);
result = trim(texts[pluralTypeIndex(locale || 'en', options.smart_count)] || texts[0]);
}
// Interpolate: Creates a `RegExp` object for each interpolation placeholder.
result = replace.call(result, tokenRegex, function (expression, argument) {
if (!has(options, argument) || options[argument] == null) { return expression; }
// Ensure replacement value is escaped to prevent special $-prefixed regex replace tokens.
return replace.call(options[argument], dollarRegex, dollarBillsYall);
});
return result;
}
example usage...
'صوتان',
'%{smart_count} أصوات',
'%{smart_count} صوت',
'%{smart_count} صوت'
].join(' |||| ');
it('does simple interpolation', function () {
expect(Polyglot.transformPhrase(simple, { name: 'Polyglot', attribute:
x27;awesome' })).to.equal('Polyglot is awesome');
});
it('removes missing keys', function () {
expect(Polyglot.transformPhrase(simple, { name: 'Polyglot' })).to.equal('Polyglot is %{attribute}');
});
it('selects the correct plural form based on smart_count', function () {
...