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

fix: fix useContext error + update example #1182

Merged
merged 1 commit into from
Jan 22, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
"build": "next build"
},
"dependencies": {
"next": "13.4.7",
"next": "14.1.0",
"next-translate": "link:../../",
"react": "link:../../node_modules/react",
"react-dom": "link:../../node_modules/react-dom"
},
"devDependencies": {
"next-translate-plugin": "2.4.4"
"next-translate-plugin": "2.6.2"
}
}
18 changes: 9 additions & 9 deletions examples/complex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
"analyze": "ANALYZE=true yarn build"
},
"dependencies": {
"@mdx-js/loader": "2.3.0",
"@mdx-js/react": "2.3.0",
"@next/mdx": "13.4.7",
"next": "13.4.7",
"@mdx-js/loader": "3.0.0",
"@mdx-js/react": "3.0.0",
"@next/mdx": "14.1.0",
"next": "14.1.0",
"next-translate": "link:../../",
"react": "link:../../node_modules/react",
"react-dom": "link:../../node_modules/react-dom"
},
"devDependencies": {
"@next/bundle-analyzer": "13.4.7",
"@types/node": "20.3.1",
"@types/react": "18.2.13",
"next-translate-plugin": "2.4.4",
"typescript": "5.1.3"
"@next/bundle-analyzer": "14.1.0",
"@types/node": "20.11.5",
"@types/react": "18.2.48",
"next-translate-plugin": "2.6.2",
"typescript": "5.3.3"
},
"resolutions": {
"webpack": "5.11.1"
Expand Down
18 changes: 9 additions & 9 deletions examples/with-app-directory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
"analyze": "ANALYZE=true yarn build"
},
"dependencies": {
"@mdx-js/loader": "2.3.0",
"@mdx-js/react": "2.3.0",
"@next/mdx": "13.4.7",
"next": "13.4.7",
"@mdx-js/loader": "3.0.0",
"@mdx-js/react": "3.0.0",
"@next/mdx": "14.1.0",
"next": "14.1.0",
"next-translate": "link:../../",
"react": "link:../../node_modules/react",
"react-dom": "link:../../node_modules/react-dom"
},
"devDependencies": {
"@next/bundle-analyzer": "13.4.6",
"@types/node": "20.3.1",
"@types/react": "18.2.12",
"next-translate-plugin": "2.4.4",
"typescript": "5.1.3"
"@next/bundle-analyzer": "14.1.0",
"@types/node": "20.11.5",
"@types/react": "18.2.48",
"next-translate-plugin": "2.6.2",
"typescript": "5.3.3"
},
"resolutions": {
"webpack": "5.11.1"
Expand Down
21 changes: 21 additions & 0 deletions examples/with-app-directory/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import useTranslation from 'next-translate/useTranslation'
import i18n from '../../i18n'
import { redirect } from 'next/navigation'

export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
const { lang } = useTranslation('common')

// 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}>
<head />
<body>{children}</body>
</html>
)
}
4 changes: 2 additions & 2 deletions examples/without-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
"build": "next build"
},
"dependencies": {
"next": "13.4.7",
"next": "14.1.0",
"next-translate": "link:../../",
"react": "link:../../node_modules/react",
"react-dom": "link:../../node_modules/react-dom"
},
"devDependencies": {
"next-translate-plugin": "2.4.0"
"next-translate-plugin": "2.6.2"
}
}
6 changes: 3 additions & 3 deletions src/context.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { createContext } from 'react'
import { I18n } from '.'
import React from 'react'

let context

// For serverComponents (app-dir), the context cannot be used and
// this makes that all the imports to here don't break the build.
// The use of this context is inside each util, depending pages-dir or app-dir.
if (typeof createContext === 'function') {
context = createContext<I18n>({
if (typeof React.createContext === 'function') {
context = React.createContext<I18n>({
t: (k) => (Array.isArray(k) ? k[0] : k),
lang: '',
})
Expand Down