From ca6bb321e7a6d69d108aec12e786994737987634 Mon Sep 17 00:00:00 2001 From: Dobromir Hristov Date: Wed, 30 Jun 2021 08:58:50 +0300 Subject: [PATCH] feature(core): pass more parameters to `$message` to help with i18n support (#881) * refactor: improve $message to work easier with i18n * chore: add tests * docs: add docs about i18n --- packages/docs/src/advanced_usage.md | 62 +++++++++- packages/test-project/main.js | 2 + packages/test-project/package.json | 1 + packages/test-project/src/App.vue | 1 + .../src/components/I18nSimpleForm.vue | 117 ++++++++++++++++++ packages/test-project/src/i18n.js | 23 ++++ packages/test-project/src/routes.js | 5 + packages/vuelidate/src/core.js | 19 ++- .../test/unit/specs/validation.spec.js | 48 ++++++- yarn.lock | 81 +++++++++++- 10 files changed, 345 insertions(+), 14 deletions(-) create mode 100644 packages/test-project/src/components/I18nSimpleForm.vue create mode 100644 packages/test-project/src/i18n.js diff --git a/packages/docs/src/advanced_usage.md b/packages/docs/src/advanced_usage.md index 17b8b337..fc25fb06 100644 --- a/packages/docs/src/advanced_usage.md +++ b/packages/docs/src/advanced_usage.md @@ -411,9 +411,69 @@ export default { To clear out the external results, you can again, use the `$clearExternalResults()` method ```js -async function validate() { +async function validate () { this.$v.$clearExternalResults() // perform validations const result = await this.runAsyncValidators() } ``` + +## i18n support + +Validator messages are very flexible. You can wrap each validator with your own helper, that returns a translated error message, based on the +validator name. Let's define a few validators: + +```js +// @/utils/validators.js +import { withI18nMessage } from '@/utils/withI18nMessage' +import * as validators from '@vuelidate/validators' + +export const required = withI18nMessage(validators.required) +export const minLength = withI18nMessage(validators.minLength) +``` + +Now lets see how that `withI18nMessage` helpers would look like: + +```js +// @/utils/withI18nMessage.js +import { i18n } from "@/i18n" + +const { t } = i18n.global || i18n // this should work for both Vue 2 and Vue 3 versions of vue-i18n + +export const withI18nMessage = (validator) => helpers.withMessage((props) => t(`messages.${props.$validator}`, { + model: props.$model, + property: props.$property, + ...props.$params +}), validator) +``` + +We can now use the validators as we normally do + +```vue + + +``` + +One drawback is that Vuelidate params passed to `$message` are prefixed with `$`, which Vue-i18n does not allow. So we would have to manually map any +parameter we need, to a new name parameter without `$`. + +We can now define our validator messages, with optional data inside each message. + +```json +{ + "messages": { + "required": "The field {property} is required.", + "minLength": "The {property} field has a value of '{model}', but it must have a min length of {min}." + } +} +``` diff --git a/packages/test-project/main.js b/packages/test-project/main.js index 2cd9f1cd..f03f8414 100644 --- a/packages/test-project/main.js +++ b/packages/test-project/main.js @@ -1,8 +1,10 @@ import { createApp } from 'vue' import App from './src/App.vue' import { router } from './src/router.js' +import { i18n } from './src/i18n' const app = createApp(App) app.use(router) +app.use(i18n) app.mount('#app') diff --git a/packages/test-project/package.json b/packages/test-project/package.json index 3656adb6..42852344 100644 --- a/packages/test-project/package.json +++ b/packages/test-project/package.json @@ -7,6 +7,7 @@ }, "dependencies": { "vue": "^3.0.1", + "vue-i18n": "^9.1.6", "vue-router": "^4.0.0-beta.6" }, "devDependencies": { diff --git a/packages/test-project/src/App.vue b/packages/test-project/src/App.vue index aae744fb..655fe5ad 100644 --- a/packages/test-project/src/App.vue +++ b/packages/test-project/src/App.vue @@ -3,6 +3,7 @@