Skip to content

Commit

Permalink
⭐ new(silent): add silent translation missing option
Browse files Browse the repository at this point in the history
Closes #139
  • Loading branch information
kazupon committed Apr 10, 2017
1 parent 12baa02 commit 29b3a17
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
5 changes: 4 additions & 1 deletion decls/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ declare type I18nOptions = {
missing?: MissingHandler,
root?: I18n, // for internal
fallbackRoot?: boolean,
sync?: boolean
sync?: boolean,
silentTranslationWarn?: boolean
};

declare interface I18n {
Expand All @@ -32,6 +33,8 @@ declare interface I18n {
set missing (handler: MissingHandler): void,
get formatter (): Formatter,
set formatter (formatter: Formatter): void,
get silentTranslationWarn (): boolean,
set silentTranslationWarn (silent: boolean): void,
getLocaleMessage (locale: Locale): LocaleMessage,
setLocaleMessage (locale: Locale, message: LocaleMessage): void,
t (key: Path, ...values: any): TranslateResult,
Expand Down
21 changes: 15 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default class VueI18n {
_missing: ?MissingHandler
_exist: Function
_watcher: any
_silentTranslationWarn: boolean

constructor (options: I18nOptions = {}) {
const locale: Locale = options.locale || 'en-US'
Expand All @@ -29,7 +30,12 @@ export default class VueI18n {
this._missing = options.missing || null
this._root = options.root || null
this._sync = options.sync === undefined ? true : !!options.sync
this._fallbackRoot = options.fallbackRoot === undefined ? true : !!options.fallbackRoot
this._fallbackRoot = options.fallbackRoot === undefined
? true
: !!options.fallbackRoot
this._silentTranslationWarn = options.silentTranslationWarn === undefined
? false
: !!options.silentTranslationWarn

this._exist = (message: Object, key: Path): boolean => {
if (!message || !key) { return false }
Expand Down Expand Up @@ -86,12 +92,15 @@ export default class VueI18n {
get formatter (): Formatter { return this._formatter }
set formatter (formatter: Formatter): void { this._formatter = formatter }

get silentTranslationWarn (): boolean { return this._silentTranslationWarn }
set silentTranslationWarn (silent: boolean): void { this._silentTranslationWarn = silent }

_warnDefault (locale: Locale, key: Path, result: ?any, vm: ?any): ?string {
if (!isNull(result)) { return result }
if (this.missing) {
this.missing.apply(null, [locale, key, vm])
} else {
if (process.env.NODE_ENV !== 'production') {
if (process.env.NODE_ENV !== 'production' && !this._silentTranslationWarn) {
warn(
`Cannot translate the value of keypath '${key}'. ` +
'Use the value of keypath as default.'
Expand All @@ -116,7 +125,7 @@ export default class VueI18n {
if (isPlainObject(message)) {
ret = message[key]
if (typeof ret !== 'string') {
if (process.env.NODE_ENV !== 'production') {
if (process.env.NODE_ENV !== 'production' && !this._silentTranslationWarn) {
warn(`Value of key '${key}' is not a string!`)
}
return null
Expand All @@ -128,7 +137,7 @@ export default class VueI18n {
if (typeof pathRet === 'string') {
ret = pathRet
} else {
if (process.env.NODE_ENV !== 'production') {
if (process.env.NODE_ENV !== 'production' && !this._silentTranslationWarn) {
warn(`Value of key '${key}' is not a string!`)
}
return null
Expand Down Expand Up @@ -166,7 +175,7 @@ export default class VueI18n {

res = this._interpolate(messages[fallback], key, args)
if (!isNull(res)) {
if (process.env.NODE_ENV !== 'production') {
if (process.env.NODE_ENV !== 'production' && !this._silentTranslationWarn) {
warn(`Fall back to translate the keypath '${key}' with '${fallback}' locale.`)
}
return res
Expand All @@ -183,7 +192,7 @@ export default class VueI18n {

const ret: any = this._translate(messages, locale, this.fallbackLocale, key, parsedArgs.params)
if (this._isFallbackRoot(ret)) {
if (process.env.NODE_ENV !== 'production') {
if (process.env.NODE_ENV !== 'production' && !this._silentTranslationWarn) {
warn(`Fall back to translate the keypath '${key}' with root locale.`)
}
if (!this._root) { throw Error('unexpected error') }
Expand Down
1 change: 1 addition & 0 deletions src/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default {
// component local i18n
if (this.$root && this.$root.$i18n && this.$root.$i18n instanceof VueI18n) {
options.i18n.root = this.$root.$i18n
options.i18n.silentTranslationWarn = this.$root.$i18n.silentTranslationWarn
}

// init locale messages via custom blocks
Expand Down
35 changes: 35 additions & 0 deletions test/unit/silent.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
describe('silent', () => {
it('should be suppressed translate warnings', done => {
const el = document.createElement('div')
const vm = new Vue({
i18n: new VueI18n({
locale: 'en',
silentTranslationWarn: true
}),
components: {
child1: { // translation with component
i18n: {
locale: 'en',
messages: {
en: { who: 'child1' },
ja: { who: '子1' }
}
},
render (h) { return h('div', {}, []) }
}
},
render (h) {
return h('div', {}, [
h('p', { ref: 'who' }, [this.$t('who')]),
h('child1', { ref: 'child1' })
])
}
}).$mount(el)

const child1 = vm.$refs.child1
nextTick(() => {
vm.$t('foo.bar.buz')
child1.$t('foo.bar.buz')
}).then(done)
})
})

1 comment on commit 29b3a17

@ssc-hrep3
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank's! 👍

Please sign in to comment.