Skip to content

Commit b5b16b2

Browse files
amannnrobin-ln
andauthored
feat: Update next-intl to support latest release (#1048)
* feat: Update `next-intl` to support latest release candidate (`getTranslations` & `t.markup`) * Update examples/by-frameworks/next-intl/package.json Co-authored-by: Robin Louarn <[email protected]> * Support object form of `getTranslations` (h/t @ixartz) --------- Co-authored-by: Robin Louarn <[email protected]>
1 parent 66313b9 commit b5b16b2

File tree

8 files changed

+63
-21
lines changed

8 files changed

+63
-21
lines changed

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": "3.0.0-beta.17",
13+
"next-intl": "3.1.2",
1414
"react": "^18.2.0",
1515
"react-dom": "^18.2.0"
1616
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { getTranslations } from 'next-intl/server'
2+
3+
export default async function GetTranslationsTest1() {
4+
const t = await getTranslations()
5+
return <p>{t('IndexPage.title')}</p>
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { getTranslations } from 'next-intl/server'
2+
3+
export default async function GetTranslationsTest2() {
4+
const t = await getTranslations('IndexPage')
5+
return <p>{t('title')}</p>
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { getTranslations } from 'next-intl/server'
2+
3+
export default async function GetTranslationsTest3() {
4+
const t = await getTranslations({
5+
locale: 'en',
6+
namespace: 'IndexPage',
7+
})
8+
return <p>{t('title')}</p>
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { useTranslations } from 'next-intl'
2+
3+
export default function UseTranslationsTest1() {
4+
const t = useTranslations('Test')
5+
return <p>{t('title')}</p>
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { useTranslations } from 'next-intl'
2+
3+
export default function UseTranslationsTest2() {
4+
const t = useTranslations()
5+
return <p>{t('Test.title')}</p>
6+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11

22
import { useTranslations } from 'next-intl'
3-
import { getTranslator } from 'next-intl/server'
3+
import { getTranslations } from 'next-intl/server'
4+
import UseTranslationsTest1 from './UseTranslationsTest1'
5+
import UseTranslationsTest2 from './UseTranslationsTest2'
6+
import GetTranslationsTest1 from './GetTranslationsTest1'
7+
import GetTranslationsTest2 from './GetTranslationsTest2'
8+
import GetTranslationsTest3 from './GetTranslationsTest3'
49

510
export async function generateMetadata({ params: { locale } }) {
6-
const t = await getTranslator(locale, 'Metadata')
11+
const t = await getTranslations({ locale, namespace: 'Metadata' })
712

813
return {
914
title: t('title'),
@@ -15,36 +20,36 @@ export default function IndexPage() {
1520

1621
t('title')
1722
t.rich('title')
23+
t.markup('title')
1824
t.raw('title')
1925

2026
return (
2127
<div>
2228
<h1>{t('title')}</h1>
2329
<p>{t('description')}</p>
24-
<Test1 />
25-
<Test2 />
26-
<Test3 />
27-
<Test4 />
30+
<UseTranslationsTest1 />
31+
<UseTranslationsTest2 />
32+
<GetTranslationsTest1 />
33+
<GetTranslationsTest2 />
34+
<GetTranslationsTest3 />
35+
<InlineTest1 />
36+
<InlineTest2 />
37+
<InlineTest3 />
2838
</div>
2939
)
3040
}
3141

32-
function Test1() {
42+
function InlineTest1() {
3343
const t = useTranslations('Test')
3444
return <p>{t('title')}</p>
3545
}
3646

37-
function Test2() {
38-
const t = useTranslations()
39-
return <p>{t('Test.title')}</p>
40-
}
41-
42-
function Test3() {
43-
const t = useTranslations('Test')
47+
function InlineTest2() {
48+
const t = useTranslations('IndexPage')
4449
return <p>{t('title')}</p>
4550
}
4651

47-
function Test4() {
48-
const t = useTranslations()
49-
return <p>{t('IndexPage.title')}</p>
52+
async function InlineTest3() {
53+
const t = await getTranslations('Test')
54+
return <p>{t('title')}</p>
5055
}

src/frameworks/next-intl.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class NextIntlFramework extends Framework {
3333
// Rich text
3434
'[^\\w\\d]t\\s*\.rich\\s*\\(\\s*[\'"`]({key})[\'"`]',
3535

36+
// Markup text
37+
'[^\\w\\d]t\\s*\.markup\\s*\\(\\s*[\'"`]({key})[\'"`]',
38+
3639
// Raw text
3740
'[^\\w\\d]t\\s*\.raw\\s*\\(\\s*[\'"`]({key})[\'"`]',
3841
]
@@ -79,10 +82,11 @@ class NextIntlFramework extends Framework {
7982
const ranges: ScopeRange[] = []
8083
const text = document.getText()
8184

82-
// Find matches of `useTranslations` and `getTranslator`. Later occurences will
85+
// Find matches of `useTranslations` and `getTranslations`. Later occurences will
8386
// override previous ones (this allows for multiple components with different
84-
// namespaces in the same file).
85-
const regex = /(useTranslations\(\s*|getTranslator\(.*,\s*)(['"`](.*?)['"`])?/g
87+
// namespaces in the same file). Note that `getTranslations` can either be called
88+
// with a single string argument or an object with a `namespace` key.
89+
const regex = /(useTranslations\(\s*|getTranslations\(\s*|namespace:\s+)(['"`](.*?)['"`])?/g
8690
let prevGlobalScope = false
8791
for (const match of text.matchAll(regex)) {
8892
if (typeof match.index !== 'number')

0 commit comments

Comments
 (0)