Skip to content

Commit

Permalink
⚡ improvement(missing): Add interpolation values to missing handler (#…
Browse files Browse the repository at this point in the history
…308) by @sebwas

* Correct small typo

* Submit formatting values to missing handler

* Remove unnecessary & harmful spread operators

* Normalize values to be array, adapt test
  • Loading branch information
sebwas authored and kazupon committed Mar 10, 2018
1 parent d03dd00 commit b912d8a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ export default class VueI18n {
_getDateTimeFormats (): DateTimeFormats { return this._vm.dateTimeFormats }
_getNumberFormats (): NumberFormats { return this._vm.numberFormats }

_warnDefault (locale: Locale, key: Path, result: ?any, vm: ?any): ?string {
_warnDefault (locale: Locale, key: Path, result: ?any, vm: ?any, values: any): ?string {
if (!isNull(result)) { return result }
if (this._missing) {
this._missing.apply(null, [locale, key, vm])
this._missing.apply(null, [locale, key, vm, values])
} else {
if (process.env.NODE_ENV !== 'production' && !this._silentTranslationWarn) {
warn(
Expand Down Expand Up @@ -277,7 +277,10 @@ export default class VueI18n {
linkPlaceholder, host, interpolateMode, values
)
}
translated = this._warnDefault(locale, linkPlaceholder, translated, host)
translated = this._warnDefault(
locale, linkPlaceholder, translated, host,
Array.isArray(values) ? values : [values]
)

// Replace the link with the translated
ret = !translated ? ret : ret.replace(link, translated)
Expand Down Expand Up @@ -335,7 +338,7 @@ export default class VueI18n {
if (!this._root) { throw Error('unexpected error') }
return this._root.t(key, ...values)
} else {
return this._warnDefault(locale, key, ret, host)
return this._warnDefault(locale, key, ret, host, values)
}
}

Expand All @@ -353,7 +356,7 @@ export default class VueI18n {
if (!this._root) { throw Error('unexpected error') }
return this._root.i(key, locale, values)
} else {
return this._warnDefault(locale, key, ret, host)
return this._warnDefault(locale, key, ret, host, [values])
}
}

Expand Down
25 changes: 25 additions & 0 deletions test/unit/missing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,29 @@ describe('missing', () => {
i18n.t('foo.bar.buz')
})
})

describe('i18n missing values', () => {
it('should receive the values for interpolation', done => {
const testValues = {
foo: 'bar',
num: 1234
}

const missing = (locale, key, vm, values) => {
assert.equal('en', locale)
assert.equal('cannot.find', key)
// `values` is normalized to be an array.
assert.equal('bar', values[0].foo)
assert.equal(1234, values[0].num)
done()
}

const i18n = new VueI18n({
locale: 'en',
missing
})

i18n.t('cannot.find', testValues)
})
})
})

0 comments on commit b912d8a

Please sign in to comment.