From ec147a552fa62898d816997b9310bc3f6c0f08fd Mon Sep 17 00:00:00 2001 From: Pierniki Date: Thu, 9 Nov 2023 23:18:00 +0100 Subject: [PATCH] feat: add global translations --- src/app/Providers.tsx | 14 +++++- src/app/[lang]/category/[slug]/page.tsx | 7 ++- src/app/[lang]/layout.tsx | 5 +- src/app/[lang]/page.tsx | 2 + src/components/ArticleCard/ArticleCard.tsx | 4 +- .../CategoryArticles/CategoryArticles.tsx | 6 ++- .../CategoryArticlesInfinite.tsx | 4 +- .../RecentArticles/RecentArticlesInfinite.tsx | 4 +- .../RecommendedArticles.tsx | 4 +- src/components/Search/RefinementCombobox.tsx | 6 ++- src/components/Search/SearchDialog.tsx | 15 +++++- .../ShareOnSocial/ShareOnSocial.tsx | 5 +- src/i18n/setTranslations.ts | 46 +++++++++++++++++++ src/i18n/useTranslations.tsx | 24 ++++++++++ src/lib/client.ts | 11 +++++ src/lib/queries/translations.ts | 23 ++++++++++ src/lib/tags.ts | 2 +- 17 files changed, 164 insertions(+), 18 deletions(-) create mode 100644 src/i18n/setTranslations.ts create mode 100644 src/i18n/useTranslations.tsx create mode 100644 src/lib/queries/translations.ts diff --git a/src/app/Providers.tsx b/src/app/Providers.tsx index 698e32c5..05ff2646 100644 --- a/src/app/Providers.tsx +++ b/src/app/Providers.tsx @@ -3,8 +3,16 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query" import { ReactNode, useState } from "react" import { TooltipProvider } from "@/components/ui/Tooltip/Tooltip" +import { GlobalTranslations } from "@/i18n/setTranslations" +import { TranslationsProvider } from "@/i18n/useTranslations" -export default function Providers({ children }: { children: ReactNode }) { +export default function Providers({ + children, + translations, +}: { + children: ReactNode + translations: GlobalTranslations +}) { const [queryClient] = useState( () => new QueryClient({ @@ -18,7 +26,9 @@ export default function Providers({ children }: { children: ReactNode }) { ) return ( - {children} + + {children} + ) } diff --git a/src/app/[lang]/category/[slug]/page.tsx b/src/app/[lang]/category/[slug]/page.tsx index 3a93f2e4..c1974eee 100644 --- a/src/app/[lang]/category/[slug]/page.tsx +++ b/src/app/[lang]/category/[slug]/page.tsx @@ -1,6 +1,8 @@ import { Metadata } from "next/types" +import { unstable_setRequestLocale } from "next-intl/server" import { CategoryArticles } from "@/components/CategoryArticles/CategoryArticles" import { Locale } from "@/i18n/i18n" +import { setTranslations } from "@/i18n/setTranslations" import { getMatadataObj } from "@/utils/getMetadataObj" type ArticlePageProps = { params: { slug: string; lang: Locale } } @@ -9,6 +11,9 @@ export async function generateMetadata({ params: { slug } }: ArticlePageProps): return getMatadataObj({ title: `Category - ${slug}` }) } -export default async function Web({ params: { slug } }: ArticlePageProps) { +export default async function Web({ params: { slug, lang } }: ArticlePageProps) { + unstable_setRequestLocale(lang) + await setTranslations(lang) + return } diff --git a/src/app/[lang]/layout.tsx b/src/app/[lang]/layout.tsx index 6df2dd1a..36b49878 100644 --- a/src/app/[lang]/layout.tsx +++ b/src/app/[lang]/layout.tsx @@ -6,6 +6,7 @@ import { Navigation } from "@/components/Navigation/Navigation" import { env } from "@/env.mjs" import { i18n, type Locale } from "@/i18n/i18n" import "@/styles/tailwind.css" +import { setTranslations } from "@/i18n/setTranslations" import { getNavigation } from "@/lib/client" import { GoogleAnalytics } from "../GoogleAnalytics" import Providers from "../Providers" @@ -40,13 +41,13 @@ export default async function Layout({ children, params }: { children: React.Rea const isValidLocale = i18n.locales.some((cur) => cur === locale) if (!isValidLocale) notFound() unstable_setRequestLocale(locale) - + const translations = await setTranslations(locale) const { navigation, footer, logo } = await getNavigation(locale) return ( - +