forked from vuelidate/vuelidate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(core): pass more parameters to
$message
to help with i18n s…
…upport (vuelidate#881) * refactor: improve $message to work easier with i18n * chore: add tests * docs: add docs about i18n
- Loading branch information
1 parent
b259587
commit ca6bb32
Showing
10 changed files
with
345 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
packages/test-project/src/components/I18nSimpleForm.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<template> | ||
<div class="SimpleForm"> | ||
<div class="lang-switcher"> | ||
Change Language to: | ||
<a | ||
href="#" | ||
@click.prevent="$i18n.locale = 'bg'" | ||
>BG</a> | | ||
<a | ||
href="#" | ||
@click.prevent="$i18n.locale = 'en'" | ||
>EN</a> | ||
</div> | ||
<div> | ||
<label>name</label> | ||
<input | ||
v-model="name" | ||
type="text" | ||
> | ||
</div> | ||
<div> | ||
<label>twitter</label> | ||
<input | ||
v-model="social.twitter" | ||
type="text" | ||
> | ||
</div> | ||
<div> | ||
<label>github</label> | ||
<input | ||
v-model="social.github" | ||
type="text" | ||
> | ||
</div> | ||
<button @click="validate"> | ||
Validate | ||
</button> | ||
<button @click="v$.$touch"> | ||
$touch | ||
</button> | ||
<button @click="v$.$reset"> | ||
$reset | ||
</button> | ||
<div style="background: rgba(219, 53, 53, 0.62); color: #ff9090; padding: 10px 15px"> | ||
<p | ||
v-for="(error, index) of v$.$errors" | ||
:key="index" | ||
style="padding: 0; margin: 5px 0" | ||
> | ||
{{ error.$message }} | ||
</p> | ||
</div> | ||
<pre>{{ v$ }}</pre> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { ref, reactive, computed } from 'vue' | ||
import useVuelidate from '@vuelidate/core' | ||
import { required, helpers, minLength } from '@vuelidate/validators' | ||
import { i18n } from '../i18n' | ||
const { global: { t } } = i18n | ||
const { withAsync } = helpers | ||
const asyncValidator = withAsync((v) => { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve(v === 'aaaa') | ||
}, 2000) | ||
}) | ||
}) | ||
const withI18nMessage = (validator) => helpers.withMessage((props) => t(`messages.${props.$validator}`, { | ||
model: props.$model, | ||
property: props.$property, | ||
...props.$params | ||
}), validator) | ||
export default { | ||
name: 'I18nForm', | ||
setup () { | ||
const name = ref('given name') | ||
const social = reactive({ | ||
github: 'hi', | ||
twitter: 'hey' | ||
}) | ||
let v$ = useVuelidate( | ||
{ | ||
name: { | ||
required: withI18nMessage(required), | ||
asyncValidator: withI18nMessage(asyncValidator) | ||
}, | ||
social: { | ||
github: { minLength: withI18nMessage(minLength(computed(() => social.twitter.length))) }, | ||
twitter: { minLength: withI18nMessage(minLength(computed(() => name.value.length))) } | ||
} | ||
}, | ||
{ name, social }, | ||
{ $autoDirty: true } | ||
) | ||
return { name, v$, social } | ||
}, | ||
methods: { | ||
async validate () { | ||
const result = await this.v$.$validate() | ||
console.log('Result is', result) | ||
} | ||
} | ||
} | ||
</script> | ||
<style> | ||
.lang-switcher a { | ||
color: white; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { createI18n } from 'vue-i18n' | ||
|
||
const messages = { | ||
en: { | ||
messages: { | ||
required: '{property} is required', | ||
minLength: 'The {property} field has a value of "{model}", but must have a min length of {min}.', | ||
asyncValidator: '{property} should equal "aaaa", but it is "{model}".' | ||
} | ||
}, | ||
bg: { | ||
messages: { | ||
required: '{property} e задължително', | ||
minLength: 'Полето {property} има стойност "{model}", но трябва да е дълго поне {min} символа.', | ||
asyncValidator: '{property} трябва да е "aaaa", но е "{model}".' | ||
} | ||
} | ||
} | ||
|
||
export const i18n = createI18n({ | ||
locale: 'en', | ||
messages | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.