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

Commit 6032a51

Browse files
committed
📈 performance: fix translation performance issue
Closes #165
1 parent f9c5c1b commit 6032a51

2 files changed

Lines changed: 16 additions & 12 deletions

File tree

src/extend.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
export default function extend (Vue: any): void {
44
Vue.prototype.$t = function (key: Path, ...values: any): TranslateResult {
55
const i18n = this.$i18n
6-
return i18n._t(key, i18n.locale, i18n.messages, this, ...values)
6+
return i18n._t(key, i18n.locale, i18n._getMessages(), this, ...values)
77
}
88

99
Vue.prototype.$tc = function (key: Path, choice?: number, ...values: any): TranslateResult {
1010
const i18n = this.$i18n
11-
return i18n._tc(key, i18n.locale, i18n.messages, this, choice, ...values)
11+
return i18n._tc(key, i18n.locale, i18n._getMessages(), this, choice, ...values)
1212
}
1313

1414
Vue.prototype.$te = function (key: Path, locale?: Locale): boolean {
1515
const i18n = this.$i18n
16-
return i18n._te(key, i18n.locale, i18n.messages, locale)
16+
return i18n._te(key, i18n.locale, i18n._getMessages(), locale)
1717
}
1818

1919
Vue.prototype.$d = function (value: number | Date, ...args: any): DateTimeFormatResult {

src/index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ export default class VueI18n {
102102

103103
get vm (): any { return this._vm }
104104

105-
get messages (): LocaleMessages { return looseClone(this._vm.messages) }
106-
get dateTimeFormats (): DateTimeFormats { return looseClone(this._vm.dateTimeFormats) }
107-
get numberFormats (): NumberFormats { return looseClone(this._vm.numberFormats) }
105+
get messages (): LocaleMessages { return looseClone(this._getMessages()) }
106+
get dateTimeFormats (): DateTimeFormats { return looseClone(this._getDateTimeFormats()) }
107+
get numberFormats (): NumberFormats { return looseClone(this._getNumberFormats()) }
108108

109109
get locale (): Locale { return this._vm.locale }
110110
set locale (locale: Locale): void {
@@ -125,6 +125,10 @@ export default class VueI18n {
125125
get silentTranslationWarn (): boolean { return this._silentTranslationWarn }
126126
set silentTranslationWarn (silent: boolean): void { this._silentTranslationWarn = silent }
127127

128+
_getMessages (): LocaleMessages { return this._vm.messages }
129+
_getDateTimeFormats (): DateTimeFormats { return this._vm.dateTimeFormats }
130+
_getNumberFormats (): NumberFormats { return this._vm.numberFormats }
131+
128132
_warnDefault (locale: Locale, key: Path, result: ?any, vm: ?any): ?string {
129133
if (!isNull(result)) { return result }
130134
if (this.missing) {
@@ -250,7 +254,7 @@ export default class VueI18n {
250254
}
251255

252256
t (key: Path, ...values: any): TranslateResult {
253-
return this._t(key, this.locale, this.messages, null, ...values)
257+
return this._t(key, this.locale, this._getMessages(), null, ...values)
254258
}
255259

256260
_i (key: Path, locale: Locale, messages: LocaleMessages, host: any, ...values: any): any {
@@ -282,7 +286,7 @@ export default class VueI18n {
282286
params.push(values[i])
283287
}
284288

285-
return this._i(key, locale, this.messages, null, ...params)
289+
return this._i(key, locale, this._getMessages(), null, ...params)
286290
}
287291

288292
_tc (
@@ -301,7 +305,7 @@ export default class VueI18n {
301305
}
302306

303307
tc (key: Path, choice?: number, ...values: any): TranslateResult {
304-
return this._tc(key, this.locale, this.messages, null, choice, ...values)
308+
return this._tc(key, this.locale, this._getMessages(), null, choice, ...values)
305309
}
306310

307311
_te (key: Path, locale: Locale, messages: LocaleMessages, ...args: any): boolean {
@@ -310,7 +314,7 @@ export default class VueI18n {
310314
}
311315

312316
te (key: Path, locale?: Locale): boolean {
313-
return this._te(key, this.locale, this.messages, locale)
317+
return this._te(key, this.locale, this._getMessages(), locale)
314318
}
315319

316320
getLocaleMessage (locale: Locale): LocaleMessageObject {
@@ -344,7 +348,7 @@ export default class VueI18n {
344348
}
345349

346350
let ret = ''
347-
const dateTimeFormats = this.dateTimeFormats
351+
const dateTimeFormats = this._getDateTimeFormats()
348352
if (key) {
349353
let locale: Locale = _locale
350354
if (isNull(dateTimeFormats[_locale][key])) {
@@ -413,7 +417,7 @@ export default class VueI18n {
413417
}
414418

415419
let ret = ''
416-
const numberFormats = this.numberFormats
420+
const numberFormats = this._getNumberFormats()
417421
if (key) {
418422
let locale: Locale = _locale
419423
if (isNull(numberFormats[_locale][key])) {

0 commit comments

Comments
 (0)