Skip to content

Commit 555218c

Browse files
committed
i18n!
1 parent 25b668b commit 555218c

12 files changed

+233
-13
lines changed

lang/en-US.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"Home": {
3+
"title": "Home page!"
4+
}
5+
}

lang/ru-RU.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"Home": {
3+
"title": "Главная страница"
4+
}
5+
}

next.config.mjs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import createNextIntlPlugin from "next-intl/plugin";
2+
3+
const withNextIntl = createNextIntlPlugin();
4+
15
/** @type {import('next').NextConfig} */
26
const nextConfig = {};
37

4-
export default nextConfig;
8+
export default withNextIntl(nextConfig);

package-lock.json

+155
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"@tiptap/starter-kit": "^2.4.0",
2525
"dayjs": "^1.11.11",
2626
"next": "14.2.3",
27+
"next-intl": "^3.14.1",
2728
"react": "^18",
2829
"react-dom": "^18"
2930
},

src/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Campground Guide to i18n
2+
<hr/>
3+
4+
- Up for discussion but good practice is only adding the language to `./supportedLocales.json` when it has been completed.
5+
- Languages should be connected to a "region" for example:
6+
- "en" > "en-US" (English - United States) or "en-GB"
7+
- "fr" > "fr-FR" (French - France)
8+
- "ru" > "ru-RU" (Russian - Russia)
9+
- and so forth
10+
- If we decide to use Crowdin, the top 30 languages will be auto-populated in `../lang`
11+
- Once a language is ready to be supported on Campground, the PR must be merged, and added to the `supportedLocales.json`
12+
13+
With this one json file, everything else is handled! :)
14+
15+
TODO:
16+
17+
- [] We need a locale switcher (should only load the locales in `supportedLocales.json`
18+
- [] When using default locale (en-US), the path should be `/`, not `/en-US`
19+
20+
<hr/>
21+
22+
# Tech Used:
23+
- [Next-Intl](https://next-intl-docs.vercel.app/docs/getting-started/app-router/with-i18n-routing)

src/app/layout.tsx src/app/[locale]/layout.tsx

+11-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { ColorSchemeScript, MantineProvider, createTheme } from "@mantine/core";
1313
import { Notifications } from "@mantine/notifications";
1414
import { ModalsProvider } from "@mantine/modals";
1515
import { Inter } from "next/font/google";
16+
import {getMessages} from "next-intl/server";
17+
import {NextIntlClientProvider} from "next-intl";
1618

1719
export const metadata = {
1820
title: {
@@ -40,21 +42,27 @@ const theme = createTheme({
4042
},
4143
});
4244

43-
export default function RootLayout({
45+
export default async function RootLayout({
4446
children,
47+
params: { locale }
4548
}: {
4649
children: React.ReactNode;
50+
params: { locale: string }
4751
}) {
52+
const messages = await getMessages();
53+
4854
return (
49-
<html lang="en" className={inter.className}>
55+
<html lang={locale} className={inter.className}>
5056
<head>
5157
<ColorSchemeScript />
5258
</head>
5359
<body>
5460
<MantineProvider>
5561
<ModalsProvider>
5662
<Notifications />
57-
{children}
63+
<NextIntlClientProvider messages={messages}>
64+
{children}
65+
</NextIntlClientProvider>
5866
</ModalsProvider>
5967
</MantineProvider>
6068
</body>

src/app/[locale]/page.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { useTranslations } from "next-intl";
2+
export default function Home() {
3+
const t = useTranslations("Home")
4+
return <h1>{t("title")}</h1>
5+
}

src/app/page.tsx

-9
This file was deleted.

src/i18n.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { notFound } from "next/navigation";
2+
import { getRequestConfig } from "next-intl/server";
3+
4+
import locales from './supportedLocales.json';
5+
export default getRequestConfig(async ({locale}) => {
6+
if (!locales.includes(locale as any)) notFound();
7+
8+
return {
9+
messages: (await import(`../lang/${locale}.json`)).default
10+
};
11+
})

src/middleware.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import createMiddleware from 'next-intl/middleware'
2+
import locales from './supportedLocales.json';
3+
4+
export default createMiddleware({
5+
locales: locales,
6+
defaultLocale: "en-US"
7+
});
8+
9+
export const config = {
10+
matcher: ["/", `/(${locales.join("|")})/:path*`]
11+
}

src/supportedLocales.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ "en-US", "ru-RU" ]

0 commit comments

Comments
 (0)