H
H
HamSter2019-10-27 19:52:01
Vue.js
HamSter, 2019-10-27 19:52:01

How to use vue-i18n inside a script?

There is a small codeandbox
project I use the vue-i18n translator.
Hello World template:
<h1>{{msg}}</h1>
And script:

data() {
    return {
      msg: `{{ $t("welcomeMsg") }}`
    }
  }

Plugins/i18n.js :
const messages = {
  en: {
    welcomeMsg: "Welcome to Your Vue.js App",
  },
  es: {
    welcomeMsg: "Bienvenido a tu aplicación Vue.js",
  }
};

Question: How can I use vue-i18n inside a script like this msg:{{ $t("welcomeMsg") }}?
**PS:** `this.$t("welcomeMsg")` this works but doesn't translate!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2019-10-27
@HamSter007

computed: {
  msg() {
    return this.$t('welcomeMsg');
  },
},

P
Pavel Shvedov, 2019-10-27
@mmmaaak

export default {
  name: "HelloWorld",
  data() {
    return {
      msg: this.$t("welcomeMsg")
    }
  }
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question