If you're a module author and want your module to install Nuxt i18n, you can do so using installModule()
but you will have to resolve paths used for vueI18n
, langDir
and those configured in locales
.
installModule()
, layers are merged by priority which allows projects to overwrite options as desired and will not cause conflicts if more than one layer provides options for the Nuxt i18n module.Note that when using installModule()
, the options passed will essentially have a higher priority than any layer (including the project layer), options are merged when possible and applicable but will otherwise override configurations.
Example:
import { createResolver, defineNuxtModule } from '@nuxt/kit'
export default defineNuxtModule({
async setup(options, nuxt) {
const { resolve } = createResolver(import.meta.url)
// paths needs to be resolved so absolute paths are used
await installModule('@nuxtjs/i18n', {
vueI18n: resolve('./i18n.config.ts'),
langDir: resolve('./lang'),
locales: [
{
code: 'en',
file: resolve('./lang/en.json'),
},
{
code: 'fr',
file: resolve('./lang/fr.json'),
},
]
})
}
})
Now the project has access to new messages and can use them through $t('my-module-example.hello')
.