Skip to content

Commit fc0e1be

Browse files
language url seems to be defeated finally =)
1 parent 87f7de7 commit fc0e1be

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed

src/client/App.tsx

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,26 +78,52 @@ const LanguageContext = createContext({});
7878

7979
export function LanguageProvider({ children }) {
8080
const location = useLocation();
81+
const query = useQuery()
8182
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()
8386

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)
87122
const searchParams = new URLSearchParams(location.search);
88123
searchParams.set('lang', newLang);
89124
navigate(`${location.pathname}?${searchParams.toString()}`, { replace: true });
90125
};
91126

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-
101127
return (
102128
<LanguageContext.Provider value={{ lang, setLang }}>
103129
{children}
@@ -114,28 +140,12 @@ const App = () => {
114140
const location = useLocation()
115141
const query = useQuery()
116142
const langParam = query.get('lang')
117-
const { t, i18n } = useTranslation()
118-
const { language } = i18n
119-
const languages = ['fi', 'sv', 'en']
120143
const { user, isLoading } = useCurrentUser()
121144

122145
useEffect(() => {
123146
initShibbolethPinger()
124147
}, [])
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')
139149

140150
if (isLoading && !onNoAccessPage) return null
141151

0 commit comments

Comments
 (0)