2021-10-12 13:11:33 +00:00
|
|
|
import { useTranslation } from "next-i18next";
|
2021-10-14 19:10:44 +00:00
|
|
|
import { useEffect } from "react";
|
2021-10-12 13:11:33 +00:00
|
|
|
|
2022-07-22 17:27:06 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2021-10-12 13:11:33 +00:00
|
|
|
|
2021-10-14 19:10:44 +00:00
|
|
|
export function useViewerI18n() {
|
2022-06-19 15:02:00 +00:00
|
|
|
return trpc.useQuery(["viewer.public.i18n"], {
|
2021-10-14 19:10:44 +00:00
|
|
|
staleTime: Infinity,
|
2022-07-18 18:59:51 +00:00
|
|
|
/**
|
|
|
|
* i18n should never be clubbed with other queries, so that it's caching can be managed independently.
|
|
|
|
* We intend to not cache i18n query
|
|
|
|
**/
|
|
|
|
context: { skipBatch: true },
|
2021-10-14 19:10:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-14 10:57:49 +00:00
|
|
|
/**
|
|
|
|
* Auto-switches locale client-side to the logged in user's preference
|
|
|
|
*/
|
|
|
|
const I18nLanguageHandler = (): null => {
|
2021-10-12 13:11:33 +00:00
|
|
|
const { i18n } = useTranslation("common");
|
2021-10-14 19:10:44 +00:00
|
|
|
const locale = useViewerI18n().data?.locale;
|
2021-10-14 10:57:49 +00:00
|
|
|
|
2021-10-14 19:10:44 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (locale && i18n.language && i18n.language !== locale) {
|
|
|
|
if (typeof i18n.changeLanguage === "function") i18n.changeLanguage(locale);
|
|
|
|
}
|
|
|
|
}, [locale, i18n]);
|
2021-10-14 10:57:49 +00:00
|
|
|
|
2021-10-12 13:11:33 +00:00
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default I18nLanguageHandler;
|