title | description |
---|---|
Installing from a module |
How to install Nuxt i18n using `installModule` inside of a module. |
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
.
::callout
We strongly recommend using layers for complete module installations over using 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.
:br :br
If you would only like your module to provide translations, consider using the hook described in extend-messages instead. ::
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: ::code-group
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'),
},
]
})
}
})
{
"my-module-example": {
"hello": "Hello from external module"
}
}
{
"my-module-example": {
"hello": "Bonjour depuis le module externe"
}
}
::
Now the project has access to new messages and can use them through $t('my-module-example.hello')
.