Skip to content

Commit

Permalink
feat(i18n): added setLocale function
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Jul 23, 2020
1 parent af88502 commit b5a1849
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
9 changes: 8 additions & 1 deletion packages/i18n/src/index.ts
Expand Up @@ -76,4 +76,11 @@ function localize(locale: string | RootI18nDictionary, dictionary?: PartialI18nD
return generateMessage;
}

export { localize };
/**
* Sets the locale
*/
function setLocale(locale: string) {
DICTIONARY.locale = locale;
}

export { localize, setLocale };
44 changes: 43 additions & 1 deletion packages/i18n/tests/index.spec.ts
@@ -1,7 +1,7 @@
import flushPromises from 'flush-promises';
import { defineRule, configure } from '@vee-validate/core';
import { required } from '@vee-validate/rules';
import { localize } from '@vee-validate/i18n';
import { localize, setLocale } from '@vee-validate/i18n';
import { mountWithHoc, setValue } from '../../core/tests/helpers';

defineRule('required', required);
Expand Down Expand Up @@ -126,3 +126,45 @@ test('falls back to the default message if rule without message exists', async (

expect(error.textContent).toContain('field is not valid');
});

test('can switch between locales with setLocale', async () => {
configure({
generateMessage: localize({
en: {
messages: {
required: 'This field is required',
},
},
ar: {
messages: {
required: 'هذا الحقل مطلوب',
},
},
}),
});

setLocale('en');

const wrapper = mountWithHoc({
template: `
<div>
<Field name="field" immediate rules="required" v-slot="{ field, errors }">
<input v-bind="field" type="text">
<span id="error">{{ errors[0] }}</span>
</Field>
</div>
`,
});

const error = wrapper.$el.querySelector('#error');

// flush the pending validation.
await flushPromises();

expect(error.textContent).toContain('This field is required');
setLocale('ar');
setValue(wrapper.$el.querySelector('input'), '');

await flushPromises();
expect(error.textContent).toContain('هذا الحقل مطلوب');
});

0 comments on commit b5a1849

Please sign in to comment.