From b1346329f1824bf8e572ccd7510e4d87b042ab7f Mon Sep 17 00:00:00 2001 From: Jan Amann Date: Thu, 9 Nov 2023 20:49:10 +0100 Subject: [PATCH 1/3] feat: Update `next-intl` to support latest release candidate (`getTranslations` & `t.markup`) --- examples/by-frameworks/next-intl/package.json | 2 +- .../src/app/[locale]/GetTranslationsTest1.tsx | 6 +++ .../src/app/[locale]/GetTranslationsTest2.tsx | 6 +++ .../src/app/[locale]/UseTranslationsTest1.tsx | 6 +++ .../src/app/[locale]/UseTranslationsTest2.tsx | 6 +++ .../next-intl/src/app/[locale]/page.tsx | 37 ++++++++++--------- src/frameworks/next-intl.ts | 7 +++- 7 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 examples/by-frameworks/next-intl/src/app/[locale]/GetTranslationsTest1.tsx create mode 100644 examples/by-frameworks/next-intl/src/app/[locale]/GetTranslationsTest2.tsx create mode 100644 examples/by-frameworks/next-intl/src/app/[locale]/UseTranslationsTest1.tsx create mode 100644 examples/by-frameworks/next-intl/src/app/[locale]/UseTranslationsTest2.tsx diff --git a/examples/by-frameworks/next-intl/package.json b/examples/by-frameworks/next-intl/package.json index bc1060f6..d2687d95 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": "3.0.0-beta.17", + "next-intl": "3.0.0-rc.10", "react": "^18.2.0", "react-dom": "^18.2.0" }, diff --git a/examples/by-frameworks/next-intl/src/app/[locale]/GetTranslationsTest1.tsx b/examples/by-frameworks/next-intl/src/app/[locale]/GetTranslationsTest1.tsx new file mode 100644 index 00000000..16e0e980 --- /dev/null +++ b/examples/by-frameworks/next-intl/src/app/[locale]/GetTranslationsTest1.tsx @@ -0,0 +1,6 @@ +import { getTranslations } from 'next-intl/server' + +export default async function GetTranslationsTest1() { + const t = await getTranslations() + return

{t('IndexPage.title')}

