Skip to content

Commit

Permalink
Ensure to always use the same Vue instance
Browse files Browse the repository at this point in the history
I used the same technique as in vue-router https://github.com/vuejs/vue-router/blob/473b25/src/install.js#L4

Thanx Radu for the bug report!
  • Loading branch information
kemar committed Dec 9, 2016
1 parent 3a2acb5 commit a95b42f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ var defaultConfig = {

let languageVm // Singleton.

export let _Vue

let GetTextPlugin = function (Vue, options = {}) {

Object.keys(options).forEach(key => {
Expand All @@ -26,6 +28,8 @@ let GetTextPlugin = function (Vue, options = {}) {
throw new Error('No translations available.')
}

_Vue = Vue

options = Object.assign(defaultConfig, options)

languageVm = new Vue({
Expand Down
8 changes: 4 additions & 4 deletions src/translate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Vue from 'vue'

import plurals from './plurals'
import { _Vue } from './index'

export default {

Expand All @@ -14,7 +13,7 @@ export default {
*
* @return {String} The translated string
*/
getTranslation: function (msgid, n = 1, context = null, language = Vue.config.language) {
getTranslation: function (msgid, n = 1, context = null, language = _Vue.config.language) {
if (!msgid) {
return '' // Allow empty strings.
}
Expand All @@ -24,7 +23,8 @@ export default {
// See the `Language` section in https://www.gnu.org/software/gettext/manual/html_node/Header-Entry.html
// So try `ll_CC` first, or the `ll` abbreviation which can be three-letter sometimes:
// https://www.gnu.org/software/gettext/manual/html_node/Language-Codes.html#Language-Codes
let translations = Vue.$translations[language] || Vue.$translations[language.split('_')[0]]

let translations = _Vue.$translations[language] || _Vue.$translations[language.split('_')[0]]
if (!translations) {
console.warn(`No translations found for ${language}`)
return msgid // Returns the untranslated string.
Expand Down

0 comments on commit a95b42f

Please sign in to comment.