Skip to content

Commit

Permalink
feat: add helpers to know if a message and a fallback message exists
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Oct 19, 2021
1 parent d0f499a commit 7ebf2be
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 4 deletions.
12 changes: 12 additions & 0 deletions adonis-typings/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 22 additions & 4 deletions src/I18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
*/
Expand All @@ -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 }

/**
Expand Down Expand Up @@ -205,7 +223,7 @@ export class I18n extends Formatter implements I18nContract {
data?: Record<string, any>,
fallbackMessage?: string
): string {
this.lazyLoadMessages()
this.lazyLoadTranslations()
const message = this.getMessage(identifier)

/**
Expand Down
62 changes: 62 additions & 0 deletions test/i18n.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
})
})

0 comments on commit 7ebf2be

Please sign in to comment.