Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.

Commit ed758be

Browse files
committed
⚡ improvement(fallbackLocale): support reactivity
1 parent 0f0914d commit ed758be

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

src/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@ export default class VueI18n {
1616
_root: ?I18n
1717
_sync: boolean
1818
_fallbackRoot: boolean
19-
_fallbackLocale: Locale
2019
_missing: ?MissingHandler
2120
_exist: Function
2221
_watcher: any
2322

2423
constructor (options: I18nOptions = {}) {
2524
const locale: Locale = options.locale || 'en-US'
25+
const fallbackLocale: Locale = options.fallbackLocale || 'en-US'
2626
const messages: LocaleMessages = options.messages || {}
2727
this._vm = null
28-
this._fallbackLocale = options.fallbackLocale || 'en-US'
2928
this._formatter = options.formatter || new BaseFormatter()
3029
this._missing = options.missing
3130
this._root = options.root || null
@@ -37,10 +36,12 @@ export default class VueI18n {
3736
return !isNull(getPathValue(message, key))
3837
}
3938

40-
this._resetVM({ locale, messages })
39+
this._initVM({ locale, fallbackLocale, messages })
4140
}
4241

43-
_resetVM (data: { locale: Locale, messages: LocaleMessages }): void {
42+
_initVM (data: {
43+
locale: Locale, fallbackLocale: Locale, messages: LocaleMessages
44+
}): void {
4445
const silent = Vue.config.silent
4546
Vue.config.silent = true
4647
this._vm = new Vue({ data })
@@ -74,9 +75,9 @@ export default class VueI18n {
7475
this._vm.$set(this._vm, 'locale', locale)
7576
}
7677

77-
get fallbackLocale (): Locale { return this._fallbackLocale }
78+
get fallbackLocale (): Locale { return this._vm.fallbackLocale }
7879
set fallbackLocale (locale: Locale): void {
79-
this._fallbackLocale = locale
80+
this._vm.$set(this._vm, 'fallbackLocale', locale)
8081
}
8182

8283
get missing (): ?MissingHandler { return this._missing }

src/mixin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { isPlainObject, warn } from './util'
66
const $t = (vm: any): Function => {
77
// add dependency tracking !!
88
const locale: Locale = vm.$i18n.locale
9+
const fallback: Locale = vm.$i18n.fallbackLocal
910
const messages: LocaleMessages = vm.$i18n.vm.messages
1011
return (key: string, ...args: any): TranslateResult => {
1112
return vm.$i18n._t(key, locale, messages, vm, ...args)
@@ -14,6 +15,7 @@ const $t = (vm: any): Function => {
1415
const $tc = (vm: any): Function => {
1516
// add dependency tracking !!
1617
const locale: Locale = vm.$i18n.locale
18+
const fallback: Locale = vm.$i18n.fallbackLocal
1719
const messages: LocaleMessages = vm.$i18n.vm.messages
1820
return (key: string, choice?: number, ...args: any): TranslateResult => {
1921
return vm.$i18n._tc(key, locale, messages, vm, choice, ...args)

0 commit comments

Comments
 (0)