0

I have looked through the other similar questions but none of them seem to apply to my case. I cannot get the translations to work every single one get the Cannot translate the value of keypath

the config looks like this

 export default function (app) {
  return {
    locales: [
      {
        code: 'sv',
        iso: 'sv-SE',
        file: 'sv-SE.js'
      },
      {
        code: 'en', // Make sure default is last in array
        iso: 'en-US',
        file: 'en-US.js'
      }
    ],
    strategy: 'no_prefix',
    lazy: true,
    langDir: 'lang/',
    defaultLocale: 'en',
    vueI18n: {
      fallbackLocale: {
        default: ['en']
      }
    }
  }
}

and then in lang/en-US.js it looks like this

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

In nuxt.config.json

  modules: [
    // https://go.nuxtjs.dev/pwa
    '@nuxtjs/pwa',
    '@nuxtjs/apollo',
    '@nuxtjs/i18n'
  ],

  i18n: '~/configs/i18n.config.js',

configs/i18n.config.js

export default function (app) {
      return {
        locales: [
          {
            code: 'sv',
            iso: 'sv-SE',
            file: 'sv-SE.js'
          },
          {
            code: 'en', // Make sure default is last in array
            iso: 'en-US',
            file: 'en-US.js'
          }
        ],
        strategy: 'no_prefix',
        lazy: true,
        langDir: '~lang/',
        defaultLocale: 'en',
        vueI18n: {
          fallbackLocale: {
            default: ['en']
          }
        }
      }
    }

One of the use cases

<label for="login-email" class="form-label text-muted fw-bold mb-1">{{
    $t("Emailaddress")
  }}</label>

But when I try to use any of these translations I get the warning that it cannot translate it and it uses no translation.

what am I missing here?

novafluff
  • 891
  • 1
  • 12
  • 30
  • Can you please add the code that you tried so far. Which help us more to find out the challenge you are facing. – Debug Diva Feb 17 '22 at 13:24
  • @CreativeLearner i have added a bit more context – novafluff Feb 17 '22 at 14:45
  • It should work. Your code looks fine. May I know is there any error you are getting ? – Debug Diva Feb 17 '22 at 16:04
  • @CreativeLearner i get warnings like [vue-i18n] Cannot translate the value of keypath 'EmailAddress'. Use the value of keypath as default. for each translation so it doesnt find any translation so for example $t{"EmailAddress"} just becomes EmailAddress on the UI and not E-Mail as the translation maps towards – novafluff Feb 18 '22 at 09:51
  • Does this answer your question? [Vue: vue-i18n: Cannot translate the value of keypath, Use the value of keypath as default](https://stackoverflow.com/questions/66490117/vue-vue-i18n-cannot-translate-the-value-of-keypath-use-the-value-of-keypath-a) – Debug Diva Feb 18 '22 at 09:54
  • @CreativeLearner tried that one but nuxt treats the vuei18n differently and i am following the official documentation at https://i18n.nuxtjs.org/setup – novafluff Feb 18 '22 at 09:59

1 Answers1

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',
    }
Ehsan A. Kian
  • 169
  • 1
  • 3
  • 9