Skip to content

Commit a66de32

Browse files
authored
feat: Support getTranslator API from next-intl (#1017)
1 parent 378dfdd commit a66de32

File tree

8 files changed

+40
-9
lines changed

8 files changed

+40
-9
lines changed

examples/by-frameworks/next-intl/messages/de.json

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"description": "Eine Beschreibung",
44
"title": "next-intl Beispiel"
55
},
6+
"Metadata": {
7+
"title": "Seitentitel"
8+
},
69
"Test": {
710
"title": "Test"
811
}

examples/by-frameworks/next-intl/messages/en.json

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"description": "Some description",
44
"title": "next-intl example"
55
},
6+
"Metadata": {
7+
"title": "Page title"
8+
},
69
"Test": {
710
"title": "Test"
811
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const withNextIntl = require('next-intl/plugin')()
2+
3+
module.exports = withNextIntl()

examples/by-frameworks/next-intl/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"dependencies": {
1212
"next": "^13.4.0",
13-
"next-intl": "^2.14.1",
13+
"next-intl": "3.0.0-beta.17",
1414
"react": "^18.2.0",
1515
"react-dom": "^18.2.0"
1616
},

examples/by-frameworks/next-intl/src/app/[locale]/layout.tsx

-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ export default async function LocaleLayout({
2121

2222
return (
2323
<html lang={locale}>
24-
<head>
25-
<title>next-intl</title>
26-
</head>
2724
<body>
2825
<NextIntlClientProvider locale={locale} messages={messages}>
2926
{children}

examples/by-frameworks/next-intl/src/app/[locale]/page.tsx

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
'use client'
21

32
import { useTranslations } from 'next-intl'
3+
import { getTranslator } from 'next-intl/server'
4+
5+
export async function generateMetadata({ params: { locale } }) {
6+
const t = await getTranslator(locale, 'Metadata')
7+
8+
return {
9+
title: t('title'),
10+
}
11+
}
412

513
export default function IndexPage() {
614
const t = useTranslations('IndexPage')
@@ -15,6 +23,8 @@ export default function IndexPage() {
1523
<p>{t('description')}</p>
1624
<Test1 />
1725
<Test2 />
26+
<Test3 />
27+
<Test4 />
1828
</div>
1929
)
2030
}
@@ -28,3 +38,13 @@ function Test2() {
2838
const t = useTranslations()
2939
return <p>{t('Test.title')}</p>
3040
}
41+
42+
function Test3() {
43+
const t = useTranslations('Test')
44+
return <p>{t('title')}</p>
45+
}
46+
47+
function Test4() {
48+
const t = useTranslations()
49+
return <p>{t('IndexPage.title')}</p>
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { getRequestConfig } from 'next-intl/server'
2+
3+
export default getRequestConfig(async({ locale }) => ({
4+
messages: (await import(`../messages/${locale}.json`)).default,
5+
}))

src/frameworks/next-intl.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,16 @@ class NextIntlFramework extends Framework {
7979
const ranges: ScopeRange[] = []
8080
const text = document.getText()
8181

82-
// Find matches of `useTranslations`, later occurences will override
83-
// previous ones (this allows for multiple components with different
82+
// Find matches of `useTranslations` and `getTranslator`. Later occurences will
83+
// override previous ones (this allows for multiple components with different
8484
// namespaces in the same file).
85-
const regex = /useTranslations\(\s*(['"`](.*?)['"`])?/g
85+
const regex = /(useTranslations\(\s*|getTranslator\(.*,\s*)(['"`](.*?)['"`])?/g
8686
let prevGlobalScope = false
8787
for (const match of text.matchAll(regex)) {
8888
if (typeof match.index !== 'number')
8989
continue
9090

91-
const namespace = match[2]
91+
const namespace = match[3]
9292

9393
// End previous scope
9494
if (prevGlobalScope)

0 commit comments

Comments
 (0)