Skip to content

Commit

Permalink
Set up next-intl
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielMajeri committed Jan 15, 2024
1 parent fbabca8 commit 7da65ce
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 4 deletions.
4 changes: 3 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const withNextIntl = require("next-intl/plugin")();

/** @type {import('next').NextConfig} */
const nextConfig = {};

module.exports = nextConfig;
module.exports = withNextIntl(nextConfig);
155 changes: 155 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"dependencies": {
"next": "14.0.4",
"next-intl": "^3.4.2",
"react": "^18",
"react-dom": "^18"
},
Expand Down
File renamed without changes.
6 changes: 5 additions & 1 deletion src/app/layout.tsx → src/app/[locale]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ export const metadata: Metadata = {

export default function RootLayout({
children,
params: { locale },
}: {
children: React.ReactNode;
params: {
locale: string;
};
}) {
return (
<html lang="en">
<html lang={locale}>
<body className={inter.className}>{children}</body>
</html>
);
Expand Down
8 changes: 6 additions & 2 deletions src/app/page.tsx → src/app/[locale]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import Image from "next/image";

import { useTranslations } from "next-intl";

export default function Home() {
const t = useTranslations("Index");

return (
<main className="flex min-h-screen flex-col items-center justify-between p-24">
<div className="z-10 max-w-5xl w-full items-center justify-between font-mono text-sm lg:flex">
<p className="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30">
Get started by editing&nbsp;
{t("getStarted")}&nbsp;
<code className="font-mono font-bold">src/app/page.tsx</code>
</p>
<div className="fixed bottom-0 left-0 flex h-48 w-full items-end justify-center bg-gradient-to-t from-white via-white dark:from-black dark:via-black lg:static lg:h-auto lg:w-auto lg:bg-none">
Expand All @@ -15,7 +19,7 @@ export default function Home() {
target="_blank"
rel="noopener noreferrer"
>
By{" "}
{t("by")}{" "}
<Image
src="/vercel.svg"
alt="Vercel Logo"
Expand Down
15 changes: 15 additions & 0 deletions src/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { notFound } from "next/navigation";
import { getRequestConfig } from "next-intl/server";

export const locales = ["ro", "en"];

export default getRequestConfig(async ({ locale }) => {
// Validate that the incoming `locale` parameter is valid
if (!locales.includes(locale as any)) {
notFound();
}

return {
messages: (await import(`./messages/${locale}.json`)).default,
};
});
6 changes: 6 additions & 0 deletions src/messages/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"Index": {
"getStarted": "Get started by editing",
"by": "By"
}
}
6 changes: 6 additions & 0 deletions src/messages/ro.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"Index": {
"getStarted": "Începe prin a edita fișierul",
"by": "De"
}
}
15 changes: 15 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import createMiddleware from "next-intl/middleware";
import { locales } from "./i18n";

export default createMiddleware({
// A list of all locales that are supported
locales,

// Used when no locale matches
defaultLocale: "ro",
});

export const config = {
// Match only internationalized pathnames
matcher: ["/", "/(en|ro)/:path*"],
};

0 comments on commit 7da65ce

Please sign in to comment.