Skip to content

Commit

Permalink
feat(i18n): disable client-side locale detection
Browse files Browse the repository at this point in the history
  • Loading branch information
9teen90nine committed Jun 26, 2023
1 parent 15c178d commit 21c1c88
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 22 deletions.
5 changes: 2 additions & 3 deletions src/components/HelpPage/HelpPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ExternalPageProps } from '../../utils/declareSsrProps';
import { Page } from '../Page';
import { routes } from '../../hooks/router';
import { AvailableHelpPages } from '../../types/help';
import { setSSRLocale, TLocale } from '../../utils/getLang';
import { TLocale } from '../../utils/getLang';

const Md = dynamic(() => import('../Md'));

Expand Down Expand Up @@ -49,14 +49,13 @@ export async function getStaticProps({ params: { locale, slug } }: { params: { l
return { slug: linkSlug, link: routes.help(locale, linkSlug) };
});

setSSRLocale(locale);

return {
props: {
source,
stat,
yml,
menu,
locale,
},
revalidate: 10,
};
Expand Down
3 changes: 3 additions & 0 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import { ReactQueryDevtools } from '@tanstack/react-query-devtools';

import { usePageLoad } from '../hooks/usePageLoad';
import { trpc } from '../utils/trpcClient';
import { TLocale, setSSRLocale } from '../utils/getLang';

const App = ({ Component, pageProps, router }: AppProps) => {
setSSRLocale((pageProps.locale ?? router.locale) as TLocale);

const pageLoadRef = usePageLoad(router);

return (
Expand Down
10 changes: 1 addition & 9 deletions src/utils/declareSsrProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import { routes } from '../hooks/router';
import { trpcRouter } from '../../trpc/router';
import type { TrpcRouter } from '../../trpc/router';

import { setSSRLocale, TLocale } from './getLang';

interface SSRProps<P = { [key: string]: string }> {
user: Session['user'];
locale: TLocale;
req: GetServerSidePropsContext['req'];
params: P;
query: Record<string, string | string[] | undefined>;
Expand All @@ -26,7 +23,7 @@ export interface ExternalPageProps<P = { [key: string]: string }> extends SSRPro
}

export function declareSsrProps<T = ExternalPageProps>(
cb?: ({ user, locale, req, params, query }: SSRProps) => T,
cb?: ({ user, req, params, query }: SSRProps) => T,
options?: { private: boolean },
) {
return async ({ locale, req, params = {}, query }: GetServerSidePropsContext) => {
Expand All @@ -42,10 +39,6 @@ export function declareSsrProps<T = ExternalPageProps>(
};
}

if (locale) {
setSSRLocale(locale as TLocale);
}

const ssrHelpers = createServerSideHelpers({ router: trpcRouter, ctx: { session }, transformer: superjson });

const ssrTime = Date.now();
Expand All @@ -56,7 +49,6 @@ export function declareSsrProps<T = ExternalPageProps>(
// look at session check in previous condition
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
user: session!.user,
locale: locale as TLocale,
params: params as Record<string, string>,
query,
ssrTime,
Expand Down
10 changes: 0 additions & 10 deletions src/utils/getLang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,12 @@ export type TLocale = (typeof languages)[number];

const defaultLocale: TLocale = languages[0];

const getLangClient = (): TLocale => {
const [_, locale] = window.location.pathname.match(/\/(en|ru)\b/) ?? [];

return (locale as TLocale) ?? defaultLocale;
};

let SSRLocale: TLocale | null = null;

export const setSSRLocale = (locale: TLocale): void => {
SSRLocale = locale;
};

export default function getLang(): TLocale {
if (typeof window !== 'undefined') {
return getLangClient();
}

return SSRLocale ?? defaultLocale;
}

0 comments on commit 21c1c88

Please sign in to comment.