Skip to content

Commit

Permalink
fix: formatMessage to use empty string as fallback message
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 12, 2023
1 parent 59e6caa commit 61e1cca
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
27 changes: 17 additions & 10 deletions src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,22 +139,29 @@ export class I18n extends Formatter {
this.#notifyForMissingTranslation(identifier, true)
}

if (message) {
return this.formatRawMessage(message.message, data)
}

/**
* Return a fallback message when identifier has no
* message.
* Return the inline fallback message (when defined)
*/
if (!message) {
return (
fallbackMessage ||
this.#i18nManager.getFallbackMessage(identifier, this.locale) ||
`translation missing: ${this.locale}, ${identifier}`
)
if (fallbackMessage !== undefined) {
return fallbackMessage
}

/**
* Return the global fallback message (when defined)
*/
const globalFallbackMessage = this.#i18nManager.getFallbackMessage(identifier, this.locale)
if (globalFallbackMessage !== undefined) {
return globalFallbackMessage
}

/**
* Format message
* Otherwise return error message string
*/
return this.formatRawMessage(message.message, data)
return `translation missing: ${this.locale}, ${identifier}`
}

/**
Expand Down
4 changes: 2 additions & 2 deletions stubs/detect_user_locale.stub
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class {{ middlewareName }} {
*/
protected getRequestLocale(ctx: HttpContext) {
const userLanguages = ctx.request.languages()
return i18n.getSupportedLocaleFor(userLanguages)
return i18nManager.getSupportedLocaleFor(userLanguages)
}

async handle(ctx: HttpContext, next: NextFn) {
Expand All @@ -44,7 +44,7 @@ export default class {{ middlewareName }} {
/**
* Assigning i18n property to the HTTP context
*/
ctx.i18n = i18nManager.locale(language || i18n.defaultLocale)
ctx.i18n = i18nManager.locale(language || i18nManager.defaultLocale)

/**
* Binding I18n class to the request specific instance of it.
Expand Down
29 changes: 29 additions & 0 deletions tests/i18n.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,35 @@ test.group('I18n', () => {
assert.equal(i18n.formatMessage('messages.greeting', { price: 100 }), 'The price is 100,00 $US')
})

test('define fallback message for missing translation', async ({ fs, assert }) => {
const i18nManager = new I18nManager(emitter, {
defaultLocale: 'en',
formatter: () => new IcuFormatter(),
loaders: [() => new FsLoader({ location: join(fs.basePath, 'resources/lang') })],
})

await i18nManager.loadTranslations()
const i18n = new I18n('fr', emitter, i18nManager)

const message = i18n.formatMessage('messages.greeting', {}, '')
assert.equal(message, '')
})

test('define fallback using global hook', async ({ fs, assert }) => {
const i18nManager = new I18nManager(emitter, {
defaultLocale: 'en',
fallback: () => '',
formatter: () => new IcuFormatter(),
loaders: [() => new FsLoader({ location: join(fs.basePath, 'resources/lang') })],
})

await i18nManager.loadTranslations()
const i18n = new I18n('fr', emitter, i18nManager)

const message = i18n.formatMessage('messages.greeting')
assert.equal(message, '')
})

test('report missing translations via events', async ({ fs, assert }) => {
await fs.createJson('resources/lang/en/messages.json', {
greeting: 'The price is {price, number, ::currency/INR}',
Expand Down

0 comments on commit 61e1cca

Please sign in to comment.