Extending messages hook
Nuxt hook to extend app's messages.
If you're a module author and want that module to provide extra messages for your project, you can merge them into the normally loaded messages by using the i18n:extend-messages
hook.
To do this, in your module's setup file listen to the Nuxt hook and push your messages. @nuxtjs/i18n
will do the rest.
This is particularly useful if your module use translated content and you want to offer to users nice default translations.
Example:
my-module-exemple/module1.ts
import { defineNuxtModule } from '@nuxt/kit'export default defineNuxtModule({ setup(options, nuxt) { nuxt.hook('i18n:extend-messages', async (additionalMessages, localeCodes) => { additionalMessages.push({ en: { 'my-module-exemple': { hello: 'Hello from external module' } }, fr: { 'my-module-exemple': { hello: 'Bonjour depuis le module externe' } } }) }) }})
Now the project has access to new messages and can use them through $t('my-module-exemple.hello')
.
The custom module that is use
i18n:extend-messages
hook should be inserted before nuxt i18n module.nuxt.config.ts
import CustomModule from './custom' // import your custom moduleexport default defineNuxtConfig({ modules: [ CustomModule, '@nuxtjs/i18n', ],})
Because module's messages are merged with the project's ones, it's safer to prefix them.Main project messages will always override the module's ones.