From 6532d0de4dcd292e36eae157de1246c6b76cfa26 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Sat, 6 May 2023 18:07:44 +0100 Subject: [PATCH] refactor: move useAcceptLang to lib i18n --- src/app/(home)/components/Header.tsx | 4 +--- src/app/(home)/page.tsx | 7 ++----- src/lib/i18n/index.ts | 11 ++++++++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/app/(home)/components/Header.tsx b/src/app/(home)/components/Header.tsx index 9b7fb2d5..89f149ca 100644 --- a/src/app/(home)/components/Header.tsx +++ b/src/app/(home)/components/Header.tsx @@ -2,7 +2,6 @@ import { ConnectButton } from "~/components/common/ConnectButton" import { Logo } from "~/components/common/Logo" import { Image } from "~/components/ui/Image" import { UniLink } from "~/components/ui/UniLink" -import { useAcceptLang } from "~/hooks/useAcceptLang" import { GITHUB_LINK } from "~/lib/env" import { useTranslation } from "~/lib/i18n" import { cn } from "~/lib/utils" @@ -29,8 +28,7 @@ const tabs = [ ] export async function Header() { - const lang = useAcceptLang() - const { t } = await useTranslation(lang, "index") + const { t } = await useTranslation("index") // const pathname = usePathname() // TODO how to set active state for Link in RSC, // make be client component and add activeClass props or some way else diff --git a/src/app/(home)/page.tsx b/src/app/(home)/page.tsx index 3b6971b5..5cd8e364 100644 --- a/src/app/(home)/page.tsx +++ b/src/app/(home)/page.tsx @@ -1,22 +1,19 @@ import Link from "next/link" import { memo } from "react" -import { Trans } from "react-i18next/TransWithoutContext" import { Logo } from "~/components/common/Logo" import { Button } from "~/components/ui/Button" import { Image } from "~/components/ui/Image" -import { useAcceptLang } from "~/hooks/useAcceptLang" import { CSB_SCAN, GITHUB_LINK } from "~/lib/env" import { getSiteLink } from "~/lib/helpers" -import { useTranslation } from "~/lib/i18n" +import { Trans, useTranslation } from "~/lib/i18n" import { default as OriginEntranceButton } from "./components/EntranceButton" import { Integration } from "./components/Integrations" import { ShowCase } from "./components/Showcase" async function Home() { - const lang = useAcceptLang() - const { t, i18n } = await useTranslation(lang, "index") + const { t, i18n } = await useTranslation("index") const EntranceButton = memo(function Button() { return ( diff --git a/src/lib/i18n/index.ts b/src/lib/i18n/index.ts index 5bc9f30a..637ec492 100644 --- a/src/lib/i18n/index.ts +++ b/src/lib/i18n/index.ts @@ -1,7 +1,10 @@ import { createInstance } from "i18next" import resourcesToBackend from "i18next-resources-to-backend" +import { Trans as TransW } from "react-i18next/TransWithoutContext" import { initReactI18next } from "react-i18next/initReactI18next" +import { useAcceptLang } from "~/hooks/useAcceptLang" + import { getOptions } from "./settings" const initI18next = async (lng: string, ns: string) => { @@ -19,13 +22,15 @@ const initI18next = async (lng: string, ns: string) => { } export async function useTranslation( - lng: string, ns: string, options: { keyPrefix?: string } = {}, ) { - const i18nextInstance = await initI18next(lng, ns) + const lang = useAcceptLang() + const i18nextInstance = await initI18next(lang, ns) return { - t: i18nextInstance.getFixedT(lng, ns, options.keyPrefix), + t: i18nextInstance.getFixedT(lang, ns, options.keyPrefix), i18n: i18nextInstance, } } + +export const Trans = TransW