Skip to content

Commit

Permalink
🌭 🍑 πŸ₯­ i18nη›΄γ—γŸ (next-i18n-router δΎε­˜γ‚„γ‚γŸ)
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-20 committed Dec 2, 2024
1 parent 236a8a2 commit bf76a1e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 32 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 0 additions & 24 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
9 changes: 7 additions & 2 deletions src/shared/i18n/client-layout.tsx
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 <I18nextProvider i18n={i18n}>{children}</I18nextProvider>;
};
12 changes: 9 additions & 3 deletions src/shared/i18n/middleware.ts
Original file line number Diff line number Diff line change
@@ -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());
};
1 change: 1 addition & 0 deletions src/shared/i18n/server-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <RootLayout locale={locale}>{children}</RootLayout>;
};

Expand Down

0 comments on commit bf76a1e

Please sign in to comment.