@@ -78,26 +78,52 @@ const LanguageContext = createContext({});
78
78
79
79
export function LanguageProvider ( { children } ) {
80
80
const location = useLocation ( ) ;
81
+ const query = useQuery ( )
81
82
const navigate = useNavigate ( ) ;
82
- const [ lang , setLangState ] = useState ( 'fi' ) ; // default
83
+ const languages = [ 'fi' , 'sv' , 'en' ]
84
+ const { t, i18n } = useTranslation ( )
85
+ const { user, isLoading } = useCurrentUser ( )
83
86
84
- // Sync context state to URL
85
- const setLang = ( newLang ) => {
86
- setLangState ( newLang ) ;
87
+ const [ lang , setLanguageState ] = useState ( localStorage . getItem ( 'lang' ) )
88
+ const langParam = query . get ( 'lang' )
89
+ console . log ( 'language is: ' + lang )
90
+ useEffect ( ( ) => {
91
+ const updatedLangFromLocal = localStorage . getItem ( 'lang' )
92
+
93
+ //use users language as a default if there is no lang url
94
+ if ( ! langParam && ! updatedLangFromLocal && user && user . language && languages . includes ( user . language ) ) {
95
+ console . log ( "using default users language" )
96
+ setLang ( user . language )
97
+ }
98
+ // If there is a lang url, then update the lang state to match it
99
+ else if ( langParam ) {
100
+ console . log ( "lang parameter based update" )
101
+ setLang ( langParam )
102
+ }
103
+ else if ( ! langParam && updatedLangFromLocal )
104
+ {
105
+ console . log ( "using local storage language" )
106
+ //there is a case where if there are two redirects after another even the useState gets wiped
107
+ // so lets use the local storage (example: see how admin page)
108
+ setLang ( updatedLangFromLocal )
109
+ }
110
+ } , [ location . pathname ] )
111
+
112
+
113
+ // sets both the url and the local lang state to match the newlang if the newLang is supported
114
+ const setLang = ( newLang ) => {
115
+ if ( ! languages . includes ( newLang ) ) {
116
+ console . log ( "aborted lang update" )
117
+ return
118
+ }
119
+ localStorage . setItem ( 'lang' , newLang )
120
+ setLanguageState ( newLang )
121
+ i18n . changeLanguage ( newLang )
87
122
const searchParams = new URLSearchParams ( location . search ) ;
88
123
searchParams . set ( 'lang' , newLang ) ;
89
124
navigate ( `${ location . pathname } ?${ searchParams . toString ( ) } ` , { replace : true } ) ;
90
125
} ;
91
126
92
- // Read URL param on initial load
93
- useEffect ( ( ) => {
94
- const searchParams = new URLSearchParams ( location . search ) ;
95
- const urlLang = searchParams . get ( 'lang' ) ;
96
- if ( urlLang ) {
97
- setLangState ( urlLang ) ;
98
- }
99
- } , [ location . search ] ) ;
100
-
101
127
return (
102
128
< LanguageContext . Provider value = { { lang, setLang } } >
103
129
{ children }
@@ -114,28 +140,12 @@ const App = () => {
114
140
const location = useLocation ( )
115
141
const query = useQuery ( )
116
142
const langParam = query . get ( 'lang' )
117
- const { t, i18n } = useTranslation ( )
118
- const { language } = i18n
119
- const languages = [ 'fi' , 'sv' , 'en' ]
120
143
const { user, isLoading } = useCurrentUser ( )
121
144
122
145
useEffect ( ( ) => {
123
146
initShibbolethPinger ( )
124
147
} , [ ] )
125
- useEffect ( ( ) => {
126
- if ( ! langParam && user && user . language && languages . includes ( user . language ) ) {
127
- i18n . changeLanguage ( user . language )
128
-
129
- }
130
- } , [ user , i18n ] )
131
- useEffect ( ( ) => {
132
- if ( langParam && languages . includes ( langParam ) ) {
133
-
134
- i18n . changeLanguage ( langParam )
135
- }
136
-
137
- } , [ langParam ] )
138
- const onNoAccessPage = location . pathname . includes ( '/noaccess' )
148
+ const onNoAccessPage = location . pathname . includes ( '/noaccess' )
139
149
140
150
if ( isLoading && ! onNoAccessPage ) return null
141
151
0 commit comments