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.
feat: support extending messages hook (nuxt-modules#1550)
* feat: support extending messages hook * add e2e spec
- Loading branch information
Showing
20 changed files
with
317 additions
and
66 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
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 { defineNuxtModule } from '@nuxt/kit' | ||
|
||
export default defineNuxtModule({ | ||
async setup(options, nuxt) { | ||
// @ts-ignore | ||
await nuxt.hook('i18n:extend-messages', (messages, localeCodes) => { | ||
messages.push({ | ||
en: { | ||
foo: 'Foo' | ||
}, | ||
fr: { | ||
foo: 'Foo FR' | ||
}, | ||
ja: { | ||
foo: 'Foo JA' | ||
} | ||
}) | ||
}) | ||
} | ||
}) |
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,30 @@ | ||
import { test, expect } from 'vitest' | ||
import { fileURLToPath } from 'node:url' | ||
import { setup, url, createPage } from '@nuxt/test-utils' | ||
import { getText } from './helper' | ||
|
||
await setup({ | ||
rootDir: fileURLToPath(new URL(`./fixtures/basic`, import.meta.url)), | ||
browser: true, | ||
// overrides | ||
nuxtConfig: { | ||
i18n: { | ||
defaultLocale: 'en' | ||
} | ||
} | ||
}) | ||
|
||
test('extend message hook', async () => { | ||
const home = url('/') | ||
const page = await createPage() | ||
const messages: string[] = [] | ||
page.on('console', msg => messages.push(msg.text())) | ||
await page.goto(home) | ||
|
||
expect(await getText(page, '#extend-message')).toEqual('Hello from external module') | ||
|
||
// click `fr` lang switch link | ||
await page.locator('#lang-switcher-with-nuxt-link a').click() | ||
|
||
expect(await getText(page, '#extend-message')).toEqual('Bonjour depuis le module externe') | ||
}) |
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,21 @@ | ||
import { defineNuxtModule } from '@nuxt/kit' | ||
|
||
export default defineNuxtModule({ | ||
async setup(options, nuxt) { | ||
// @ts-ignore | ||
await nuxt.hook('i18n:extend-messages', (messages, localeCodes) => { | ||
messages.push({ | ||
en: { | ||
'my-module-exemple': { | ||
hello: 'Hello from external module' | ||
} | ||
}, | ||
fr: { | ||
'my-module-exemple': { | ||
hello: 'Bonjour depuis le module externe' | ||
} | ||
} | ||
}) | ||
}) | ||
} | ||
}) |
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,31 @@ | ||
import createDebug from 'debug' | ||
|
||
import type { Nuxt } from '@nuxt/schema' | ||
import type { DefineLocaleMessage, Locale, LocaleMessages } from 'vue-i18n' | ||
|
||
const debug = createDebug('@nuxtjs/i18n:messages') | ||
|
||
export type AdditionalMessages = Record<Locale, DefineLocaleMessage[]> | ||
|
||
export async function extendMessages(nuxt: Nuxt, localeCodes: string[]): Promise<AdditionalMessages> { | ||
const additionalMessages: LocaleMessages<DefineLocaleMessage>[] = [] | ||
await nuxt.callHook('i18n:extend-messages', additionalMessages, localeCodes) | ||
debug('i18n:extend-messages additional messages', additionalMessages) | ||
|
||
return normalizeAdditionalMessages(additionalMessages, localeCodes) | ||
} | ||
|
||
async function normalizeAdditionalMessages(additional: LocaleMessages<DefineLocaleMessage>[], localeCodes: string[]) { | ||
const additionalMessages: AdditionalMessages = {} | ||
for (const localeCode of localeCodes) { | ||
additionalMessages[localeCode] = [] | ||
} | ||
|
||
for (const [, messages] of Object.entries(additional)) { | ||
for (const [locale, message] of Object.entries(messages)) { | ||
additionalMessages[locale].push(message) | ||
} | ||
} | ||
|
||
return additionalMessages | ||
} |
Oops, something went wrong.