basic translation plugin for VueJS 2+
Vue-Polyglot
notes
-
Vue-Polyglot doesn't get translation asynchronously in version 2+
-
This is not a plugin to integrate AirBnb's Polyglot.js into Vue, but a different plugin for managing translation in VueJs.
Installation
npm install --save vue-polyglot
TLDR
-
guess browser language and use it automatically
-
default message directly in your component
{{ $t('error_684', 'User already exists') }}
-
load data in translation
this.$t('helloUser', 'hello {user}', {user: 'toto'})
> hello toto
Demo
Example
{{$t('title', 'Vue-Polyglot in English')}} {{ createdBy }} {{lang}}
; Vue; Vue; el: "#app" methods: { this$polyglot; } computed: { return this; } ;
API
Vue; Vue;
$t(key[, fallbackMessage][, data])
browser locale | method | translated text |
---|---|---|
en-US |
$t('hello') |
hello |
zh-CN |
$t('hello') |
你好 |
fr-FR |
$t('hello') |
bonjour |
en-US |
$t('hello', 'Hello !') |
Hello ! |
es-ES |
this.$t('hiUser', 'hi {user}', {user: 'Guillaume'}) |
hi Guillaume |
fr-FR |
this.$t('hiUser', 'hi {user}', {user: 'Guillaume'}) |
bonjour Guillaume |
Loading translations
Set locales with Vue.locales
option in your app:
Vue;
Extend translations synchronously
Vue.locales replace all locales. If you want to update translations use extendLocales
method instead:
this$polyglot;
Changing the language to use
Use the setLang
method of the $polyglot
property, like this:
Vue;
Utils
License
License MIT (see LICENSE file)