forked from nuxt-modules/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: options passed with
installModule
are overwritten (nuxt-module…
…s#2882) * fix: locales provided via `installModule` being ignored * fix: use `locales` and `vueI18n` passed by `installModule` * test: add `installModule` options tests * docs: add `installModule` documentation
- Loading branch information
1 parent
86d847c
commit 860dca9
Showing
12 changed files
with
172 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--- | ||
title: Installing from a module | ||
description: 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](/docs/guide/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](/docs/guide/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 | ||
|
||
```ts [my-module-example/module.ts] | ||
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'), | ||
}, | ||
] | ||
}) | ||
} | ||
}) | ||
``` | ||
|
||
```json [lang/en.json] | ||
{ | ||
"my-module-example": { | ||
"hello": "Hello from external module" | ||
} | ||
} | ||
``` | ||
|
||
```json [lang/fr.json] | ||
{ | ||
"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')`. | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { I18nOptions } from 'vue-i18n' | ||
|
||
export default { | ||
messages: { | ||
en: { | ||
installerModuleVueI18nMessage: 'Installer module vue-i18n works!' | ||
} | ||
} | ||
} as I18nOptions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { createResolver, defineNuxtModule, installModule } from '@nuxt/kit' | ||
|
||
export default defineNuxtModule({ | ||
async setup(options, nuxt) { | ||
const { resolve } = createResolver(import.meta.url) | ||
|
||
installModule('@nuxtjs/i18n', { | ||
langDir: resolve('./locales'), | ||
vueI18n: resolve('./i18n.config.ts'), | ||
locales: [ | ||
{ | ||
code: 'en', | ||
iso: 'en', | ||
files: [resolve('./locales/en.json')], | ||
name: 'English' | ||
} | ||
] | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"installerModuleLocaleMessage": "Installer module locale works!" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters