From af999b8d8e7f27984cd20f68952663cbd0b60fe4 Mon Sep 17 00:00:00 2001 From: Andres Parra Date: Fri, 18 Aug 2023 23:17:54 -0500 Subject: [PATCH] feature: add i18n --- .eslintrc.json | 5 +- package.json | 12 +- src/app/{ => [lang]}/globals.css | 0 src/app/[lang]/layout.tsx | 39 ++++++ src/app/[lang]/page.tsx | 30 +++++ .../posts/[slug]/getPostService.ts | 0 src/app/{ => [lang]}/posts/[slug]/page.tsx | 0 src/app/{ => [lang]}/posts/page.tsx | 3 +- src/app/i18n/client.ts | 59 ++++++++ src/app/i18n/index.ts | 37 +++++ src/app/i18n/locales/en/translation.json | 15 +++ src/app/i18n/locales/es/translation.json | 15 +++ src/app/i18n/settings.ts | 15 +++ src/app/layout.tsx | 30 ----- src/app/page.tsx | 34 ----- src/components/Footer.tsx | 11 +- src/components/Header.tsx | 25 ++-- src/components/LanguageSwitcher.tsx | 100 ++++++++++++++ src/components/Link.tsx | 7 +- src/components/ThemeSwitch.tsx | 2 +- src/middleware.ts | 45 +++++++ yarn.lock | 126 +++++++++++++++++- 22 files changed, 528 insertions(+), 82 deletions(-) rename src/app/{ => [lang]}/globals.css (100%) create mode 100644 src/app/[lang]/layout.tsx create mode 100644 src/app/[lang]/page.tsx rename src/app/{ => [lang]}/posts/[slug]/getPostService.ts (100%) rename src/app/{ => [lang]}/posts/[slug]/page.tsx (100%) rename src/app/{ => [lang]}/posts/page.tsx (99%) create mode 100644 src/app/i18n/client.ts create mode 100644 src/app/i18n/index.ts create mode 100644 src/app/i18n/locales/en/translation.json create mode 100644 src/app/i18n/locales/es/translation.json create mode 100644 src/app/i18n/settings.ts delete mode 100644 src/app/layout.tsx delete mode 100644 src/app/page.tsx create mode 100644 src/components/LanguageSwitcher.tsx create mode 100644 src/middleware.ts diff --git a/.eslintrc.json b/.eslintrc.json index bffb357..403e316 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,3 +1,6 @@ { - "extends": "next/core-web-vitals" + "extends": "next/core-web-vitals", + "rules": { + "react-hooks/rules-of-hooks": "off" // Checks rules of Hooks + } } diff --git a/package.json b/package.json index 74689e7..3876e1b 100644 --- a/package.json +++ b/package.json @@ -12,21 +12,31 @@ "lint": "next lint" }, "dependencies": { + "@formatjs/intl-localematcher": "^0.4.0", "@types/node": "20.4.5", "@types/react": "18.2.17", "@types/react-dom": "18.2.7", + "accept-language": "^3.0.18", "autoprefixer": "10.4.14", "eslint": "8.46.0", "eslint-config-next": "13.4.12", "gray-matter": "^4.0.3", + "i18next": "^23.4.4", + "i18next-browser-languagedetector": "^7.1.0", + "i18next-resources-to-backend": "^1.1.4", + "negotiator": "^0.6.3", "next": "13.4.12", + "next-i18next": "^14.0.0", "postcss": "8.4.27", "react": "18.2.0", "react-dom": "18.2.0", + "react-i18next": "^13.1.2", "remark": "^14.0.3", "remark-html": "^15.0.2", "tailwindcss": "3.3.3", "typescript": "5.1.6" }, - "devDependencies": {} + "devDependencies": { + "@types/negotiator": "^0.6.1" + } } diff --git a/src/app/globals.css b/src/app/[lang]/globals.css similarity index 100% rename from src/app/globals.css rename to src/app/[lang]/globals.css diff --git a/src/app/[lang]/layout.tsx b/src/app/[lang]/layout.tsx new file mode 100644 index 0000000..bf4753e --- /dev/null +++ b/src/app/[lang]/layout.tsx @@ -0,0 +1,39 @@ +import { dir } from "i18next"; +import { Inter } from "next/font/google"; +import { ReactNode } from "react"; +import "./globals.css"; + +import Container from "@/components/Container"; +import Footer from "@/components/Footer"; +import Header from "@/components/Header"; +import { ThemeProvider } from "@/context/ThemeContext"; +import { languages } from "@/app/i18n/settings"; + +export async function generateStaticParams() { + return languages.map((lng) => ({ lng })); +} + +const inter = Inter({ subsets: ["latin"] }); + +type Props = { + children: ReactNode; + params: { lang: string }; +}; + +async function RootLayout({ children, params: { lang } }: Props) { + return ( + + + +
+ {children} +