Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue i18n - using it as a javascript object (not inside the template)?

Tags:

vue.js

vuejs2

Is it possible to use vue, vue-i18n only with javascript (as an object), not in the template ?

I want to use it in something like window.confirm, is that possible ?

Thanks.

like image 810
KevinHu Avatar asked Apr 17 '17 08:04

KevinHu


People also ask

How do I use i18n in JavaScript Vue?

The vue-i18n plugin provides components with access to the i18n instance through the $i18n variable. Simply set $i18n. locale to switch to a new locale. Now, after an app reload, you'll see a <select> element that allows us to change the current locale.

What is i18n in VUE JS?

Vue I18n is internationalization plugin of Vue. js. It easily integrates some localization features to your Vue. js Application.

Can I use JavaScript in Vue?

Single-file components and readability A Vue application/web page is built of components that represent encapsulated elements of your interface. Components can be written in HTML, CSS, and JavaScript without dividing them into separate files.


1 Answers

yes it's possible, i never used the plugin, but it looks pretty straightforward:

first you create the instance:

// Ready translated locale messages
const messages = {
  en: {
    message: {
      greeting: 'hi {name}'
    }
  },
  es: {
    message: {
      greeting: 'hola {name}'
    }
  }
}

// Create VueI18n instance with options
const i18n = new VueI18n({
  locale: 'es', // set locale
  messages, // set locale messages
})

(be aware 'const' is es6)

Then you can use it in any js where i18n var exists:

i18n.t('greeting', { name: 'kazupon' }) // -> hola kazupon

Doc:

http://kazupon.github.io/vue-i18n/en/started.html

http://kazupon.github.io/vue-i18n/en/migrations.html

like image 171
Gerardo Rosciano Avatar answered Nov 08 '22 11:11

Gerardo Rosciano