From 7ebf2be5d3a312a24832b5f288345cb6e72a9101 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Tue, 19 Oct 2021 07:59:13 +0530 Subject: [PATCH] feat: add helpers to know if a message and a fallback message exists --- adonis-typings/i18n.ts | 12 ++++++++ src/I18n/index.ts | 26 +++++++++++++++--- test/i18n.spec.ts | 62 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 4 deletions(-) diff --git a/adonis-typings/i18n.ts b/adonis-typings/i18n.ts index bec8bfa..c429460 100644 --- a/adonis-typings/i18n.ts +++ b/adonis-typings/i18n.ts @@ -218,6 +218,18 @@ declare module '@ioc:Adonis/Addons/I18n' { '*': ValidatorWildcardCallback } + /** + * Returns a boolean identifying if the message for a given + * identifier exists or not + */ + hasMessage(identifier: string): boolean + + /** + * Returns a boolean identifying if a fallback message for a given + * identifier exists or not + */ + hasFallbackMessage(identifier: string): boolean + /** * Format a message using its identifier. The message from the * fallback language is used when the message from current diff --git a/src/I18n/index.ts b/src/I18n/index.ts index c7bb265..ed86499 100644 --- a/src/I18n/index.ts +++ b/src/I18n/index.ts @@ -60,10 +60,10 @@ export class I18n extends Formatter implements I18nContract { } /** - * Lazy load messages. Doing this as i18n class usually results in switchLocale + * Lazy load translations. Doing this as i18n class usually results in switchLocale * during real world use cases */ - private lazyLoadMessages() { + private lazyLoadTranslations() { if (!this.localeTranslations && !this.fallbackTranslations) { this.loadTranslations() } @@ -135,6 +135,24 @@ export class I18n extends Formatter implements I18nContract { return this.formatRawMessage(message.message, data) } + /** + * Returns a boolean identifying if the message for a given + * identifier exists or not + */ + public hasMessage(identifier: string): boolean { + this.lazyLoadTranslations() + return this.localeTranslations[identifier] !== undefined + } + + /** + * Returns a boolean identifying if a fallback message for a given + * identifier exists or not + */ + public hasFallbackMessage(identifier: string): boolean { + this.lazyLoadTranslations() + return this.fallbackTranslations[identifier] !== undefined + } + /** * Switch locale for the current instance */ @@ -153,7 +171,7 @@ export class I18n extends Formatter implements I18nContract { } { return { '*': (field, rule, arrayExpressionPointer, options) => { - this.lazyLoadMessages() + this.lazyLoadTranslations() const data = { field, rule, options } /** @@ -205,7 +223,7 @@ export class I18n extends Formatter implements I18nContract { data?: Record, fallbackMessage?: string ): string { - this.lazyLoadMessages() + this.lazyLoadTranslations() const message = this.getMessage(identifier) /** diff --git a/test/i18n.spec.ts b/test/i18n.spec.ts index 5c82eab..c29f6af 100644 --- a/test/i18n.spec.ts +++ b/test/i18n.spec.ts @@ -401,4 +401,66 @@ test.group('I18n | validatorBindings', (group) => { assert.deepEqual(error.messages, { username: ['username is required to signup'] }) } }) + + test('find if a message exists', async (assert) => { + const app = await setup() + const emitter = app.container.resolveBinding('Adonis/Core/Event') + const logger = app.container.resolveBinding('Adonis/Core/Logger') + + await fs.add( + 'resources/lang/en/messages.json', + JSON.stringify({ + greeting: '', + }) + ) + + const i18nManager = new I18nManager(app, emitter, logger, { + defaultLocale: 'en', + translationsFormat: 'icu', + provideValidatorMessages: true, + loaders: { + fs: { + enabled: true, + location: join(fs.basePath, 'resources/lang'), + }, + }, + }) + + await i18nManager.loadTranslations() + + const i18n = new I18n('en', emitter, logger, i18nManager) + assert.isTrue(i18n.hasMessage('messages.greeting')) + assert.isFalse(i18n.hasMessage('messages.title')) + }) + + test('find if a fallback message exists', async (assert) => { + const app = await setup() + const emitter = app.container.resolveBinding('Adonis/Core/Event') + const logger = app.container.resolveBinding('Adonis/Core/Logger') + + await fs.add( + 'resources/lang/en/messages.json', + JSON.stringify({ + greeting: '', + }) + ) + + const i18nManager = new I18nManager(app, emitter, logger, { + defaultLocale: 'en', + translationsFormat: 'icu', + provideValidatorMessages: true, + loaders: { + fs: { + enabled: true, + location: join(fs.basePath, 'resources/lang'), + }, + }, + }) + + await i18nManager.loadTranslations() + + const i18n = new I18n('fr', emitter, logger, i18nManager) + assert.isFalse(i18n.hasMessage('messages.greeting')) + assert.isTrue(i18n.hasFallbackMessage('messages.greeting')) + }) })