Giter Club home page Giter Club logo

vue-polyglot's Introduction

Build Status

basic translation plugin for VueJS 2+

Vue-Polyglot

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

Demo

demo

Example

<div id="app">
  <h1>{{$t('title', 'Vue-Polyglot in English')}}</h1>
  <p>{{ createdBy }}</p>
  <p>
    <button type="button"
            v-for="lang in this.$polyglot.languagesAvailable"
            v-on:click="showAppIn(lang)">
      {{lang}}
    </button>
  </p>
</div>
import Polyglot from 'vue-polyglot';

Vue.use(window.Polyglot.default, {
  defaultLanguage: 'en',
  languagesAvailable: ['fr', 'es']
});

Vue.locales({
  'fr': {
    'title': 'Vue-Polyglot en Français',
    'createdBy': 'Créé par {user}',
  },
  'es': {
    'title': 'Vue-Polyglot en Español',
    'createdBy': 'Creado por {user}',
  }
});

new Vue({
  el: '#app',
  methods: {
    showAppIn: function(lang) {
      this.$polyglot.setLang({lang: lang});
    }
  },
  computed: {
    createdBy: function() {
      return this.$t('createdBy', 'Created by {user}', {user: 'Guillaume Vincent (@guillaume20100)'});
    }
  }
});

API

Vue.use(Polyglot, {
  defaultLanguage: 'en',
  languagesAvailable: ['zh', 'fr']
});

Vue.locales({
  'fr': {
    'hello': 'bonjour',
    'hiUser': 'bonjour {user}'
  },
  'zh': {
    'hello': '你好',
    'hiUser': '你好 {user}'
  }
})

$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'}) hello Guillaume
fr-FR this.$t('hiUser', 'hi {user}', {user: 'Guillaume'}) bonjour Guillaume

Loading translations synchronously

Set locales with Vue.locales option in your app:

Vue.locales({
  'fr': {
    'hello world': 'bonjour monde'
  },
  'zh': {
    'hello world': '你好,世界'
  }
});

Loading translation file asynchronously

this.$polyglot.getLocale(options = {baseURL = 'i18n', lang = 'auto'})

Vue.use(Polyglot, {
  defaultLanguage: 'en',
  languagesAvailable: ['zh', 'fr']
});

new Vue({
    el: '#test',
    beforeCreate() {
      this.$polyglot.getLocale({baseURL: 'dist/i18n'});
    },
    methods: {
      getTranslationAndShowAppInChinese() {
        this.$polyglot.getLocale({lang: 'zh'});
      }
    }
  })

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.

Changing the language to use

Use the setLang method of the $polyglot property, like this:

Vue.component({
  methods: {
    showAppInChinese() {
      this.$polyglot.setLang({lang: 'zh'});
    }
  }
});

Utils

See Vue-Polyglot-Utils

Similar plugin

License

License MIT (see LICENSE file)

vue-polyglot's People

Contributors

guillaumevincent avatar

Watchers

athiwatp avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.