[英]How to integrate VeeValidate with vue-i18n?
通过单击事件,我更改了 vue-i18n 语言,我想将 vee-validate 字典更改为相同的语言
main.js
import VeeValidate from 'vee-validate'
import validations from './validations.js'
import i18n from './lang'
Vue.use(VeeValidate)
new Vue({
router,
store,
i18n,
render: h => h(App)
}).$mount('#app')
vue-i18n 文件夹 > lang/index.js
import Vue from 'vue'
import VueI18n from 'vue-i18n'
import en from './locals/en'
import es from './locals/es'
Vue.use(VueI18n)
export default new VueI18n({
locale: 'en',
messages: {
en: {
lang: en
},
es: {
lang: es
}
}
})
vee-validate 文件夹 > src/validations.js
import {Validator} from 'vee-validate'
const dictionary = {
en: {
custom: {
signupEmail: {
required: 'Error',
},
}
},
es: {
custom: {
signupEmail: {
required: 'Hubo un error',
},
}
}
}
Validator.localize(dictionary)
const validator = new Validator()
validator.localize('en')
export default Validator
我试图定位validator.localize('en')并且我无法更改为es字典,即使我手动更改它validator.localize('es')。 我缺少什么?
看起来你很接近,但缺少一些关键部分。
当您连接 VeeValidate 时,您可以像这样传递object:
Vue.use(VeeValidate, {
i18nRootKey: 'custom', // customize the root path for validation messages.
i18n,
dictionary: {
en: {
custom: {
signupEmail: {
required: 'Error',
},
}
},
es: {
custom: {
signupEmail: {
required: 'Hubo un error',
},
}
}
}
});
显然我只是将您的字典内联在这里,最好将其保存为单独的文件并以这种方式导入。
我想出了自己该怎么做。
在已安装的循环钩子里面设置的 App.vue
VeeValidate.Validator.locale = `${language}`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.