+} diff --git a/examples/by-frameworks/next-intl/src/app/[locale]/GetTranslationsTest2.tsx b/examples/by-frameworks/next-intl/src/app/[locale]/GetTranslationsTest2.tsx new file mode 100644 index 00000000..3423d0fa --- /dev/null +++ b/examples/by-frameworks/next-intl/src/app/[locale]/GetTranslationsTest2.tsx @@ -0,0 +1,6 @@ +import { getTranslations } from 'next-intl/server' + +export default async function GetTranslationsTest2() { + const t = await getTranslations('IndexPage') + return

{t('title')}

+} diff --git a/examples/by-frameworks/next-intl/src/app/[locale]/UseTranslationsTest1.tsx b/examples/by-frameworks/next-intl/src/app/[locale]/UseTranslationsTest1.tsx new file mode 100644 index 00000000..9f6d1c45 --- /dev/null +++ b/examples/by-frameworks/next-intl/src/app/[locale]/UseTranslationsTest1.tsx @@ -0,0 +1,6 @@ +import { useTranslations } from 'next-intl' + +export default function UseTranslationsTest1() { + const t = useTranslations('Test') + return

{t('title')}

+} diff --git a/examples/by-frameworks/next-intl/src/app/[locale]/UseTranslationsTest2.tsx b/examples/by-frameworks/next-intl/src/app/[locale]/UseTranslationsTest2.tsx new file mode 100644 index 00000000..85d61a71 --- /dev/null +++ b/examples/by-frameworks/next-intl/src/app/[locale]/UseTranslationsTest2.tsx @@ -0,0 +1,6 @@ +import { useTranslations } from 'next-intl' + +export default function UseTranslationsTest2() { + const t = useTranslations() + return

{t('Test.title')}

+} 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 832d0f4e..21434e80 100644 --- a/examples/by-frameworks/next-intl/src/app/[locale]/page.tsx +++ b/examples/by-frameworks/next-intl/src/app/[locale]/page.tsx @@ -1,9 +1,13 @@ import { useTranslations } from 'next-intl' -import { getTranslator } from 'next-intl/server' +import { getTranslations } from 'next-intl/server' +import UseTranslationsTest1 from './UseTranslationsTest1' +import UseTranslationsTest2 from './UseTranslationsTest2' +import GetTranslationsTest1 from './GetTranslationsTest1' +import GetTranslationsTest2 from './GetTranslationsTest2' export async function generateMetadata({ params: { locale } }) { - const t = await getTranslator(locale, 'Metadata') + const t = await getTranslations({ locale, namespace: 'Metadata' }) return { title: t('title'), @@ -15,36 +19,35 @@ export default function IndexPage() { t('title') t.rich('title') + t.markup('title') t.raw('title') return (

{t('title')}

{t('description')}

- - - - + + + + + + +
) } -function Test1() { +function InlineTest1() { const t = useTranslations('Test') return

{t('title')}

} -function Test2() { - const t = useTranslations() - return

{t('Test.title')}

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

{t('title')}

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

{t('IndexPage.title')}

+async function InlineTest3() { + const t = await getTranslations('Test') + return

{t('title')}

} diff --git a/src/frameworks/next-intl.ts b/src/frameworks/next-intl.ts index 5a628545..bc9f8637 100644 --- a/src/frameworks/next-intl.ts +++ b/src/frameworks/next-intl.ts @@ -33,6 +33,9 @@ class NextIntlFramework extends Framework { // Rich text '[^\\w\\d]t\\s*\.rich\\s*\\(\\s*[\'"`]({key})[\'"`]', + // Markup text + '[^\\w\\d]t\\s*\.markup\\s*\\(\\s*[\'"`]({key})[\'"`]', + // Raw text '[^\\w\\d]t\\s*\.raw\\s*\\(\\s*[\'"`]({key})[\'"`]', ] @@ -79,10 +82,10 @@ class NextIntlFramework extends Framework { const ranges: ScopeRange[] = [] const text = document.getText() - // Find matches of `useTranslations` and `getTranslator`. Later occurences will + // Find matches of `useTranslations` and `getTranslations`. Later occurences will // override previous ones (this allows for multiple components with different // namespaces in the same file). - const regex = /(useTranslations\(\s*|getTranslator\(.*,\s*)(['"`](.*?)['"`])?/g + const regex = /(useTranslations\(\s*|getTranslations\(\s*)(['"`](.*?)['"`])?/g let prevGlobalScope = false for (const match of text.matchAll(regex)) { if (typeof match.index !== 'number') From cc73ffd56fb4e147d6671ebe933e9f7ce66c178c Mon Sep 17 00:00:00 2001 From: Jan Amann Date: Wed, 22 Nov 2023 22:01:11 +0100 Subject: [PATCH 2/3] Update examples/by-frameworks/next-intl/package.json Co-authored-by: Robin Louarn <34136686+Robin-Ln@users.noreply.github.com> --- examples/by-frameworks/next-intl/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/by-frameworks/next-intl/package.json b/examples/by-frameworks/next-intl/package.json index d2687d95..f475efa7 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": "3.0.0-rc.10", + "next-intl": "3.1.2", "react": "^18.2.0", "react-dom": "^18.2.0" }, From 184a518b216eebcb05f558fcfc834d1c215f8856 Mon Sep 17 00:00:00 2001 From: Jan Amann Date: Tue, 14 May 2024 16:53:11 +0200 Subject: [PATCH 3/3] Support object form of `getTranslations` (h/t @ixartz) --- .../next-intl/src/app/[locale]/GetTranslationsTest3.tsx | 9 +++++++++ .../by-frameworks/next-intl/src/app/[locale]/page.tsx | 2 ++ src/frameworks/next-intl.ts | 5 +++-- 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 examples/by-frameworks/next-intl/src/app/[locale]/GetTranslationsTest3.tsx diff --git a/examples/by-frameworks/next-intl/src/app/[locale]/GetTranslationsTest3.tsx b/examples/by-frameworks/next-intl/src/app/[locale]/GetTranslationsTest3.tsx new file mode 100644 index 00000000..7bfc7dc5 --- /dev/null +++ b/examples/by-frameworks/next-intl/src/app/[locale]/GetTranslationsTest3.tsx @@ -0,0 +1,9 @@ +import { getTranslations } from 'next-intl/server' + +export default async function GetTranslationsTest3() { + const t = await getTranslations({ + locale: 'en', + namespace: 'IndexPage', + }) + return

{t('title')}

+} 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 21434e80..943f9233 100644 --- a/examples/by-frameworks/next-intl/src/app/[locale]/page.tsx +++ b/examples/by-frameworks/next-intl/src/app/[locale]/page.tsx @@ -5,6 +5,7 @@ import UseTranslationsTest1 from './UseTranslationsTest1' import UseTranslationsTest2 from './UseTranslationsTest2' import GetTranslationsTest1 from './GetTranslationsTest1' import GetTranslationsTest2 from './GetTranslationsTest2' +import GetTranslationsTest3 from './GetTranslationsTest3' export async function generateMetadata({ params: { locale } }) { const t = await getTranslations({ locale, namespace: 'Metadata' }) @@ -30,6 +31,7 @@ export default function IndexPage() { + diff --git a/src/frameworks/next-intl.ts b/src/frameworks/next-intl.ts index bc9f8637..edfa765e 100644 --- a/src/frameworks/next-intl.ts +++ b/src/frameworks/next-intl.ts @@ -84,8 +84,9 @@ class NextIntlFramework extends Framework { // Find matches of `useTranslations` and `getTranslations`. Later occurences will // override previous ones (this allows for multiple components with different - // namespaces in the same file). - const regex = /(useTranslations\(\s*|getTranslations\(\s*)(['"`](.*?)['"`])?/g + // namespaces in the same file). Note that `getTranslations` can either be called + // with a single string argument or an object with a `namespace` key. + const regex = /(useTranslations\(\s*|getTranslations\(\s*|namespace:\s+)(['"`](.*?)['"`])?/g let prevGlobalScope = false for (const match of text.matchAll(regex)) { if (typeof match.index !== 'number')