Skip to content

Commit f379661

Browse files
fix(core-base): fallback interpolation should not break in runtime only (fix #768) (#784)
* fix(core-base): fallback interpolation should not break in runtime only * style: lint
1 parent eaac280 commit f379661

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

packages/core-base/src/translate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ export function translate<
342342
fallbackFormat,
343343
postTranslation,
344344
unresolving,
345+
messageCompiler,
345346
fallbackLocale,
346347
messages
347348
} = context
@@ -368,7 +369,7 @@ export function translate<
368369
? options.default
369370
: key
370371
: fallbackFormat // default by `fallbackFormat` option
371-
? key
372+
? (!messageCompiler ? () => key : key)
372373
: ''
373374
const enableDefaultMsg = fallbackFormat || defaultMsgOrKey !== ''
374375
const locale = isString(options.locale) ? options.locale : context.locale

packages/core-base/test/translate.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,28 @@ describe('context fallbackFormat option', () => {
500500
`Not found 'hi, {name}!' key in 'en' locale messages.`
501501
)
502502
})
503+
504+
test('runtimeOnly', () => {
505+
const mockWarn = warn as jest.MockedFunction<typeof warn>
506+
mockWarn.mockImplementation(() => {})
507+
508+
const ctx = context({
509+
locale: 'en',
510+
fallbackFormat: true,
511+
messages: {
512+
en: {}
513+
}
514+
})
515+
ctx.messageCompiler = null
516+
517+
expect(translate(ctx, 'hi, {name}!', { name: 'kazupon' })).toEqual(
518+
'hi, {name}!'
519+
)
520+
expect(mockWarn).toHaveBeenCalledTimes(1)
521+
expect(mockWarn.mock.calls[0][0]).toEqual(
522+
`Not found 'hi, {name}!' key in 'en' locale messages.`
523+
)
524+
})
503525
})
504526

505527
describe('context unresolving option', () => {

0 commit comments

Comments
 (0)