Skip to content

Commit

Permalink
⚡ improvement(mixin): add error throwings and a warning
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Mar 7, 2017
1 parent 891f8ea commit 0e4ac39
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/mixin.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
/* @flow */

import VueI18n from './index'
import { typeName, isPlainObject, warn } from './util'

export default {
computed: {
$t () {
// HACK: add dependency tracking !!
if (!this.$i18n) {
throw Error(`Failed in $t due to not find VueI18n instance`)
}
// add dependency tracking !!
const locale: string = this.$i18n.locale
const messages: Messages = this.$i18n.messages
return (key: string, ...args: any): string => {
Expand All @@ -14,7 +18,10 @@ export default {
},

$tc () {
// HACK: add dependency tracking !!
if (!this.$i18n) {
throw Error(`Failed in $tc due to not find VueI18n instance`)
}
// add dependency tracking !!
const locale: string = this.$i18n.locale
const messages: Messages = this.$i18n.messages
return (key: string, choice?: number, ...args: any): string => {
Expand All @@ -23,7 +30,10 @@ export default {
},

$te () {
// HACK: add dependency tracking !!
if (!this.$i18n) {
throw Error(`Failed in $te due to not find VueI18n instance`)
}
// add dependency tracking !!
const locale: string = this.$i18n.locale
const messages: Messages = this.$i18n.messages
return (key: string, ...args: any): boolean => {
Expand All @@ -35,22 +45,26 @@ export default {
beforeCreate () {
const options: any = this.$options
if (options.i18n) {
if (options.i18n instanceof VueI18n) {
if (typeName(options.i18n) === 'VueI18n') {
this.$i18n = options.i18n
} else {
} else if (isPlainObject(options.i18n)) {
// component local i18n
if (this.$root && this.$root.$i18n && this.$root.$i18n instanceof VueI18n) {
if (this.$root && this.$root.$i18n && typeName(this.$root.$i18n) === 'VueI18n') {
options.i18n.root = this.$root.$i18n
}
this.$i18n = new VueI18n(options.i18n)
} else {
if (process.env.NODE_ENV !== 'production') {
warn(`Cannot be interpreted 'i18n' options.`)
}
}
} else if (this.$root && this.$root.$i18n && this.$root.$i18n instanceof VueI18n) {
} else if (this.$root && this.$root.$i18n && typeName(this.$root.$i18n) === 'VueI18n') {
// root i18n
this.$i18n = this.$root.$i18n
}
},

beforeDestroy () {
destroyed () {
this.$i18n = null
}
}

0 comments on commit 0e4ac39

Please sign in to comment.