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

build: update i18n version and routing #568

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions app/[locale]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Analytics } from "@vercel/analytics/react";

// Next Intl
import { NextIntlClientProvider } from "next-intl";
import { getMessages } from "next-intl/server";

// ShadCn
import { Toaster } from "@/components/ui/toaster";
Expand Down Expand Up @@ -69,12 +70,8 @@ export default async function LocaleLayout({
children: React.ReactNode;
params: { locale: string };
}) {
let messages;
try {
messages = (await import(`@/i18n/locales/${locale}.json`)).default;
} catch (error) {
notFound();
}

const messages = await getMessages()
Comment on lines +73 to +74
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for message loading

The error handling for message loading has been removed. Consider adding error handling to gracefully handle cases where messages fail to load.

-    const messages = await getMessages()
+    try {
+        const messages = await getMessages()
+    } catch (error) {
+        console.error('Failed to load messages:', error);
+        notFound();
+    }

Committable suggestion skipped: line range outside the PR's diff.


return (
<html lang={locale}>
Expand All @@ -88,7 +85,7 @@ export default async function LocaleLayout({
<body
className={`${outfit.className} ${dancingScript.variable} ${parisienne.variable} ${greatVibes.variable} ${alexBrush.variable} antialiased bg-slate-100 dark:bg-slate-800`}
>
<NextIntlClientProvider locale={locale} messages={messages}>
<NextIntlClientProvider messages={messages}>
<Providers>
<BaseNavbar />

Expand Down
2 changes: 1 addition & 1 deletion app/components/reusables/LanguageSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { useParams } from "next/navigation";

// Next Intl
import { useRouter } from "next-intl/client"; // This useRouter is wrapped with next/navigation useRouter
import { useRouter } from "@/i18n/routing"; // This useRouter is wrapped with next/navigation useRouter

// ShadCn
import {
Expand Down
17 changes: 17 additions & 0 deletions i18n/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {getRequestConfig} from 'next-intl/server';
import {routing} from './routing';

export default getRequestConfig(async ({requestLocale}) => {
// This typically corresponds to the `[locale]` segment
let locale = await requestLocale;

// Ensure that a valid locale is used
if (!locale || !routing.locales.includes(locale as any)) {
locale = routing.defaultLocale;
}

return {
locale,
messages: (await import(`./locales/${locale}.json`)).default
};
});
17 changes: 17 additions & 0 deletions i18n/routing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {defineRouting} from 'next-intl/routing';
import {createNavigation} from 'next-intl/navigation';

import { LOCALES, DEFAULT_LOCALE } from "@/lib/variables";

export const routing = defineRouting({
// A list of all locales that are supported
locales: LOCALES.map((locale) => locale.code),

// Used when no locale matches
defaultLocale: DEFAULT_LOCALE
});

// Lightweight wrappers around Next.js' navigation APIs
// that will consider the routing configuration
export const {Link, redirect, usePathname, useRouter, getPathname} =
createNavigation(routing);
12 changes: 2 additions & 10 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import createMiddleware from "next-intl/middleware";
import { routing } from "@/i18n/routing";

// Variables
import { LOCALES, DEFAULT_LOCALE } from "@/lib/variables";

export default createMiddleware({
// A list of all locales that are supported
locales: LOCALES.map((locale) => locale.code),

// If this locale is matched, pathnames work without a prefix (e.g. `/about` instead of `/en/about`)
defaultLocale: DEFAULT_LOCALE,
});
export default createMiddleware(routing);

export const config = {
// Skip all paths that should not be internationalized. This example skips
Expand Down
6 changes: 5 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const createNextIntlPlugin = require("next-intl/plugin");

const withNextIntl = createNextIntlPlugin()

/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
Expand All @@ -17,4 +21,4 @@ const withBundleAnalyzer = require("@next/bundle-analyzer")({
enabled: process.env.ANALYZE === "true",
});

module.exports = withBundleAnalyzer(nextConfig);
module.exports = withNextIntl(withBundleAnalyzer(nextConfig));
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"scripts": {
"dev": "next dev",
"build": "next build",
"postinstall": "node node_modules/puppeteer/install.js",
"start": "next start",
"lint": "next lint",
"analyze": "cross-env ANALYZE=true next build"
Expand Down Expand Up @@ -39,7 +38,7 @@
"clsx": "^2.0.0",
"lucide-react": "^0.279.0",
"next": "^13.5.6",
"next-intl": "^2.21.0",
"next-intl": "^3.26.3",
"next-themes": "^0.2.1",
"nodemailer": "^6.9.7",
"number-to-words": "^1.2.4",
Expand All @@ -63,8 +62,8 @@
"zod": "^3.22.2"
},
"devDependencies": {
"@sparticuz/chromium": "122.0.0",
"@next/bundle-analyzer": "^14.0.4",
"@sparticuz/chromium": "122.0.0",
"@types/nodemailer": "^6.4.14",
"@types/number-to-words": "^1.2.1",
"@types/react": "^18.2.22",
Expand All @@ -73,6 +72,6 @@
"@types/xml2js": "^0.4.13",
"cross-env": "^7.0.3",
"ignore-loader": "^0.1.2",
"puppeteer": "^21.3.1"
"puppeteer": "^22.5.0"
}
}
Loading