2022-01-26 12:08:38 +05:30
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import Vue from 'vue';
|
2020-06-23 00:09:42 +05:30
|
|
|
import locale from '~/locale';
|
|
|
|
import Translate from '~/vue_shared/translate';
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
Vue.use(Translate);
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
describe('Vue translate filter', () => {
|
|
|
|
const createTranslationMock = (key, ...translations) => {
|
|
|
|
locale.textdomain('app');
|
|
|
|
|
|
|
|
locale.options.locale_data = {
|
|
|
|
app: {
|
|
|
|
'': {
|
|
|
|
domain: 'app',
|
|
|
|
lang: 'vo',
|
|
|
|
plural_forms: 'nplurals=2; plural=(n != 1);',
|
|
|
|
},
|
|
|
|
[key]: translations,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
it('translate singular text (`__`)', () => {
|
|
|
|
const key = 'singular';
|
|
|
|
const translation = 'singular_translated';
|
|
|
|
createTranslationMock(key, translation);
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
const wrapper = mount({
|
|
|
|
template: `
|
2020-06-23 00:09:42 +05:30
|
|
|
<span>
|
|
|
|
{{ __('${key}') }}
|
|
|
|
</span>
|
|
|
|
`,
|
2022-01-26 12:08:38 +05:30
|
|
|
});
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
expect(wrapper.text()).toBe(translation);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('translate plural text (`n__`) without any substituting text', () => {
|
|
|
|
const key = 'plural';
|
|
|
|
const translationPlural = 'plural_multiple translation';
|
|
|
|
createTranslationMock(key, 'plural_singular translation', translationPlural);
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
const wrapper = mount({
|
|
|
|
template: `
|
2020-06-23 00:09:42 +05:30
|
|
|
<span>
|
|
|
|
{{ n__('${key}', 'plurals', 2) }}
|
|
|
|
</span>
|
|
|
|
`,
|
2022-01-26 12:08:38 +05:30
|
|
|
});
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
expect(wrapper.text()).toBe(translationPlural);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('translate plural text (`n__`) with substituting %d', () => {
|
|
|
|
const key = '%d day';
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
createTranslationMock(key, '%d singular translated', '%d plural translated');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('and n === 1', () => {
|
2022-01-26 12:08:38 +05:30
|
|
|
const wrapper = mount({
|
|
|
|
template: `
|
2020-06-23 00:09:42 +05:30
|
|
|
<span>
|
|
|
|
{{ n__('${key}', '%d days', 1) }}
|
|
|
|
</span>
|
|
|
|
`,
|
2022-01-26 12:08:38 +05:30
|
|
|
});
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
expect(wrapper.text()).toBe('1 singular translated');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('and n > 1', () => {
|
2022-01-26 12:08:38 +05:30
|
|
|
const wrapper = mount({
|
|
|
|
template: `
|
2020-06-23 00:09:42 +05:30
|
|
|
<span>
|
|
|
|
{{ n__('${key}', '%d days', 2) }}
|
|
|
|
</span>
|
|
|
|
`,
|
2022-01-26 12:08:38 +05:30
|
|
|
});
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
expect(wrapper.text()).toBe('2 plural translated');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('translates text with context `s__`', () => {
|
|
|
|
const key = 'Context|Foobar';
|
|
|
|
const translation = 'Context|Foobar translated';
|
|
|
|
const expectation = 'Foobar translated';
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
createTranslationMock(key, translation);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('and using two parameters', () => {
|
2022-01-26 12:08:38 +05:30
|
|
|
const wrapper = mount({
|
|
|
|
template: `
|
2020-06-23 00:09:42 +05:30
|
|
|
<span>
|
|
|
|
{{ s__('Context', 'Foobar') }}
|
|
|
|
</span>
|
|
|
|
`,
|
2022-01-26 12:08:38 +05:30
|
|
|
});
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
expect(wrapper.text()).toBe(expectation);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('and using the pipe syntax', () => {
|
2022-01-26 12:08:38 +05:30
|
|
|
const wrapper = mount({
|
|
|
|
template: `
|
2020-06-23 00:09:42 +05:30
|
|
|
<span>
|
|
|
|
{{ s__('${key}') }}
|
|
|
|
</span>
|
|
|
|
`,
|
2022-01-26 12:08:38 +05:30
|
|
|
});
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
expect(wrapper.text()).toBe(expectation);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('translate multi line text', () => {
|
|
|
|
const translation = 'multiline string translated';
|
|
|
|
createTranslationMock('multiline string', translation);
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
const wrapper = mount({
|
|
|
|
template: `
|
2020-06-23 00:09:42 +05:30
|
|
|
<span>
|
|
|
|
{{ __(\`
|
|
|
|
multiline
|
|
|
|
string
|
|
|
|
\`) }}
|
|
|
|
</span>
|
|
|
|
`,
|
2022-01-26 12:08:38 +05:30
|
|
|
});
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
expect(wrapper.text()).toBe(translation);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('translate pluralized multi line text', () => {
|
|
|
|
const translation = 'multiline string plural';
|
|
|
|
|
|
|
|
createTranslationMock('multiline string', 'multiline string singular', translation);
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
const wrapper = mount({
|
|
|
|
template: `
|
2020-06-23 00:09:42 +05:30
|
|
|
<span>
|
|
|
|
{{ n__(
|
|
|
|
\`
|
|
|
|
multiline
|
|
|
|
string
|
|
|
|
\`,
|
|
|
|
\`
|
|
|
|
multiline
|
|
|
|
strings
|
|
|
|
\`,
|
|
|
|
2
|
|
|
|
) }}
|
|
|
|
</span>
|
|
|
|
`,
|
2022-01-26 12:08:38 +05:30
|
|
|
});
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
expect(wrapper.text()).toBe(translation);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('translate pluralized multi line text with context', () => {
|
|
|
|
const translation = 'multiline string with context';
|
|
|
|
|
|
|
|
createTranslationMock('Context| multiline string', translation);
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
const wrapper = mount({
|
|
|
|
template: `
|
2020-06-23 00:09:42 +05:30
|
|
|
<span>
|
|
|
|
{{ s__(
|
|
|
|
\`
|
|
|
|
Context|
|
|
|
|
multiline
|
|
|
|
string
|
|
|
|
\`
|
|
|
|
) }}
|
|
|
|
</span>
|
|
|
|
`,
|
2022-01-26 12:08:38 +05:30
|
|
|
});
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
expect(wrapper.text()).toBe(translation);
|
|
|
|
});
|
|
|
|
});
|