1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
import i18next from '../src/i18next.js';
const instance = i18next.createInstance();
describe('i18next.translation.formatting', () => {
before(done => {
instance.init(
{
lng: 'en',
resources: {
en: {
translation: {
oneFormatterTest: 'The following text is uppercased: $t(key5, uppercase)',
anotherOneFormatterTest: 'The following text is underscored: $t(key6, underscore)',
twoFormattersTest:
'The following text is uppercased: $t(key5, uppercase). The following text is underscored: $t(key5, underscore)',
twoFormattersTogetherTest:
'The following text is uppercased, underscored, then uri component encoded: $t(key7, uppercase, underscore, encodeuricomponent)',
oneFormatterUsingAnotherFormatterTest:
'The following text is lowercased: $t(twoFormattersTogetherTest, lowercase)',
missingTranslationTest:
'No text will be shown when the translation key is missing: $t(, uppercase)',
key5: 'Here is some text',
key6: 'Here is some text with numb3r5',
key7: 'Here is some: text? with, (punctuation)',
withSpace: ' there',
keyWithNesting: 'hi$t(withSpace)',
},
},
},
interpolation: {
format: function(value, format, lng) {
if (format === 'uppercase') return value.toUpperCase();
if (format === 'lowercase') return value.toLowerCase();
if (format === 'underscore') return value.replace(/\s+/g, '_');
if (format === 'encodeuricomponent') return encodeURIComponent(value);
return value;
},
},
},
() => {
done();
},
);
});
describe('formatting', () => {
var tests = [
{
args: ['oneFormatterTest'],
expected: 'The following text is uppercased: HERE IS SOME TEXT',
},
{
args: ['anotherOneFormatterTest'],
expected: 'The following text is underscored: Here_is_some_text_with_numb3r5',
},
{
args: ['twoFormattersTest'],
expected:
'The following text is uppercased: HERE IS SOME TEXT. The following text is underscored: Here_is_some_text',
},
{
args: ['twoFormattersTogetherTest'],
expected:
'The following text is uppercased, underscored, then uri component encoded: HERE_IS_SOME%3A_TEXT%3F_WITH%2C_(PUNCTUATION)',
},
{
args: ['oneFormatterUsingAnotherFormatterTest'],
expected:
'The following text is lowercased: the following text is uppercased, underscored, then uri component encoded: here_is_some%3a_text%3f_with%2c_(punctuation)',
},
{
args: ['missingTranslationTest'],
expected: 'No text will be shown when the translation key is missing: ',
},
{
args: ['keyWithNesting'],
expected: 'hi there',
},
];
tests.forEach(test => {
it('correctly formats translations for ' + JSON.stringify(test.args), () => {
expect(instance.t.apply(instance, test.args)).to.eql(test.expected);
});
});
});
});
|