i am using vee-validate v.2 and i want to localize error massages .
i have wrote a plugin like this
import {configure} from 'vee-validate'
export default function ({app}){
configure({
defaultMessage:(field,values)=>{
values._field_=app.i18n.t(`fields.${field}`)
return app.i18n.t(`validation.${values._rule_}`,values)
}
})
}
and this is loacales/en.js
validation: {
required: "{_field_} can not be empty",
min: "{_field_} may not be Less than {length} characters",
confirmed: "{_field_} do not matches",
email: "{_field_} is not valid"
},
fields: {
email: 'Email',
password: 'Password',
userName: 'Username',
},
}
and Also this is $i18n in nuxt config
i18n: {
seo: false,
locales: [
{ code: 'en', iso: 'en-US', file: 'en.js' },
{ code: 'de', iso: 'de-GER', file: 'de.js' }
],
lazy: true,
langDir: 'locales/',
baseUrl: process.env.BASE_URL,
defaultLocale: 'de'
},
BUT it doesnt work ,
and pages dont shows
and i get error like this
How can do i fixed it ?
2
Answers
Let me explain my solution step by step :
Step 1) Install the required packages:
Step 2) Create a folder named /locales to the root of your project.
Step 3) Create a file /locales/en.js for English
Step 4) Create a file /locales/tr.js for Second Language (Türkçe)
Step 5) Create a i18n plugin file /plugins/i18n.js
Step 6) Create a validation plugin file /plugins/validate.js
Note: I used ‘required’ and ’email’ rule in this example if you want to use more rules you can visit the guide page and add to this file.
All rule list : https://vee-validate.logaretm.com/v2/guide/rules.html
Step 7) nuxt.config -> plugin Configuration
Step 8) nuxt.config -> i18n module Configuration
Step 9) nuxt.config -> vee-validate configuration add this :
Usage -> page : contact/index.vue
in the file
vee-validate.js
import configure method
then use it like
and in
i18n
translations folder add for every language