[Vuejs]-Vue-i18n Cannot translate the value of keypath

0👍

i18n.js:

import Vue from 'vue'
import VueI18n from 'vue-i18n'

Vue.use(VueI18n)

function loadLocaleMessages () {
  const locales = require.context('./locales', true, /[A-Za-z0-9-_,\s]+\.json$/i)
  const messages = {}
  locales.keys().forEach(key => {
    const matched = key.match(/([A-Za-z0-9-_]+)\./i)
    if (matched && matched.length > 1) {
      const locale = matched[1]
      messages[locale] = locales(key)
    }
  })

  return messages
}

export default new VueI18n({
  locale: 'fi',
  fallbackLocale: process.env.VUE_APP_I18N_FALLBACK_LOCALE || 'fi',
  messages: loadLocaleMessages()
})

Make locales directory and create a Json file for each language like this:

   {
      "Welcome": 'Welcome',
      "Logout": 'Logout',
      "Login": 'Login',
      "Emailaddress": 'Email',
      "Password": 'Password',
      "Register": 'Register',
    }

Leave a comment