-
-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose hook for extending messages (#1319)
Co-authored-by: existe_deja <[email protected]> Co-authored-by: Rafal Chlodnicki <[email protected]>
- Loading branch information
1 parent
afd70b0
commit 696bd12
Showing
13 changed files
with
202 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
title: Extending messages hook | ||
description: "Nuxt hook to extend app's messages" | ||
position: 14 | ||
category: Guide | ||
--- | ||
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: | ||
|
||
```js{}[my-module-exemple/setup.js] | ||
export default function () { | ||
const { nuxt } = this | ||
nuxt.hook('i18n:extend-messages', function (additionalMessages) { | ||
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')`. | ||
|
||
<alert> | ||
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. | ||
</alert> |
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
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 @@ | ||
export default { | ||
home: 'langDir Homepage' | ||
} |
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 @@ | ||
export default { | ||
home: 'langDir Accueil' | ||
} |
19 changes: 19 additions & 0 deletions
19
test/fixture/extend-locales/modules/externalModule/index.js
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,19 @@ | ||
/** @type {import('@nuxt/types').Module} */ | ||
export default function () { | ||
const { nuxt } = this | ||
|
||
nuxt.hook('i18n:extend-messages', function (additionalMessages) { | ||
additionalMessages.push({ | ||
en: { | ||
'external-module': { | ||
hello: 'Hello external module' | ||
} | ||
}, | ||
fr: { | ||
'external-module': { | ||
hello: 'Bonjour module externe' | ||
} | ||
} | ||
}) | ||
}) | ||
} |
19 changes: 19 additions & 0 deletions
19
test/fixture/extend-locales/modules/externalModuleBis/index.js
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,19 @@ | ||
/** @type {import('@nuxt/types').Module} */ | ||
export default function () { | ||
const { nuxt } = this | ||
|
||
nuxt.hook('i18n:extend-messages', function (additionalMessages) { | ||
additionalMessages.push({ | ||
en: { | ||
'external-module-bis': { | ||
hello: 'Hello external module bis' | ||
} | ||
}, | ||
fr: { | ||
'external-module-bis': { | ||
hello: 'Bonjour module externe bis' | ||
} | ||
} | ||
}) | ||
}) | ||
} |
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,11 @@ | ||
import { resolve } from 'path' | ||
import BaseConfig from '../base.config' | ||
|
||
/** @type {import('@nuxt/types').NuxtConfig} */ | ||
const config = { | ||
...BaseConfig, | ||
buildDir: resolve(__dirname, '.nuxt'), | ||
srcDir: __dirname | ||
} | ||
|
||
module.exports = config |
Empty file.
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