diff --git a/src/i18n.ts b/src/i18n.ts index 5275089..39cb16c 100644 --- a/src/i18n.ts +++ b/src/i18n.ts @@ -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}` } /** diff --git a/stubs/detect_user_locale.stub b/stubs/detect_user_locale.stub index 826fd16..657d99a 100644 --- a/stubs/detect_user_locale.stub +++ b/stubs/detect_user_locale.stub @@ -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) { @@ -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. diff --git a/tests/i18n.spec.ts b/tests/i18n.spec.ts index 4a1db06..a5de461 100644 --- a/tests/i18n.spec.ts +++ b/tests/i18n.spec.ts @@ -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}',