Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v-t rendering improvements #1713

Open
wants to merge 2 commits into
base: v8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions decls/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ declare type LocaleMessage = string | MessageFunction | LocaleMessageObject | Lo
declare type LocaleMessageObject = { [key: Path]: LocaleMessage };
declare type LocaleMessageArray = Array<LocaleMessage>;
declare type LocaleMessages = { [key: Locale]: LocaleMessageObject };
declare type LocaleTimestamp = { [key: Locale]: number };

// This options is the same as Intl.DateTimeFormat constructor options:
// http://www.ecma-international.org/ecma-402/2.0/#sec-intl-datetimeformat-constructor
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"babel-plugin-istanbul": "^6.0.0",
"babel-preset-power-assert": "^3.0.0",
"buble": "^0.19.3",
"chromedriver": "^121.0.0",
"chromedriver": "^126.0.0",
"core-js": "^3.6.5",
"cross-env": "^7.0.2",
"cross-spawn": "^7.0.3",
Expand Down
8 changes: 4 additions & 4 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function update (el: any, binding: Object, vnode: any, oldVNode: any): vo
const i18n: any = vnode.context.$i18n
if (localeEqual(el, vnode) &&
(looseEqual(binding.value, binding.oldValue) &&
looseEqual(el._localeMessage, i18n.getLocaleMessage(i18n.locale)))) { return }
el._timestamp == i18n._getLocaleTimestamp(i18n.locale))) { return }

t(el, binding, vnode)
}
Expand All @@ -34,8 +34,8 @@ export function unbind (el: any, binding: Object, vnode: any, oldVNode: any): vo
delete el['_vt']
el._locale = undefined
delete el['_locale']
el._localeMessage = undefined
delete el['_localeMessage']
el._timestamp = undefined
delete el['_timestamp']
}

function assert (el: any, vnode: any): boolean {
Expand Down Expand Up @@ -79,7 +79,7 @@ function t (el: any, binding: Object, vnode: any): void {
el._vt = el.textContent = vm.$i18n.t(path, ...makeParams(locale, args))
}
el._locale = vm.$i18n.locale
el._localeMessage = vm.$i18n.getLocaleMessage(vm.$i18n.locale)
el._timestamp = vm.$i18n._getLocaleTimestamp(vm.$i18n.locale)
}

function parseValue (value: any): Object {
Expand Down
9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default class VueI18n {
const fallbackLocale: FallbackLocale = options.fallbackLocale === false
? false
: options.fallbackLocale || 'en-US'
const timestamp: LocaleTimestamp = { [locale]: 0 }
const messages: LocaleMessages = options.messages || {}
const dateTimeFormats = options.dateTimeFormats || options.datetimeFormats || {}
const numberFormats = options.numberFormats || {}
Expand Down Expand Up @@ -179,6 +180,7 @@ export default class VueI18n {
locale,
fallbackLocale,
messages,
timestamp,
dateTimeFormats,
numberFormats
})
Expand Down Expand Up @@ -237,6 +239,7 @@ export default class VueI18n {
locale: Locale,
fallbackLocale: FallbackLocale,
messages: LocaleMessages,
timestamp: LocaleTimestamp,
dateTimeFormats: DateTimeFormats,
numberFormats: NumberFormats
}): void {
Expand Down Expand Up @@ -797,17 +800,23 @@ export default class VueI18n {
return looseClone(this._vm.messages[locale] || {})
}

_getLocaleTimestamp(locale: Locale): number {
return this._vm.timestamp[locale]
}

setLocaleMessage (locale: Locale, message: LocaleMessageObject): void {
if (this._warnHtmlInMessage === 'warn' || this._warnHtmlInMessage === 'error') {
this._checkLocaleMessage(locale, this._warnHtmlInMessage, message)
}
this._vm.$set(this._vm.timestamp, locale, Date.now())
this._vm.$set(this._vm.messages, locale, message)
}

mergeLocaleMessage (locale: Locale, message: LocaleMessageObject): void {
if (this._warnHtmlInMessage === 'warn' || this._warnHtmlInMessage === 'error') {
this._checkLocaleMessage(locale, this._warnHtmlInMessage, message)
}
this._vm.$set(this._vm.timestamp, locale, Date.now())
this._vm.$set(this._vm.messages, locale, merge(
typeof this._vm.messages[locale] !== 'undefined' && Object.keys(this._vm.messages[locale]).length
? Object.assign({}, this._vm.messages[locale])
Expand Down
Loading