Skip to content

Commit

Permalink
⚡ improvement(fallbackLocale): support reactivity
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Mar 20, 2017
1 parent 0f0914d commit ed758be
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ export default class VueI18n {
_root: ?I18n
_sync: boolean
_fallbackRoot: boolean
_fallbackLocale: Locale
_missing: ?MissingHandler
_exist: Function
_watcher: any

constructor (options: I18nOptions = {}) {
const locale: Locale = options.locale || 'en-US'
const fallbackLocale: Locale = options.fallbackLocale || 'en-US'
const messages: LocaleMessages = options.messages || {}
this._vm = null
this._fallbackLocale = options.fallbackLocale || 'en-US'
this._formatter = options.formatter || new BaseFormatter()
this._missing = options.missing
this._root = options.root || null
Expand All @@ -37,10 +36,12 @@ export default class VueI18n {
return !isNull(getPathValue(message, key))
}

this._resetVM({ locale, messages })
this._initVM({ locale, fallbackLocale, messages })
}

_resetVM (data: { locale: Locale, messages: LocaleMessages }): void {
_initVM (data: {
locale: Locale, fallbackLocale: Locale, messages: LocaleMessages
}): void {
const silent = Vue.config.silent
Vue.config.silent = true
this._vm = new Vue({ data })
Expand Down Expand Up @@ -74,9 +75,9 @@ export default class VueI18n {
this._vm.$set(this._vm, 'locale', locale)
}

get fallbackLocale (): Locale { return this._fallbackLocale }
get fallbackLocale (): Locale { return this._vm.fallbackLocale }
set fallbackLocale (locale: Locale): void {
this._fallbackLocale = locale
this._vm.$set(this._vm, 'fallbackLocale', locale)
}

get missing (): ?MissingHandler { return this._missing }
Expand Down
2 changes: 2 additions & 0 deletions src/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { isPlainObject, warn } from './util'
const $t = (vm: any): Function => {
// add dependency tracking !!
const locale: Locale = vm.$i18n.locale
const fallback: Locale = vm.$i18n.fallbackLocal
const messages: LocaleMessages = vm.$i18n.vm.messages
return (key: string, ...args: any): TranslateResult => {
return vm.$i18n._t(key, locale, messages, vm, ...args)
Expand All @@ -14,6 +15,7 @@ const $t = (vm: any): Function => {
const $tc = (vm: any): Function => {
// add dependency tracking !!
const locale: Locale = vm.$i18n.locale
const fallback: Locale = vm.$i18n.fallbackLocal
const messages: LocaleMessages = vm.$i18n.vm.messages
return (key: string, choice?: number, ...args: any): TranslateResult => {
return vm.$i18n._tc(key, locale, messages, vm, choice, ...args)
Expand Down

0 comments on commit ed758be

Please sign in to comment.