From 778485b31dac5502c38efbf9839228a777bc7507 Mon Sep 17 00:00:00 2001 From: Greg Pabian <35925521+grzpab@users.noreply.github.com> Date: Tue, 10 Oct 2023 18:36:28 +0200 Subject: [PATCH] refactor: implementation of locale calculated server-side (#11534) --- apps/web/components/I18nLanguageHandler.tsx | 50 +------------------ apps/web/components/PageWrapper.tsx | 3 -- apps/web/lib/app-providers.tsx | 34 +++++++------ apps/web/lib/withLocale.tsx | 3 ++ apps/web/lib/withNonce.tsx | 19 ++++--- apps/web/pages/_app.tsx | 32 +++++++++++- apps/web/pages/_document.tsx | 25 ++++++++-- apps/web/pages/auth/forgot-password/[id].tsx | 4 +- apps/web/pages/auth/forgot-password/index.tsx | 4 +- apps/web/pages/auth/login.tsx | 3 +- .../web/pages/getting-started/[[...step]].tsx | 4 +- .../web/pages/settings/my-account/general.tsx | 6 ++- apps/web/server/lib/ssr.ts | 4 +- packages/features/auth/lib/getLocale.ts | 33 ++++++++++++ packages/lib/getLocaleFromRequest.ts | 22 -------- packages/trpc/server/createContext.ts | 4 +- .../server/middlewares/sessionMiddleware.ts | 2 +- .../routers/publicViewer/i18n.handler.ts | 7 --- 18 files changed, 139 insertions(+), 120 deletions(-) create mode 100644 apps/web/lib/withLocale.tsx create mode 100644 packages/features/auth/lib/getLocale.ts delete mode 100644 packages/lib/getLocaleFromRequest.ts diff --git a/apps/web/components/I18nLanguageHandler.tsx b/apps/web/components/I18nLanguageHandler.tsx index 2db3dcc792..29ffcd97d9 100644 --- a/apps/web/components/I18nLanguageHandler.tsx +++ b/apps/web/components/I18nLanguageHandler.tsx @@ -1,12 +1,7 @@ -import { lookup } from "bcp-47-match"; -import { useSession } from "next-auth/react"; -import { useTranslation } from "next-i18next"; -import { useEffect } from "react"; - import { CALCOM_VERSION } from "@calcom/lib/constants"; import { trpc } from "@calcom/trpc/react"; -function useViewerI18n(locale: string) { +export function useViewerI18n(locale: string) { return trpc.viewer.public.i18n.useQuery( { locale, CalComVersion: CALCOM_VERSION }, { @@ -19,46 +14,3 @@ function useViewerI18n(locale: string) { } ); } - -function useClientLocale(locales: string[]) { - const session = useSession(); - // If the user is logged in, use their locale - if (session.data?.user.locale) return session.data.user.locale; - // If the user is not logged in, use the browser locale - if (typeof window !== "undefined") { - // This is the only way I found to ensure the prefetched locale is used on first render - // FIXME: Find a better way to pick the best matching locale from the browser - return lookup(locales, window.navigator.language) || window.navigator.language; - } - // If the browser is not available, use English - return "en"; -} - -export function useClientViewerI18n(locales: string[]) { - const clientLocale = useClientLocale(locales); - return useViewerI18n(clientLocale); -} - -/** - * Auto-switches locale client-side to the logged in user's preference - */ -const I18nLanguageHandler = (props: { locales: string[] }) => { - const { locales } = props; - const { i18n } = useTranslation("common"); - const locale = useClientViewerI18n(locales).data?.locale || i18n.language; - - useEffect(() => { - // bail early when i18n = {} - if (Object.keys(i18n).length === 0) return; - // if locale is ready and the i18n.language does != locale - changeLanguage - if (locale && i18n.language !== locale) { - i18n.changeLanguage(locale); - } - // set dir="rtl|ltr" - document.dir = i18n.dir(); - document.documentElement.setAttribute("lang", locale); - }, [locale, i18n]); - return null; -}; - -export default I18nLanguageHandler; diff --git a/apps/web/components/PageWrapper.tsx b/apps/web/components/PageWrapper.tsx index 2ac8afca7c..5690b369bf 100644 --- a/apps/web/components/PageWrapper.tsx +++ b/apps/web/components/PageWrapper.tsx @@ -13,8 +13,6 @@ import type { AppProps } from "@lib/app-providers"; import AppProviders from "@lib/app-providers"; import { seoConfig } from "@lib/config/next-seo.config"; -import I18nLanguageHandler from "@components/I18nLanguageHandler"; - export interface CalPageWrapper { (props?: AppProps): JSX.Element; PageWrapper?: AppProps["Component"]["PageWrapper"]; @@ -72,7 +70,6 @@ function PageWrapper(props: AppProps) { } {...seoConfig.defaultNextSeo} /> -