Skip to content

Commit

Permalink
📈 performance: fix translation performance issue
Browse files Browse the repository at this point in the history
Closes #165
  • Loading branch information
kazupon committed May 25, 2017
1 parent f9c5c1b commit 6032a51
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
export default function extend (Vue: any): void {
Vue.prototype.$t = function (key: Path, ...values: any): TranslateResult {
const i18n = this.$i18n
return i18n._t(key, i18n.locale, i18n.messages, this, ...values)
return i18n._t(key, i18n.locale, i18n._getMessages(), this, ...values)
}

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

Vue.prototype.$te = function (key: Path, locale?: Locale): boolean {
const i18n = this.$i18n
return i18n._te(key, i18n.locale, i18n.messages, locale)
return i18n._te(key, i18n.locale, i18n._getMessages(), locale)
}

Vue.prototype.$d = function (value: number | Date, ...args: any): DateTimeFormatResult {
Expand Down
22 changes: 13 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ export default class VueI18n {

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

get messages (): LocaleMessages { return looseClone(this._vm.messages) }
get dateTimeFormats (): DateTimeFormats { return looseClone(this._vm.dateTimeFormats) }
get numberFormats (): NumberFormats { return looseClone(this._vm.numberFormats) }
get messages (): LocaleMessages { return looseClone(this._getMessages()) }
get dateTimeFormats (): DateTimeFormats { return looseClone(this._getDateTimeFormats()) }
get numberFormats (): NumberFormats { return looseClone(this._getNumberFormats()) }

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

_getMessages (): LocaleMessages { return this._vm.messages }
_getDateTimeFormats (): DateTimeFormats { return this._vm.dateTimeFormats }
_getNumberFormats (): NumberFormats { return this._vm.numberFormats }

_warnDefault (locale: Locale, key: Path, result: ?any, vm: ?any): ?string {
if (!isNull(result)) { return result }
if (this.missing) {
Expand Down Expand Up @@ -250,7 +254,7 @@ export default class VueI18n {
}

t (key: Path, ...values: any): TranslateResult {
return this._t(key, this.locale, this.messages, null, ...values)
return this._t(key, this.locale, this._getMessages(), null, ...values)
}

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

return this._i(key, locale, this.messages, null, ...params)
return this._i(key, locale, this._getMessages(), null, ...params)
}

_tc (
Expand All @@ -301,7 +305,7 @@ export default class VueI18n {
}

tc (key: Path, choice?: number, ...values: any): TranslateResult {
return this._tc(key, this.locale, this.messages, null, choice, ...values)
return this._tc(key, this.locale, this._getMessages(), null, choice, ...values)
}

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

te (key: Path, locale?: Locale): boolean {
return this._te(key, this.locale, this.messages, locale)
return this._te(key, this.locale, this._getMessages(), locale)
}

getLocaleMessage (locale: Locale): LocaleMessageObject {
Expand Down Expand Up @@ -344,7 +348,7 @@ export default class VueI18n {
}

let ret = ''
const dateTimeFormats = this.dateTimeFormats
const dateTimeFormats = this._getDateTimeFormats()
if (key) {
let locale: Locale = _locale
if (isNull(dateTimeFormats[_locale][key])) {
Expand Down Expand Up @@ -413,7 +417,7 @@ export default class VueI18n {
}

let ret = ''
const numberFormats = this.numberFormats
const numberFormats = this._getNumberFormats()
if (key) {
let locale: Locale = _locale
if (isNull(numberFormats[_locale][key])) {
Expand Down

0 comments on commit 6032a51

Please sign in to comment.