Skip to content

Commit c3197db

Browse files
arek-bitnoiseMichal Gasiorek
authored andcommitted
refactor: Simplified i18n context and provider
1 parent 901328c commit c3197db

File tree

4 files changed

+27
-57
lines changed

4 files changed

+27
-57
lines changed
Lines changed: 25 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,39 @@
1-
import { useCallback, useContext, useEffect, useState } from "react";
1+
import { useContext, useEffect, useState } from "react";
22
import dayjs from "dayjs";
33
import { localeContext } from "./localeContext";
44
import { locales } from "./locales";
5-
import { LocaleProviderProps, LocaleType } from "./types";
5+
import { LocaleProviderProps } from "./types";
66

77
const LocaleProvider = ({ children, lang, translations }: LocaleProviderProps) => {
8-
const [localLang, setLocalLang] = useState<string>("en");
9-
const localesData = locales.getLocales();
10-
11-
const findLocale = useCallback(() => {
12-
const locale = localesData.find((l) => {
13-
return l.id === localLang;
14-
});
15-
16-
if (typeof locale?.dayjsTranslations === "object") {
17-
dayjs.locale(locale.dayjsTranslations);
18-
}
19-
20-
return locale || localesData[0];
21-
}, [localLang]);
22-
23-
const [currentLocale, setCurrentLocale] = useState<LocaleType>(findLocale());
24-
25-
const saveCurrentLocale = (locale: LocaleType) => {
26-
localStorage.setItem("locale", locale.translateCode);
27-
setCurrentLocale(locale);
28-
};
8+
const [currentLocale, setCurrentLocale] = useState(
9+
locales.getLocales().filter((locale) => locale.id === "en")[0]
10+
);
2911

3012
useEffect(() => {
31-
translations?.forEach((translation) => {
32-
const localeData = localesData.find((el) => el.id === translation.id);
33-
if (!localeData) {
34-
locales.addLocales(translation);
35-
}
13+
const overwrittenLocalesData = locales.locales.map((locale) => {
14+
let localeTemp = locale;
15+
translations?.forEach((translation) => {
16+
if (locale.id === translation.id) {
17+
localeTemp = translation;
18+
}
19+
});
20+
return localeTemp;
3621
});
37-
}, [translations]);
3822

39-
useEffect(() => {
40-
const localeId = localStorage.getItem("locale");
41-
const language = lang ?? localeId ?? "en";
42-
localStorage.setItem("locale", language);
43-
setLocalLang(language);
44-
setCurrentLocale(findLocale());
45-
}, [findLocale, lang]);
46-
47-
const { Provider } = localeContext;
23+
const location = overwrittenLocalesData?.find((locale) => locale.id === lang);
24+
if (location) {
25+
setCurrentLocale(location);
26+
dayjs.locale(location.dayjsTranslations);
27+
}
28+
}, [translations, lang]);
4829

4930
return (
50-
<Provider value={{ currentLocale, localesData, setCurrentLocale: saveCurrentLocale }}>
31+
<localeContext.Provider
32+
value={{
33+
currentLocale
34+
}}>
5135
{children}
52-
</Provider>
36+
</localeContext.Provider>
5337
);
5438
};
5539

@@ -58,15 +42,5 @@ const useLanguage = () => {
5842
return context.currentLocale.lang;
5943
};
6044

61-
const useLocales = () => {
62-
const context = useContext(localeContext);
63-
return context.localesData;
64-
};
65-
66-
const useSetLocale = () => {
67-
const context = useContext(localeContext);
68-
return context.setCurrentLocale;
69-
};
70-
7145
export default LocaleProvider;
72-
export { useLanguage, useLocales, useSetLocale };
46+
export { useLanguage };
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { default, useLanguage, useLocales, useSetLocale } from "./LocaleProvider";
1+
export { default, useLanguage } from "./LocaleProvider";

src/context/LocaleProvider/localeContext.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,5 @@ import { locales } from "./locales";
33
import { LocaleContextType } from "./types";
44

55
export const localeContext = createContext<LocaleContextType>({
6-
localesData: locales.getLocales(),
7-
currentLocale: locales.getLocales()[0],
8-
setCurrentLocale: () => {}
6+
currentLocale: locales.getLocales().filter((locale) => locale.id === "en")[0]
97
});

src/context/LocaleProvider/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { LangCodes } from "@/types/global";
33

44
export type LocaleContextType = {
55
currentLocale: LocaleType;
6-
localesData: LocaleType[];
7-
setCurrentLocale: (locale: LocaleType) => void;
86
};
97

108
export type LocaleProviderProps = {

0 commit comments

Comments
 (0)