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
|
|
|
|
2021-10-14 10:57:49 +00:00
|
|
|
import { trpc } from "@lib/trpc";
|
2021-10-12 13:11:33 +00:00
|
|
|
|
2021-10-14 19:10:44 +00:00
|
|
|
export function useViewerI18n() {
|
|
|
|
return trpc.useQuery(["viewer.i18n"], {
|
|
|
|
staleTime: Infinity,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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;
|