Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate VeeValidate with vue-i18n?

With a click event i change the vue-i18n language i want to change the vee-validate dictionary to the same language

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 folder > 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 folder > 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

Im trying to target the validator.localize('en') and i cant change to the es dictionary, even if i change it manually validator.localize('es'). What im missing?

like image 332
Alex Hunter Avatar asked Nov 07 '22 06:11

Alex Hunter


1 Answers

It looks like you're close, but missing a few key pieces.

You can pass an object like this when you wire up VeeValidate:

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',
        },    
       }
     }
   }
});

Obviously I just put your dictionary inline here, it would be much better to keep it as a separate file and import it that way.

like image 62
maxshuty Avatar answered Nov 12 '22 16:11

maxshuty