Skip to content

Commit 184a518

Browse files
committed
Support object form of getTranslations (h/t @ixartz)
1 parent cc73ffd commit 184a518

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed
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+
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import UseTranslationsTest1 from './UseTranslationsTest1'
55
import UseTranslationsTest2 from './UseTranslationsTest2'
66
import GetTranslationsTest1 from './GetTranslationsTest1'
77
import GetTranslationsTest2 from './GetTranslationsTest2'
8+
import GetTranslationsTest3 from './GetTranslationsTest3'
89

910
export async function generateMetadata({ params: { locale } }) {
1011
const t = await getTranslations({ locale, namespace: 'Metadata' })
@@ -30,6 +31,7 @@ export default function IndexPage() {
3031
<UseTranslationsTest2 />
3132
<GetTranslationsTest1 />
3233
<GetTranslationsTest2 />
34+
<GetTranslationsTest3 />
3335
<InlineTest1 />
3436
<InlineTest2 />
3537
<InlineTest3 />

src/frameworks/next-intl.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ class NextIntlFramework extends Framework {
8484

8585
// Find matches of `useTranslations` and `getTranslations`. Later occurences will
8686
// override previous ones (this allows for multiple components with different
87-
// namespaces in the same file).
88-
const regex = /(useTranslations\(\s*|getTranslations\(\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
8990
let prevGlobalScope = false
9091
for (const match of text.matchAll(regex)) {
9192
if (typeof match.index !== 'number')

0 commit comments

Comments
 (0)