From 5f6b2713e05a7c7a80c69b813dc5602b9f82cbbb Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Sat, 13 Aug 2016 23:05:24 +0900 Subject: [PATCH] :zap: improvement(translate): fallback translation warning --- src/extend.js | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/src/extend.js b/src/extend.js index 369032ceb..10a2eaa93 100644 --- a/src/extend.js +++ b/src/extend.js @@ -11,7 +11,7 @@ import Path from './path' */ export default function (Vue) { - const { isObject } = Vue.util + const { isObject, bind } = Vue.util const format = Format(Vue) const getValue = Path(Vue) @@ -37,7 +37,7 @@ export default function (Vue) { return { lang, fallback, params: args } } - function translate (locale, key, args) { + function interpolate (locale, key, args) { if (!locale) { return null } const val = getValue(locale, key) || locale[key] @@ -46,6 +46,23 @@ export default function (Vue) { return args ? format(val, args) : val } + function translate (getter, lang, fallback, key, params) { + let res = null + res = interpolate(getter(lang), key, params) + if (res) { return res } + + res = interpolate(getter(fallback), key, params) + if (res) { + if (process.env.NODE_ENV !== 'production') { + warn('Fall back to translate the keypath "' + key + '" with "' + fallback + '" language.') + } + return res + } else { + return null + } + } + + function warnDefault (key) { if (process.env.NODE_ENV !== 'production') { warn('Cannot translate the value of keypath "' + key + '". ' @@ -54,6 +71,13 @@ export default function (Vue) { return key } + function getAssetLocale (lang) { + return Vue.locale(lang) + } + + function getComponentLocale (lang) { + return this.$options.locales[lang] + } /** * Vue.t @@ -65,10 +89,8 @@ export default function (Vue) { Vue.t = (key, ...args) => { if (!key) { return '' } - const { lang, fallback, params } = parseArgs(...args) - return translate(Vue.locale(lang), key, params) - || translate(Vue.locale(fallback), key, params) + return translate(getAssetLocale, lang, fallback, key, params) || warnDefault(key) } @@ -83,12 +105,13 @@ export default function (Vue) { Vue.prototype.$t = function (key, ...args) { if (!key) { return '' } - const { lang, fallback, params } = parseArgs(...args) - return translate(this.$options.locales && this.$options.locales[lang], key, params) - || translate(this.$options.locales && this.$options.locales[fallback], key, params) - || translate(Vue.locale(lang), key, params) - || translate(Vue.locale(fallback), key, params) + let res = null + if (this.$options.locales) { + res = translate(bind(getComponentLocale, this), lang, fallback, key, params) + if (res) { return res } + } + return translate(getAssetLocale, lang, fallback, key, params) || warnDefault(key) }