Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.

Commit b912d8a

Browse files
sebwaskazupon
authored andcommitted
⚡ improvement(missing): Add interpolation values to missing handler (#308) by @sebwas
* Correct small typo * Submit formatting values to missing handler * Remove unnecessary & harmful spread operators * Normalize values to be array, adapt test
1 parent d03dd00 commit b912d8a

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

src/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ export default class VueI18n {
169169
_getDateTimeFormats (): DateTimeFormats { return this._vm.dateTimeFormats }
170170
_getNumberFormats (): NumberFormats { return this._vm.numberFormats }
171171

172-
_warnDefault (locale: Locale, key: Path, result: ?any, vm: ?any): ?string {
172+
_warnDefault (locale: Locale, key: Path, result: ?any, vm: ?any, values: any): ?string {
173173
if (!isNull(result)) { return result }
174174
if (this._missing) {
175-
this._missing.apply(null, [locale, key, vm])
175+
this._missing.apply(null, [locale, key, vm, values])
176176
} else {
177177
if (process.env.NODE_ENV !== 'production' && !this._silentTranslationWarn) {
178178
warn(
@@ -277,7 +277,10 @@ export default class VueI18n {
277277
linkPlaceholder, host, interpolateMode, values
278278
)
279279
}
280-
translated = this._warnDefault(locale, linkPlaceholder, translated, host)
280+
translated = this._warnDefault(
281+
locale, linkPlaceholder, translated, host,
282+
Array.isArray(values) ? values : [values]
283+
)
281284

282285
// Replace the link with the translated
283286
ret = !translated ? ret : ret.replace(link, translated)
@@ -335,7 +338,7 @@ export default class VueI18n {
335338
if (!this._root) { throw Error('unexpected error') }
336339
return this._root.t(key, ...values)
337340
} else {
338-
return this._warnDefault(locale, key, ret, host)
341+
return this._warnDefault(locale, key, ret, host, values)
339342
}
340343
}
341344

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

test/unit/missing.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,29 @@ describe('missing', () => {
5050
i18n.t('foo.bar.buz')
5151
})
5252
})
53+
54+
describe('i18n missing values', () => {
55+
it('should receive the values for interpolation', done => {
56+
const testValues = {
57+
foo: 'bar',
58+
num: 1234
59+
}
60+
61+
const missing = (locale, key, vm, values) => {
62+
assert.equal('en', locale)
63+
assert.equal('cannot.find', key)
64+
// `values` is normalized to be an array.
65+
assert.equal('bar', values[0].foo)
66+
assert.equal(1234, values[0].num)
67+
done()
68+
}
69+
70+
const i18n = new VueI18n({
71+
locale: 'en',
72+
missing
73+
})
74+
75+
i18n.t('cannot.find', testValues)
76+
})
77+
})
5378
})

0 commit comments

Comments
 (0)