diff --git a/web/lib/locale-cookie.ts b/web/lib/locale-cookie.ts index db31a4bb..b21eb33b 100644 --- a/web/lib/locale-cookie.ts +++ b/web/lib/locale-cookie.ts @@ -3,14 +3,16 @@ import {defaultLocale, supportedLocales, Locale} from "common/constants" let cachedLocale: string | null | undefined = null -export function getLocale(req?: IncomingMessage): string { +export function getLocale( + // req?: IncomingMessage +): string { // if (cachedLocale) return cachedLocale console.log('cachedLocale', cachedLocale) let cookie = null // Server - if (req?.headers?.cookie) { - cookie = req.headers.cookie - } + // if (req?.headers?.cookie) { + // cookie = req.headers.cookie + // } // Client if (typeof document !== 'undefined') { @@ -24,6 +26,7 @@ export function getLocale(req?: IncomingMessage): string { .find(c => c.startsWith('lang=')) ?.split('=')[1] ?.split(' ')[0] + ?.replace(";", "") console.log('Locale cookie', cachedLocale) } diff --git a/web/middleware.ts b/web/middleware.ts new file mode 100644 index 00000000..825c6d91 --- /dev/null +++ b/web/middleware.ts @@ -0,0 +1,6 @@ +import { NextResponse } from 'next/server'; +export function middleware(req: any) { + const lang = req.cookies.get('lang') ?? 'en'; + req.nextUrl.searchParams.set('lang', lang); + return NextResponse.next(); +} \ No newline at end of file diff --git a/web/pages/_app.tsx b/web/pages/_app.tsx index c43269c1..a0f48ef3 100644 --- a/web/pages/_app.tsx +++ b/web/pages/_app.tsx @@ -87,13 +87,13 @@ function printBuildInfo() { // specially treated props that may be present in the server/static props type PageProps = { auth?: AuthUser } -function MyApp(props: AppProps & { locale: string }) { +function MyApp(props: AppProps) { const {Component, pageProps} = props useEffect(printBuildInfo, []) useHasLoaded() const router = useRouter() - const [locale, setLocaleState] = useState(props.locale); + const [locale, setLocaleState] = useState(getLocale()); const setLocale = (newLocale: string) => { document.cookie = `lang=${newLocale}; path=/; max-age=31536000`; setLocaleState(newLocale); @@ -208,14 +208,4 @@ function MyApp(props: AppProps & { locale: string }) { ) } -MyApp.getInitialProps = async (appContext: AppContext) => { - const appProps = await import('next/app').then(m => m.default.getInitialProps(appContext)) - const locale = getLocale(appContext.ctx.req) - - return { - ...appProps, - locale - }; -}; - export default MyApp