Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support getTranslator API from next-intl #1017

Merged
merged 1 commit into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/by-frameworks/next-intl/messages/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"description": "Eine Beschreibung",
"title": "next-intl Beispiel"
},
"Metadata": {
"title": "Seitentitel"
},
"Test": {
"title": "Test"
}
Expand Down
3 changes: 3 additions & 0 deletions examples/by-frameworks/next-intl/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"description": "Some description",
"title": "next-intl example"
},
"Metadata": {
"title": "Page title"
},
"Test": {
"title": "Test"
}
Expand Down
3 changes: 3 additions & 0 deletions examples/by-frameworks/next-intl/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const withNextIntl = require('next-intl/plugin')()

module.exports = withNextIntl()
2 changes: 1 addition & 1 deletion examples/by-frameworks/next-intl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
3 changes: 0 additions & 3 deletions examples/by-frameworks/next-intl/src/app/[locale]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ export default async function LocaleLayout({

return (
<html lang={locale}>
<head>
<title>next-intl</title>
</head>
<body>
<NextIntlClientProvider locale={locale} messages={messages}>
{children}
Expand Down
22 changes: 21 additions & 1 deletion examples/by-frameworks/next-intl/src/app/[locale]/page.tsx
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -15,6 +23,8 @@ export default function IndexPage() {
<p>{t('description')}</p>
<Test1 />
<Test2 />
<Test3 />
<Test4 />
</div>
)
}
Expand All @@ -28,3 +38,13 @@ function Test2() {
const t = useTranslations()
return <p>{t('Test.title')}</p>
}

function Test3() {
const t = useTranslations('Test')
return <p>{t('title')}</p>
}

function Test4() {
const t = useTranslations()
return <p>{t('IndexPage.title')}</p>
}
5 changes: 5 additions & 0 deletions examples/by-frameworks/next-intl/src/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { getRequestConfig } from 'next-intl/server'

export default getRequestConfig(async({ locale }) => ({
messages: (await import(`../messages/${locale}.json`)).default,
}))
8 changes: 4 additions & 4 deletions src/frameworks/next-intl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down