1- import { useCallback , useContext , useEffect , useState } from "react" ;
1+ import { useContext , useEffect , useState } from "react" ;
22import dayjs from "dayjs" ;
33import { localeContext } from "./localeContext" ;
44import { locales } from "./locales" ;
5- import { LocaleProviderProps , LocaleType } from "./types" ;
5+ import { LocaleProviderProps } from "./types" ;
66
77const 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-
7145export default LocaleProvider ;
72- export { useLanguage , useLocales , useSetLocale } ;
46+ export { useLanguage } ;
0 commit comments