From bf76a1e1cce5cf4baae2e4517656d1cc1de677a7 Mon Sep 17 00:00:00 2001 From: cp-20 Date: Mon, 2 Dec 2024 23:27:16 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=AD=20=F0=9F=8D=A1=20=F0=9F=A5=AD=20i1?= =?UTF-8?q?8n=E7=9B=B4=E3=81=97=E3=81=9F=20(next-i18n-router=20=E4=BE=9D?= =?UTF-8?q?=E5=AD=98=E3=82=84=E3=82=81=E3=81=9F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 - pnpm-lock.yaml | 24 ------------------------ src/middleware.ts | 5 +++-- src/shared/i18n/client-layout.tsx | 9 +++++++-- src/shared/i18n/middleware.ts | 12 +++++++++--- src/shared/i18n/server-layout.tsx | 1 + 6 files changed, 20 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index 5da77f02..d09fab4b 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,6 @@ "lucide-react": "^0.462.0", "nanoid": "^5.0.9", "next": "15.0.3", - "next-i18n-router": "^5.5.1", "next-pwa": "^5.6.0", "nextjs13-progress": "^1.3.3", "nprogress": "^0.2.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 616878a5..6c503b55 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -90,9 +90,6 @@ importers: next: specifier: 15.0.3 version: 15.0.3(@babel/core@7.26.0)(react-dom@19.0.0-rc-66855b96-20241106(react@19.0.0-rc-66855b96-20241106))(react@19.0.0-rc-66855b96-20241106) - next-i18n-router: - specifier: ^5.5.1 - version: 5.5.1 next-pwa: specifier: ^5.6.0 version: 5.6.0(@babel/core@7.26.0)(next@15.0.3(@babel/core@7.26.0)(react-dom@19.0.0-rc-66855b96-20241106(react@19.0.0-rc-66855b96-20241106))(react@19.0.0-rc-66855b96-20241106))(webpack@5.96.1) @@ -1371,9 +1368,6 @@ packages: '@floating-ui/utils@0.2.8': resolution: {integrity: sha512-kym7SodPp8/wloecOpcmSnWJsK7M0E5Wg8UcFA+uO4B9s5d0ywXOEro/8HM9x0rW+TljRzul/14UYz3TleT3ig==} - '@formatjs/intl-localematcher@0.5.8': - resolution: {integrity: sha512-I+WDNWWJFZie+jkfkiK5Mp4hEDyRSEvmyfYadflOno/mmKJKcB17fEpEH0oJu/OWhhCJ8kJBDz2YMd/6cDl7Mg==} - '@grpc/grpc-js@1.9.15': resolution: {integrity: sha512-nqE7Hc0AzI+euzUwDAy0aY5hCp10r734gMGRdU+qOPX0XSceI2ULrcXB5U2xSc5VkWwalCj4M7GzCAygZl2KoQ==} engines: {node: ^8.13.0 || >=10.10.0} @@ -3708,16 +3702,9 @@ packages: engines: {node: ^18 || >=20} hasBin: true - negotiator@0.6.4: - resolution: {integrity: sha512-myRT3DiWPHqho5PrJaIRyaMv2kgYf0mUVgBNOYMuCH5Ki1yEiQaf/ZJuQ62nvpc44wL5WDbTX7yGJi1Neevw8w==} - engines: {node: '>= 0.6'} - neo-async@2.6.2: resolution: {integrity: sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==} - next-i18n-router@5.5.1: - resolution: {integrity: sha512-uJGYUAQS33LbRT3Jx+kurR/E79iPQo1jWZUYmc+614UkPt58k2XYyGloSvHR74b21i4K/d6eksdBj6T2WojjdA==} - next-pwa@5.6.0: resolution: {integrity: sha512-XV8g8C6B7UmViXU8askMEYhWwQ4qc/XqJGnexbLV68hzKaGHZDMtHsm2TNxFcbR7+ypVuth/wwpiIlMwpRJJ5A==} peerDependencies: @@ -6434,10 +6421,6 @@ snapshots: '@floating-ui/utils@0.2.8': {} - '@formatjs/intl-localematcher@0.5.8': - dependencies: - tslib: 2.8.1 - '@grpc/grpc-js@1.9.15': dependencies: '@grpc/proto-loader': 0.7.13 @@ -8878,15 +8861,8 @@ snapshots: nanoid@5.0.9: {} - negotiator@0.6.4: {} - neo-async@2.6.2: {} - next-i18n-router@5.5.1: - dependencies: - '@formatjs/intl-localematcher': 0.5.8 - negotiator: 0.6.4 - next-pwa@5.6.0(@babel/core@7.26.0)(next@15.0.3(@babel/core@7.26.0)(react-dom@19.0.0-rc-66855b96-20241106(react@19.0.0-rc-66855b96-20241106))(react@19.0.0-rc-66855b96-20241106))(webpack@5.96.1): dependencies: babel-loader: 8.4.1(@babel/core@7.26.0)(webpack@5.96.1) diff --git a/src/middleware.ts b/src/middleware.ts index 08451300..d9bdb9b7 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -2,12 +2,13 @@ import type { NextMiddleware } from 'next/server'; import { i18nMiddleware } from '@/shared/i18n/middleware'; import { cspMiddleware } from '@/shared/lib/csp'; -const normalRoutePattern = /https?:\/\/[^/]+\/((?!api|static|.*\\..*|_next|favicon.ico|ogp.png|icon).*)/; +const normalRoutePattern = /^\/([^/]+\/)?(?:analyze-logs|ccfolia|dice|expect)?$/; export const middleware: NextMiddleware = (req, event) => { cspMiddleware(req, event); - if (normalRoutePattern.test(req.url)) { + const pathname = new URL(req.url).pathname; + if (normalRoutePattern.test(pathname)) { return i18nMiddleware(req, event); } }; diff --git a/src/shared/i18n/client-layout.tsx b/src/shared/i18n/client-layout.tsx index a66fef5f..47240963 100644 --- a/src/shared/i18n/client-layout.tsx +++ b/src/shared/i18n/client-layout.tsx @@ -1,11 +1,11 @@ 'use client'; import i18n from 'i18next'; -import { useCurrentLocale } from 'next-i18n-router/client'; import type { ReactNode } from 'react'; import { I18nextProvider } from 'react-i18next'; import { i18nextInitOptions } from '@/locales/i18next'; import { i18nConfig } from '@/shared/i18n/config'; +import { usePathname } from 'next/navigation'; i18n.init(i18nextInitOptions, (err) => { if (err) { @@ -14,6 +14,11 @@ i18n.init(i18nextInitOptions, (err) => { }); export const I18nProvider = ({ children }: { children: ReactNode }) => { - i18n.changeLanguage(useCurrentLocale(i18nConfig)); + const pathname = usePathname(); + const localeCandidate = pathname.split('/')[1]; + const locale = i18nConfig.locales.includes(localeCandidate) ? localeCandidate : i18nConfig.defaultLocale; + console.log('client locale', locale); + + i18n.changeLanguage(locale); return {children}; }; diff --git a/src/shared/i18n/middleware.ts b/src/shared/i18n/middleware.ts index 7753b046..f4278106 100644 --- a/src/shared/i18n/middleware.ts +++ b/src/shared/i18n/middleware.ts @@ -1,7 +1,13 @@ -import type { NextMiddleware } from 'next/server'; -import { i18nRouter } from 'next-i18n-router'; +import { NextResponse, type NextMiddleware } from 'next/server'; import { i18nConfig } from './config'; export const i18nMiddleware: NextMiddleware = (req) => { - return i18nRouter(req, i18nConfig); + const url = new URL(req.url); + console.log('i18n', url.pathname); + + if (i18nConfig.locales.some((l) => url.pathname.startsWith(`/${l}`))) return; + url.pathname = `/${i18nConfig.defaultLocale}${url.pathname}`; + console.log('redirecting to', url.toString()); + + return NextResponse.rewrite(url.toString()); }; diff --git a/src/shared/i18n/server-layout.tsx b/src/shared/i18n/server-layout.tsx index 24c6beca..18098f6e 100644 --- a/src/shared/i18n/server-layout.tsx +++ b/src/shared/i18n/server-layout.tsx @@ -15,6 +15,7 @@ export const wrapRootLayout = (RootLayout: FC<{ children: ReactNode; locale: str }> = async ({ children, params }) => { const { locale } = await params; i18n.changeLanguage(locale); + console.log('server locale', locale); return {children}; };