Skip to content

Commit

Permalink
Bug: loadLocaleFrom() fails Node
Browse files Browse the repository at this point in the history
Details:
---
*
  • Loading branch information
crs1138 committed Aug 16, 2023
1 parent 01a4084 commit f8f7fb4
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 2 deletions.
6 changes: 6 additions & 0 deletions examples/with-app-directory/honza/cs/common.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"title": "next-translate knihovna",
"second-page": "Druhá <0>stránka</0>",
"loading": "Nahrávám...",
"layout-title": "Titulek rozložení"
}
3 changes: 3 additions & 0 deletions examples/with-app-directory/honza/cs/home.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"client-only": "Pouze client-side"
}
11 changes: 10 additions & 1 deletion examples/with-app-directory/i18n.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
module.exports = {
locales: ['en', 'ca', 'es'],
locales: ['en', 'ca', 'es', 'cs'],
defaultLocale: 'en',
pages: {
'*': ['common'],
'/[lang]': ['home'],
'/[lang]/second-page': ['home'],
'/[lang]/[slug]': ['home'],
},
loadLocaleFrom: (locale, namespace) => {
const i18nLocale = `./honza/${locale}/${namespace}.json`
console.log('i18n.js > loadLocaleFrom() :::', { i18nLocale })

return import(i18nLocale)
.then((m) => m.default)
.catch((e) => console.error(e))
},
}
16 changes: 16 additions & 0 deletions examples/with-app-directory/src/app/[lang]/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Link from 'next/link'
import useTranslation from 'next-translate/useTranslation'
import Trans from 'next-translate/Trans'

export default function Page() {
const { t, lang } = useTranslation('common')
return (
<>
<h2>{t`title`}</h2>
<Trans i18nKey="common:second-page" components={[<b />]} />
<div>
<Link href={`/${lang}`}>⬅️</Link>
</div>
</>
)
}
9 changes: 8 additions & 1 deletion examples/with-app-directory/src/app/[lang]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@ import useTranslation from 'next-translate/useTranslation'
import i18n from '../../../i18n'
import { redirect } from 'next/navigation'

export async function generateStaticParams() {
return [{ lang: 'en' }, { lang: 'es' }, { lang: 'ca' }, { lang: 'cs' }]
}

export default function RootLayout({
children,
params,
}: {
children: React.ReactNode
params?: any
}) {
const { t, lang } = useTranslation('common')
console.log({ lang })

// Redirect to default locale if lang is not supported. /second-page -> /en/second-page
if (!i18n.locales.includes(lang)) redirect(`/${i18n.defaultLocale}/${lang}`)

return (
<html lang={lang}>
<html lang={params.lang}>
<head />
<body>
<h1>{t`layout-title`}</h1>
Expand Down
5 changes: 5 additions & 0 deletions examples/with-app-directory/src/app/[lang]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default async function Page() {
await sleep(500) // simulate slow page load to show loading page

const { t, lang } = useTranslation('common')
console.log({ lang })

return (
<>
Expand All @@ -26,6 +27,10 @@ export default async function Page() {
<Link href="/ca">Català</Link>
</div>

<div>
<Link href="/cs">Česky</Link>
</div>

<div>
<Link href={`/${lang}/second-page`}>➡️</Link>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default function Page() {
<Trans i18nKey="common:second-page" components={[<b />]} />
<div>
<Link href={`/${lang}`}>⬅️</Link>
Hi
</div>
</>
)
Expand Down

0 comments on commit f8f7fb4

Please sign in to comment.