mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-24 13:41:25 +10:00
45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import "@/client/libs/dayjs";
|
|
|
|
import { i18n } from "@lingui/core";
|
|
import { detect, fromNavigator, fromStorage, fromUrl } from "@lingui/detect-locale";
|
|
import { I18nProvider } from "@lingui/react";
|
|
import { useEffect } from "react";
|
|
|
|
import { defaultLocale, dynamicActivate } from "../libs/lingui";
|
|
import { updateUser } from "../services/user";
|
|
import { useAuthStore } from "../stores/auth";
|
|
|
|
type Props = {
|
|
children: React.ReactNode;
|
|
};
|
|
|
|
export const LocaleProvider = ({ children }: Props) => {
|
|
const userLocale = useAuthStore((state) => state.user?.locale);
|
|
|
|
useEffect(() => {
|
|
const detectedLocale = detect(
|
|
fromUrl("locale"),
|
|
fromStorage("locale"),
|
|
fromNavigator(),
|
|
userLocale,
|
|
defaultLocale,
|
|
)!;
|
|
|
|
dynamicActivate(detectedLocale);
|
|
}, [userLocale]);
|
|
|
|
return <I18nProvider i18n={i18n}>{children}</I18nProvider>;
|
|
};
|
|
|
|
export const changeLanguage = async (locale: string) => {
|
|
// Update locale in local storage
|
|
window.localStorage.setItem("locale", locale);
|
|
|
|
// Update locale in user profile, if authenticated
|
|
const state = useAuthStore.getState();
|
|
if (state.user) await updateUser({ locale }).catch(() => null);
|
|
|
|
// Reload the page for language switch to take effect
|
|
window.location.reload();
|
|
};
|