Search code examples
javascripttypescriptvue.jsvuejs3vue-i18n

Locale messages with using i18n custom block for Vue 3 + Vuetify 3


I want to have my locale messages separately for every component. I found an example how to do it for Vue 2, but I can't find how to do it for Vue 3 and Vuetify 3. This is what I've done:

package.json

  "dependencies": {
    "@mdi/font": "6.5.95",
    "core-js": "^3.8.3",
    "roboto-fontface": "*",
    "vue": "^3.2.13",
    "vue-class-component": "^8.0.0-0",
    "vue-i18n": "^9.1.2",
    "vue-router": "^4.0.3",
    "vuetify": "npm:@vuetify/nightly@next",
    "vuex": "^4.0.0",
    "webfontloader": "^1.0.0"
  },
  "devDependencies": {
    "@kazupon/vue-i18n-loader": "^0.3.0",  <--- This one is used for Vue 2.
    "@babel/core": "^7.12.16",
    "@babel/eslint-parser": "^7.12.16",
    "@types/webfontloader": "^1.6.34",
    "@typescript-eslint/eslint-plugin": "^5.4.0",
    "@typescript-eslint/parser": "^5.4.0",
    "@vue/cli-plugin-babel": "~5.0.0-rc.1",
    "@vue/cli-plugin-eslint": "~5.0.0-rc.1",
    "@vue/cli-plugin-router": "~5.0.0-rc.1",
    "@vue/cli-plugin-typescript": "~5.0.0-rc.1",
    "@vue/cli-plugin-vuex": "~5.0.0-rc.1",
    "@vue/cli-service": "~5.0.0-rc.1",
    "@vue/eslint-config-typescript": "^9.1.0",
    "eslint": "^7.32.0",
    "eslint-plugin-vue": "^8.0.3",
    "less": "^4.1.1",
    "less-loader": "7.3.0",
    "sass": "^1.38.0",
    "sass-loader": "^10.0.0",
    "typescript": "~4.1.5",
    "vue-cli-plugin-vue-i18n": "~1.0.1",
    "vue-cli-plugin-vuetify": "~2.4.5",
    "vuetify-loader": "^2.0.0-alpha.0"
  },

My component.vue:

<template>
  <v-card-actions>
    <v-tooltip left>
      <template v-slot:activator="{ props }">
        <v-btn v-bind="props">
          <v-icon size="25">mdi-home</v-icon>
        </v-btn>
      </template>
      <span>{{ $t('hello') }}</span>
    </v-tooltip>
  </v-card-actions>
</template>

<i18n>
{
  "en": {
    "hello": "Hello, world!"
  },
  "ru": {
    "hello": "Привет, мир"
  }
}
</i18n>

And main.ts:

const app = createApp(App)
installI18n(app)

const i18n = createI18n({
  locale: 'en',
  messages: {
  }
})

app
  .use(i18n)
  .use(vuetify)
  .mount('#app')

However, when I run my component, I don't see Hello, world! in the tooltip. Could anyone help to make it work?

EDIT: I tried to use @intlify/vue-i18n-loader instead of @kazupon/vue-i18n-loader but it didn't help. This test project is available on github


Solution

  • In addition to installing @intlify/vue-i18n-loader (instead of @kazupon/vue-i18n-loader) per the other answer by @BoussadjraBrahim, the loader must be configured in your Vue CLI project. That config is missing from your vue.config.js, so the <i18n> blocks are ignored.

    Add the following config to enable processing of the <i18n> blocks in SFCs:

    // vue.config.js
    module.exports = {
      chainWebpack: (config) => {
        config.module
          .rule('i18n')
          .resourceQuery(/blockType=i18n/)
          .type('javascript/auto')
          .use('i18n')
          .loader('@intlify/vue-i18n-loader')
          .end()
      },
      ⋮
    }
    

    screencast

    demo