diff --git a/examples/by-frameworks/next-intl/messages/de.json b/examples/by-frameworks/next-intl/messages/de.json index 1e3138ca..5f65bc72 100644 --- a/examples/by-frameworks/next-intl/messages/de.json +++ b/examples/by-frameworks/next-intl/messages/de.json @@ -3,6 +3,9 @@ "description": "Eine Beschreibung", "title": "next-intl Beispiel" }, + "Metadata": { + "title": "Seitentitel" + }, "Test": { "title": "Test" } diff --git a/examples/by-frameworks/next-intl/messages/en.json b/examples/by-frameworks/next-intl/messages/en.json index 648f7ccb..7e08e4cc 100644 --- a/examples/by-frameworks/next-intl/messages/en.json +++ b/examples/by-frameworks/next-intl/messages/en.json @@ -3,6 +3,9 @@ "description": "Some description", "title": "next-intl example" }, + "Metadata": { + "title": "Page title" + }, "Test": { "title": "Test" } diff --git a/examples/by-frameworks/next-intl/next.config.js b/examples/by-frameworks/next-intl/next.config.js new file mode 100644 index 00000000..9b47e0df --- /dev/null +++ b/examples/by-frameworks/next-intl/next.config.js @@ -0,0 +1,3 @@ +const withNextIntl = require('next-intl/plugin')() + +module.exports = withNextIntl() diff --git a/examples/by-frameworks/next-intl/package.json b/examples/by-frameworks/next-intl/package.json index 5109dff1..bc1060f6 100644 --- a/examples/by-frameworks/next-intl/package.json +++ b/examples/by-frameworks/next-intl/package.json @@ -10,7 +10,7 @@ }, "dependencies": { "next": "^13.4.0", - "next-intl": "^2.14.1", + "next-intl": "3.0.0-beta.17", "react": "^18.2.0", "react-dom": "^18.2.0" }, diff --git a/examples/by-frameworks/next-intl/src/app/[locale]/layout.tsx b/examples/by-frameworks/next-intl/src/app/[locale]/layout.tsx index b7cc6464..4b4ad32e 100644 --- a/examples/by-frameworks/next-intl/src/app/[locale]/layout.tsx +++ b/examples/by-frameworks/next-intl/src/app/[locale]/layout.tsx @@ -21,9 +21,6 @@ export default async function LocaleLayout({ return ( - - next-intl - {children} diff --git a/examples/by-frameworks/next-intl/src/app/[locale]/page.tsx b/examples/by-frameworks/next-intl/src/app/[locale]/page.tsx index 2b76a084..832d0f4e 100644 --- a/examples/by-frameworks/next-intl/src/app/[locale]/page.tsx +++ b/examples/by-frameworks/next-intl/src/app/[locale]/page.tsx @@ -1,6 +1,14 @@ -'use client' import { useTranslations } from 'next-intl' +import { getTranslator } from 'next-intl/server' + +export async function generateMetadata({ params: { locale } }) { + const t = await getTranslator(locale, 'Metadata') + + return { + title: t('title'), + } +} export default function IndexPage() { const t = useTranslations('IndexPage') @@ -15,6 +23,8 @@ export default function IndexPage() {

{t('description')}

+ + ) } @@ -28,3 +38,13 @@ function Test2() { const t = useTranslations() return

{t('Test.title')}

} + +function Test3() { + const t = useTranslations('Test') + return

{t('title')}

+} + +function Test4() { + const t = useTranslations() + return

{t('IndexPage.title')}

+} diff --git a/examples/by-frameworks/next-intl/src/i18n.ts b/examples/by-frameworks/next-intl/src/i18n.ts new file mode 100644 index 00000000..a55b5af2 --- /dev/null +++ b/examples/by-frameworks/next-intl/src/i18n.ts @@ -0,0 +1,5 @@ +import { getRequestConfig } from 'next-intl/server' + +export default getRequestConfig(async({ locale }) => ({ + messages: (await import(`../messages/${locale}.json`)).default, +})) diff --git a/src/frameworks/next-intl.ts b/src/frameworks/next-intl.ts index 879bb39e..28e70752 100644 --- a/src/frameworks/next-intl.ts +++ b/src/frameworks/next-intl.ts @@ -79,16 +79,16 @@ class NextIntlFramework extends Framework { const ranges: ScopeRange[] = [] const text = document.getText() - // Find matches of `useTranslations`, later occurences will override - // previous ones (this allows for multiple components with different + // Find matches of `useTranslations` and `getTranslator`. Later occurences will + // override previous ones (this allows for multiple components with different // namespaces in the same file). - const regex = /useTranslations\(\s*(['"`](.*?)['"`])?/g + const regex = /(useTranslations\(\s*|getTranslator\(.*,\s*)(['"`](.*?)['"`])?/g let prevGlobalScope = false for (const match of text.matchAll(regex)) { if (typeof match.index !== 'number') continue - const namespace = match[2] + const namespace = match[3] // End previous scope if (prevGlobalScope)