Skip to content

Commit

Permalink
feat: Update next-intl to support latest release candidate (`getTra…
Browse files Browse the repository at this point in the history
…nslations` & `t.markup`)
  • Loading branch information
amannn committed Nov 9, 2023
1 parent 66313b9 commit b134632
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 20 deletions.
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": "3.0.0-beta.17",
"next-intl": "3.0.0-rc.10",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { getTranslations } from 'next-intl/server'

export default async function GetTranslationsTest1() {
const t = await getTranslations()
return <p>{t('IndexPage.title')}</p>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { getTranslations } from 'next-intl/server'

export default async function GetTranslationsTest2() {
const t = await getTranslations('IndexPage')
return <p>{t('title')}</p>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useTranslations } from 'next-intl'

export default function UseTranslationsTest1() {
const t = useTranslations('Test')
return <p>{t('title')}</p>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useTranslations } from 'next-intl'

export default function UseTranslationsTest2() {
const t = useTranslations()
return <p>{t('Test.title')}</p>
}
37 changes: 20 additions & 17 deletions examples/by-frameworks/next-intl/src/app/[locale]/page.tsx
Original file line number Diff line number Diff line change
@@ -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'),
Expand All @@ -15,36 +19,35 @@ export default function IndexPage() {

t('title')
t.rich('title')
t.markup('title')
t.raw('title')

return (
<div>
<h1>{t('title')}</h1>
<p>{t('description')}</p>
<Test1 />
<Test2 />
<Test3 />
<Test4 />
<UseTranslationsTest1 />
<UseTranslationsTest2 />
<GetTranslationsTest1 />
<GetTranslationsTest2 />
<InlineTest1 />
<InlineTest2 />
<InlineTest3 />
</div>
)
}

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

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

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

function Test4() {
const t = useTranslations()
return <p>{t('IndexPage.title')}</p>
async function InlineTest3() {
const t = await getTranslations('Test')
return <p>{t('title')}</p>
}
7 changes: 5 additions & 2 deletions src/frameworks/next-intl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})[\'"`]',
]
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit b134632

Please sign in to comment.