Skip to content

Commit efd50be

Browse files
committed
Language selector now works for changing the direction automatically
1 parent 31ed2d3 commit efd50be

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

src/App.tsx

+21-13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import thunk from 'redux-thunk'
77
import Login from './containers/Login'
88
import PageRoot from './containers/PageRoot'
99
import DarkModeContext from './contexts/DarkModeContext'
10+
import LanguageContext from './contexts/LanguageContext'
1011
import reducers from './redux/reducers'
1112
import './styles/style.css'
1213
import CrashReporter from './utils/CrashReporter'
@@ -36,12 +37,11 @@ function App() {
3637
const [isDarkMode, setIsDarkMode] = useState(
3738
StorageHelper.getDarkModeFromLocalStorage()
3839
)
39-
40-
const currentLanguageOption = getCurrentLanguageOption()
40+
const [currentLang, setCurrentLang] = useState(getCurrentLanguageOption())
4141

4242
return (
4343
<ConfigProvider
44-
direction={currentLanguageOption.rtl ? 'rtl' : 'ltr'}
44+
direction={currentLang.rtl ? 'rtl' : 'ltr'}
4545
theme={{
4646
algorithm: isDarkMode ? darkAlgorithm : defaultAlgorithm,
4747
token: {
@@ -52,21 +52,29 @@ function App() {
5252
'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'`,
5353
},
5454
}}
55-
locale={currentLanguageOption.antdLocale}
55+
locale={currentLang.antdLocale}
5656
>
57-
<DarkModeContext.Provider
57+
<LanguageContext.Provider
5858
value={{
59-
isDarkMode,
60-
setIsDarkMode: (value) => {
61-
setIsDarkMode(value)
62-
StorageHelper.setDarkModeInLocalStorage(value)
59+
setCurrentLanguageOptionContext: (value) => {
60+
setCurrentLang(value)
6361
},
6462
}}
6563
>
66-
<Provider store={store}>
67-
<MainComponent />
68-
</Provider>
69-
</DarkModeContext.Provider>
64+
<DarkModeContext.Provider
65+
value={{
66+
isDarkMode,
67+
setIsDarkMode: (value) => {
68+
setIsDarkMode(value)
69+
StorageHelper.setDarkModeInLocalStorage(value)
70+
},
71+
}}
72+
>
73+
<Provider store={store}>
74+
<MainComponent />
75+
</Provider>
76+
</DarkModeContext.Provider>
77+
</LanguageContext.Provider>
7078
</ConfigProvider>
7179
)
7280
}

src/containers/global/LanguageSelector.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Select } from 'antd'
2-
import { useEffect, useState } from 'react'
2+
import { useContext, useEffect, useState } from 'react'
33
import { useDispatch } from 'react-redux'
4+
import LanguageContext from '../../contexts/LanguageContext'
45
import { emitRootKeyChanged } from '../../redux/actions/GlobalActions'
56
import {
67
getCurrentLanguageOption,
@@ -16,10 +17,14 @@ const LanguageSelector: React.FC<LanguageSelectorProps> = ({ forceReload }) => {
1617
const [currentLanguage, setCurrentLanguage] = useState(
1718
getCurrentLanguageOption().value
1819
)
20+
21+
const { setCurrentLanguageOptionContext } = useContext(LanguageContext)
22+
1923
const dispatch = useDispatch()
2024

2125
const handleChange = (value: string) => {
22-
setCurrentLanguageOption(value)
26+
const langOption = setCurrentLanguageOption(value)
27+
setCurrentLanguageOptionContext(langOption)
2328
setCurrentLanguage(value)
2429
dispatch(emitRootKeyChanged())
2530
if (forceReload) {

src/contexts/LanguageContext.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createContext } from 'react'
2+
import { LanguageOption } from '../utils/Language'
3+
4+
const LanguageContext = createContext({
5+
setCurrentLanguageOptionContext: (value: LanguageOption) => {},
6+
})
7+
8+
export default LanguageContext

src/utils/Language.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,13 @@ export function getCurrentLanguageOption() {
129129
return currentLanguageOption
130130
}
131131

132-
export function setCurrentLanguageOption(languageAcronym: string) {
132+
export function setCurrentLanguageOption(
133+
languageAcronym: string
134+
): LanguageOption {
133135
currentLanguageOption = findLanguageOption(languageAcronym)
134136
StorageHelper.setLanguageInLocalStorage(currentLanguageOption.value)
137+
138+
return currentLanguageOption
135139
}
136140

137141
setCurrentLanguageOption(savedLanguageInBrowser)

0 commit comments

Comments
 (0)