From 1e468c938252586b46f44850b53d0643b3d793a2 Mon Sep 17 00:00:00 2001 From: arek-bitnoise Date: Fri, 28 Jun 2024 16:49:22 +0200 Subject: [PATCH] refactor: Simplified i18n context and provider --- src/context/LocaleProvider/LocaleProvider.tsx | 78 ++++++------------- src/context/LocaleProvider/index.ts | 2 +- src/context/LocaleProvider/localeContext.tsx | 4 +- src/context/LocaleProvider/types.ts | 2 - 4 files changed, 27 insertions(+), 59 deletions(-) diff --git a/src/context/LocaleProvider/LocaleProvider.tsx b/src/context/LocaleProvider/LocaleProvider.tsx index d4818a9..0075b1d 100644 --- a/src/context/LocaleProvider/LocaleProvider.tsx +++ b/src/context/LocaleProvider/LocaleProvider.tsx @@ -1,57 +1,39 @@ -import { useCallback, useContext, useEffect, useState } from "react"; +import { useContext, useEffect, useState } from "react"; import dayjs from "dayjs"; import { localeContext } from "./localeContext"; import { locales } from "./locales"; -import { LocaleProviderProps, LocaleType } from "./types"; +import { LocaleProviderProps } from "./types"; const LocaleProvider = ({ children, lang, translations }: LocaleProviderProps) => { - const [localLang, setLocalLang] = useState("en"); - const localesData = locales.getLocales(); - - const findLocale = useCallback(() => { - const locale = localesData.find((l) => { - return l.id === localLang; - }); - - - if (typeof locale?.dayjsTranslations === "object") { - dayjs.locale(locale.dayjsTranslations); - } - - return locale || localesData[0]; - - }, [localLang]); - - const [currentLocale, setCurrentLocale] = useState(findLocale()); - - const saveCurrentLocale = (locale: LocaleType) => { - localStorage.setItem("locale", locale.translateCode); - setCurrentLocale(locale); - }; + const [currentLocale, setCurrentLocale] = useState( + locales.getLocales().filter((locale) => locale.id === "en")[0] + ); useEffect(() => { - translations?.forEach((translation) => { - const localeData = localesData.find((el) => el.id === translation.id); - if (!localeData) { - locales.addLocales(translation); - } + const overwrittenLocalesData = locales.locales.map((locale) => { + let localeTemp = locale; + translations?.forEach((translation) => { + if (locale.id === translation.id) { + localeTemp = translation; + } + }); + return localeTemp; }); - }, [translations]); - useEffect(() => { - const localeId = localStorage.getItem("locale"); - const language = lang ?? localeId ?? "en"; - localStorage.setItem("locale", language); - setLocalLang(language); - setCurrentLocale(findLocale()); - }, [findLocale, lang]); - - const { Provider } = localeContext; + const location = overwrittenLocalesData?.find((locale) => locale.id === lang); + if (location) { + setCurrentLocale(location); + dayjs.locale(location.dayjsTranslations); + } + }, [translations, lang]); return ( - + {children} - + ); }; @@ -60,15 +42,5 @@ const useLanguage = () => { return context.currentLocale.lang; }; -const useLocales = () => { - const context = useContext(localeContext); - return context.localesData; -}; - -const useSetLocale = () => { - const context = useContext(localeContext); - return context.setCurrentLocale; -}; - export default LocaleProvider; -export { useLanguage, useLocales, useSetLocale }; +export { useLanguage }; diff --git a/src/context/LocaleProvider/index.ts b/src/context/LocaleProvider/index.ts index 33a052a..a94fcf3 100644 --- a/src/context/LocaleProvider/index.ts +++ b/src/context/LocaleProvider/index.ts @@ -1 +1 @@ -export { default, useLanguage, useLocales, useSetLocale } from "./LocaleProvider"; +export { default, useLanguage } from "./LocaleProvider"; diff --git a/src/context/LocaleProvider/localeContext.tsx b/src/context/LocaleProvider/localeContext.tsx index b74720f..9627eb7 100644 --- a/src/context/LocaleProvider/localeContext.tsx +++ b/src/context/LocaleProvider/localeContext.tsx @@ -3,7 +3,5 @@ import { locales } from "./locales"; import { LocaleContextType } from "./types"; export const localeContext = createContext({ - localesData: locales.getLocales(), - currentLocale: locales.getLocales()[0], - setCurrentLocale: () => {} + currentLocale: locales.getLocales().filter((locale) => locale.id === "en")[0] }); diff --git a/src/context/LocaleProvider/types.ts b/src/context/LocaleProvider/types.ts index 7f78d59..ede5321 100644 --- a/src/context/LocaleProvider/types.ts +++ b/src/context/LocaleProvider/types.ts @@ -3,8 +3,6 @@ import { LangCodes } from "@/types/global"; export type LocaleContextType = { currentLocale: LocaleType; - localesData: LocaleType[]; - setCurrentLocale: (locale: LocaleType) => void; }; export type LocaleProviderProps = {