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-11-10 23:40:01 +00:00
|
|
|
return trpc.viewer.public.i18n.useQuery(undefined, {
|
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
|
|
|
|
**/
|
2022-09-29 16:58:29 +00:00
|
|
|
trpc: {
|
|
|
|
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
|
|
|
|
*/
|
2023-07-19 09:19:53 +00:00
|
|
|
const useI18nLanguageHandler = () => {
|
2021-10-12 13:11:33 +00:00
|
|
|
const { i18n } = useTranslation("common");
|
2023-04-21 00:45:22 +00:00
|
|
|
const locale = useViewerI18n().data?.locale || i18n.language;
|
2021-10-14 10:57:49 +00:00
|
|
|
|
2021-10-14 19:10:44 +00:00
|
|
|
useEffect(() => {
|
2023-04-21 00:45:22 +00:00
|
|
|
// bail early when i18n = {}
|
2023-07-19 09:19:53 +00:00
|
|
|
if (Object.keys(i18n).length === 0) return;
|
2023-04-21 00:45:22 +00:00
|
|
|
// if locale is ready and the i18n.language does != locale - changeLanguage
|
|
|
|
if (locale && i18n.language !== locale) {
|
|
|
|
i18n.changeLanguage(locale);
|
2021-10-14 19:10:44 +00:00
|
|
|
}
|
2023-04-21 00:45:22 +00:00
|
|
|
// set dir="rtl|ltr"
|
|
|
|
document.dir = i18n.dir();
|
2023-01-03 10:09:05 +00:00
|
|
|
document.documentElement.setAttribute("lang", locale);
|
2021-10-14 19:10:44 +00:00
|
|
|
}, [locale, i18n]);
|
2021-10-12 13:11:33 +00:00
|
|
|
};
|
|
|
|
|
2023-07-19 09:19:53 +00:00
|
|
|
export default useI18nLanguageHandler;
|