Vue-Polyglot
basic translation plugin for VueJS 2+.
Installation
npm install --save vue-polyglot
TLDR
-
can load translation asynchronously with HTTP requests (use
axios
module) -
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
Disclaimer:
This is not a plugin to integrate AirBnb's Polyglot.js into Vue, but a different implementation of its own.
Example
API
$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 synchronously
Set locales with Vue.locales
option in your app:
Loading translation file asynchronously
this.$polyglot.getLocale(options = {baseURL = 'i18n', lang = 'auto', ext = '.json'})
It will load asynchronously translations using browser's language.
For example if browser language is fr-FR
, languages available are ['zh', 'fr']
, this.$polyglot.getLocale({baseURL: 'dist/i18n'})
will get translation from dist/i18n/fr.json
file.
this.$polyglot.getLocale({lang: 'zh'})
will get translation from i18n/zh.json
file.
Extend translations synchronously
Vue.locales replace all locales. If you want to update translations use extendLocales
method instead:
Changing the language to use
Use the setLang
method of the $polyglot
property, like this: