├── .eslintrc.json
├── bun.lockb
├── app
├── favicon.ico
└── [lang]
│ ├── components
│ ├── button.tsx
│ ├── footer.tsx
│ ├── main-layout.tsx
│ ├── header.tsx
│ └── product.tsx
│ ├── dictionaries.ts
│ ├── page.tsx
│ ├── dictionaries
│ ├── en.json
│ └── nl.json
│ ├── globals.css
│ └── layout.tsx
├── next.config.mjs
├── postcss.config.js
├── constants
└── locales.ts
├── i18n-config.ts
├── public
├── write.svg
├── vercel.svg
└── next.svg
├── .gitignore
├── tailwind.config.ts
├── package.json
├── tsconfig.json
├── README.md
└── middleware.ts
/.eslintrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "next/core-web-vitals"
3 | }
4 |
--------------------------------------------------------------------------------
/bun.lockb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rendrdotio/i18n-basics-video/HEAD/bun.lockb
--------------------------------------------------------------------------------
/app/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rendrdotio/i18n-basics-video/HEAD/app/favicon.ico
--------------------------------------------------------------------------------
/next.config.mjs:
--------------------------------------------------------------------------------
1 | /** @type {import('next').NextConfig} */
2 | const nextConfig = {};
3 |
4 | export default nextConfig;
5 |
--------------------------------------------------------------------------------
/postcss.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | plugins: {
3 | tailwindcss: {},
4 | autoprefixer: {},
5 | },
6 | };
7 |
--------------------------------------------------------------------------------
/constants/locales.ts:
--------------------------------------------------------------------------------
1 | const locales = [{ locale: "en" }, { locale: "nl" }];
2 |
3 | const defaultLocale = "en";
4 |
5 | export { defaultLocale, locales };
6 |
--------------------------------------------------------------------------------
/i18n-config.ts:
--------------------------------------------------------------------------------
1 | import { defaultLocale, locales } from "@/constants/locales";
2 |
3 | export const i18n = {
4 | defaultLocale,
5 | locales: locales.map((l) => l.locale),
6 | } as const;
7 |
8 | export type Locale = (typeof i18n)["locales"][number];
9 |
--------------------------------------------------------------------------------
/app/[lang]/components/button.tsx:
--------------------------------------------------------------------------------
1 | export default function Button({ dict }: { dict: any }) {
2 | return (
3 |
6 | );
7 | }
8 |
--------------------------------------------------------------------------------
/app/[lang]/components/footer.tsx:
--------------------------------------------------------------------------------
1 | export default function Footer({ dict }: { dict: any }) {
2 | const date = new Date();
3 |
4 | return (
5 |
6 |
© Logo Company {date.getFullYear()}
7 |
{dict.footer.allRights}
8 |
9 | );
10 | }
11 |
--------------------------------------------------------------------------------
/app/[lang]/dictionaries.ts:
--------------------------------------------------------------------------------
1 | import "server-only";
2 |
3 | const dictionaries: any = {
4 | en: () => import("./dictionaries/en.json").then((module) => module.default),
5 | nl: () => import("./dictionaries/nl.json").then((module) => module.default),
6 | };
7 |
8 | export const getDictionary = async (locale: string) => dictionaries[locale]();
9 |
--------------------------------------------------------------------------------
/public/write.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 | .yarn/install-state.gz
8 |
9 | # testing
10 | /coverage
11 |
12 | # next.js
13 | /.next/
14 | /out/
15 |
16 | # production
17 | /build
18 |
19 | # misc
20 | .DS_Store
21 | *.pem
22 |
23 | # debug
24 | npm-debug.log*
25 | yarn-debug.log*
26 | yarn-error.log*
27 |
28 | # local env files
29 | .env*.local
30 |
31 | # vercel
32 | .vercel
33 |
34 | # typescript
35 | *.tsbuildinfo
36 | next-env.d.ts
37 |
--------------------------------------------------------------------------------
/app/[lang]/components/main-layout.tsx:
--------------------------------------------------------------------------------
1 | import Header from "@/components/header";
2 | import Footer from "@/components/footer";
3 |
4 | type Props = {
5 | children: React.ReactNode;
6 | dict: any;
7 | lang: string;
8 | };
9 |
10 | export default function MainLayout({ children, dict, lang }: Props) {
11 | return (
12 |
13 |
14 |
{children}
15 |
16 |
17 | );
18 | }
19 |
--------------------------------------------------------------------------------
/tailwind.config.ts:
--------------------------------------------------------------------------------
1 | import type { Config } from "tailwindcss";
2 |
3 | const config: Config = {
4 | content: [
5 | "./pages/**/*.{js,ts,jsx,tsx,mdx}",
6 | "./components/**/*.{js,ts,jsx,tsx,mdx}",
7 | "./app/**/*.{js,ts,jsx,tsx,mdx}",
8 | ],
9 | theme: {
10 | extend: {
11 | backgroundImage: {
12 | "gradient-radial": "radial-gradient(var(--tw-gradient-stops))",
13 | "gradient-conic":
14 | "conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))",
15 | },
16 | },
17 | },
18 | plugins: [],
19 | };
20 | export default config;
21 |
--------------------------------------------------------------------------------
/public/vercel.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/app/[lang]/page.tsx:
--------------------------------------------------------------------------------
1 | import { getDictionary } from "./dictionaries";
2 | import Product from "@/components/product";
3 |
4 | type Props = {
5 | params: { lang: string };
6 | };
7 |
8 | export default async function Home({ params }: Props) {
9 | const { lang } = params;
10 |
11 | const dict = await getDictionary(lang);
12 |
13 | return (
14 |
15 |
16 | {dict.products.catalog}
17 |
18 |
22 |
23 | );
24 | }
25 |
--------------------------------------------------------------------------------
/app/[lang]/dictionaries/en.json:
--------------------------------------------------------------------------------
1 | {
2 | "title": "The Logo",
3 | "products": {
4 | "catalog": "Catalog",
5 | "cart": "Add to Cart",
6 | "productOne": {
7 | "altText": "A pencil icon",
8 | "title": "Writes Upside-Down",
9 | "description": "The Zero Gravity Pen can be used to write in any orientation, including upside-down. It even works in outer space."
10 | },
11 | "productTwo": {
12 | "altText": "A pencil icon",
13 | "title": "Writes Any Language",
14 | "description": "The i18n pen will translate your text for you. Just write your phrase and magically it will be translated to any language you want!"
15 | }
16 | },
17 | "footer": {
18 | "allRights": "All rights reserved (not really)"
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/app/[lang]/dictionaries/nl.json:
--------------------------------------------------------------------------------
1 | {
2 | "title": "Het Logo",
3 | "products": {
4 | "catalog": "Catalogus",
5 | "cart": "In winkelwagen",
6 | "productOne": {
7 | "altText": "Een potlood",
8 | "title": "Ondersteboven schrijven",
9 | "description": "Met de Zero Gravity Pen kun je in elke richting schrijven, ook ondersteboven. Hij werkt zelfs in de ruimte."
10 | },
11 | "productTwo": {
12 | "altText": "Een potlood",
13 | "title": "Schrijft elke taal",
14 | "description": "De i18n pen vertaalt je tekst voor je. Schrijf gewoon je zin en op magische wijze wordt deze vertaald naar elke taal die je maar wilt!"
15 | }
16 | },
17 | "footer": {
18 | "allRights": "Alle rechten voorbehouden (niet echt)"
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/app/[lang]/globals.css:
--------------------------------------------------------------------------------
1 | @tailwind base;
2 | @tailwind components;
3 | @tailwind utilities;
4 |
5 | :root {
6 | --foreground-rgb: 0, 0, 0;
7 | --background-start-rgb: 214, 219, 220;
8 | --background-end-rgb: 255, 255, 255;
9 | }
10 |
11 | @media (prefers-color-scheme: dark) {
12 | :root {
13 | --foreground-rgb: 255, 255, 255;
14 | --background-start-rgb: 0, 0, 0;
15 | --background-end-rgb: 0, 0, 0;
16 | }
17 | }
18 |
19 | body {
20 | color: rgb(var(--foreground-rgb));
21 | background: linear-gradient(
22 | to bottom,
23 | transparent,
24 | rgb(var(--background-end-rgb))
25 | )
26 | rgb(var(--background-start-rgb));
27 | }
28 |
29 | @layer utilities {
30 | .text-balance {
31 | text-wrap: balance;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/app/[lang]/components/header.tsx:
--------------------------------------------------------------------------------
1 | import Link from "next/link";
2 |
3 | export default function Header({ dict, lang }: { dict: any; lang: string }) {
4 | return (
5 |
6 |
{dict.title}
7 |
8 |
9 |
10 | EN
11 |
12 |
13 |
14 |
15 | NL
16 |
17 |
18 |
19 |
20 | );
21 | }
22 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "i18n-basics",
3 | "version": "0.1.0",
4 | "private": true,
5 | "scripts": {
6 | "dev": "next dev",
7 | "build": "next build",
8 | "start": "next start",
9 | "lint": "next lint"
10 | },
11 | "dependencies": {
12 | "@formatjs/intl-localematcher": "^0.5.4",
13 | "@types/negotiator": "^0.6.3",
14 | "negotiator": "^0.6.3",
15 | "next": "14.1.0",
16 | "react": "^18",
17 | "react-dom": "^18"
18 | },
19 | "devDependencies": {
20 | "typescript": "^5",
21 | "@types/node": "^20",
22 | "@types/react": "^18",
23 | "@types/react-dom": "^18",
24 | "autoprefixer": "^10.0.1",
25 | "postcss": "^8",
26 | "tailwindcss": "^3.3.0",
27 | "eslint": "^8",
28 | "eslint-config-next": "14.1.0"
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "baseUrl": ".",
4 | "lib": ["dom", "dom.iterable", "esnext"],
5 | "allowJs": true,
6 | "skipLibCheck": true,
7 | "strict": true,
8 | "noEmit": true,
9 | "esModuleInterop": true,
10 | "module": "esnext",
11 | "moduleResolution": "bundler",
12 | "resolveJsonModule": true,
13 | "isolatedModules": true,
14 | "jsx": "preserve",
15 | "incremental": true,
16 | "plugins": [
17 | {
18 | "name": "next"
19 | }
20 | ],
21 | "paths": {
22 | "@/constants/*": ["./constants/*"],
23 | "@/components/*": ["./app/[lang]/components/*"],
24 | "@/*": ["./*"]
25 | }
26 | },
27 | "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
28 | "exclude": ["node_modules"]
29 | }
30 |
--------------------------------------------------------------------------------
/app/[lang]/components/product.tsx:
--------------------------------------------------------------------------------
1 | import Image from "next/image";
2 | import Button from "@/components/button";
3 | import write from "public/write.svg";
4 |
5 | type Props = {
6 | dict: any;
7 | id: string;
8 | };
9 |
10 | export default function Product({ dict, id }: Props) {
11 | return (
12 |
13 |
14 |
19 |
20 |
21 | {dict.products[id].title}
22 |
23 |
24 | {dict.products[id].description}
25 |
26 |
27 |
28 | );
29 | }
30 |
--------------------------------------------------------------------------------
/app/[lang]/layout.tsx:
--------------------------------------------------------------------------------
1 | import type { Metadata } from "next";
2 | import { Inter } from "next/font/google";
3 | import "./globals.css";
4 | import { getDictionary } from "./dictionaries";
5 | import MainLayout from "@/components/main-layout";
6 |
7 | const inter = Inter({ subsets: ["latin"] });
8 |
9 | export const metadata: Metadata = {
10 | title: "Create Next App",
11 | description: "Generated by create next app",
12 | };
13 |
14 | export async function generateStaticParams() {
15 | return [{ lang: "en" }, { lang: "nl" }];
16 | }
17 |
18 | type Props = {
19 | children: React.ReactNode;
20 | params: { lang: string };
21 | };
22 |
23 | export default async function RootLayout({
24 | children,
25 | params: { lang },
26 | }: Readonly) {
27 | const dict = await getDictionary(lang);
28 |
29 | return (
30 |
31 |
32 |
33 | {children}
34 |
35 |
36 |
37 | );
38 | }
39 |
--------------------------------------------------------------------------------
/public/next.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
2 |
3 | ## Getting Started
4 |
5 | First, run the development server:
6 |
7 | ```bash
8 | npm run dev
9 | # or
10 | yarn dev
11 | # or
12 | pnpm dev
13 | # or
14 | bun dev
15 | ```
16 |
17 | Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
18 |
19 | You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
20 |
21 | This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.
22 |
23 | ## Learn More
24 |
25 | To learn more about Next.js, take a look at the following resources:
26 |
27 | - [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
28 | - [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
29 |
30 | You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
31 |
32 | ## Deploy on Vercel
33 |
34 | The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
35 |
36 | Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
37 |
--------------------------------------------------------------------------------
/middleware.ts:
--------------------------------------------------------------------------------
1 | import { NextResponse, type NextRequest } from "next/server";
2 | import { defaultLocale } from "@/constants/locales";
3 | import { i18n } from "@/i18n-config";
4 |
5 | export function middleware(request: NextRequest) {
6 | // Check if there is any supported locale in the pathname
7 | const { pathname } = request.nextUrl;
8 |
9 | if (
10 | pathname.startsWith(`/${defaultLocale}/`) ||
11 | pathname === `/${defaultLocale}`
12 | ) {
13 | // The incoming request is for /en/whatever, so we'll reDIRECT to /whatever
14 | return NextResponse.redirect(
15 | new URL(
16 | pathname.replace(
17 | `/${defaultLocale}`,
18 | pathname === `/${defaultLocale}` ? "/" : ""
19 | ),
20 | request.url
21 | )
22 | );
23 | }
24 |
25 | const pathnameIsMissingLocale = i18n.locales.every(
26 | (locale) => !pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`
27 | );
28 |
29 | if (pathnameIsMissingLocale) {
30 | // Now for EITHER /en or /nl (for example) we're going to tell Next.js that the request is for /en/whatever
31 | // or /nl/whatever, and then reWRITE the request to that it is handled properly.
32 | return NextResponse.rewrite(
33 | new URL(
34 | `/${defaultLocale}${pathname}${request.nextUrl.search}`,
35 | request.nextUrl.href
36 | )
37 | );
38 | }
39 | }
40 |
41 | export const config = {
42 | matcher: [
43 | // Skip all internal paths (_next)
44 | "/((?!_next).*)",
45 | // Optional: only run on root (/) URL
46 | // '/'
47 | ],
48 | };
49 |
--------------------------------------------------------------------------------