Skip to content

Commit 7f9cc3c

Browse files
committed
Added language selector to logged in state
1 parent aa9dc8b commit 7f9cc3c

File tree

5 files changed

+123
-57
lines changed

5 files changed

+123
-57
lines changed

src/App.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import DarkModeContext from './contexts/DarkModeContext'
1010
import reducers from './redux/reducers'
1111
import './styles/style.css'
1212
import CrashReporter from './utils/CrashReporter'
13-
import { currentLanguageOption } from './utils/Language'
13+
import { getCurrentLanguageOption } from './utils/Language'
1414
import StorageHelper from './utils/StorageHelper'
1515

1616
CrashReporter.getInstance().init()
@@ -37,6 +37,8 @@ function App() {
3737
StorageHelper.getDarkModeFromLocalStorage()
3838
)
3939

40+
const currentLanguageOption = getCurrentLanguageOption()
41+
4042
return (
4143
<ConfigProvider
4244
direction={currentLanguageOption.rtl ? 'rtl' : 'ltr'}

src/containers/Login.tsx

+6-20
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import { LockOutlined } from '@ant-design/icons'
2-
import { Button, Card, Collapse, Input, Layout, Radio, Row, Select } from 'antd'
3-
import React, { ReactComponentElement } from 'react'
2+
import { Button, Card, Collapse, Input, Layout, Radio, Row } from 'antd'
3+
import React from 'react'
44
import { Redirect, RouteComponentProps } from 'react-router'
55
import ApiManager from '../api/ApiManager'
66
import ErrorFactory from '../utils/ErrorFactory'
7-
import {
8-
currentLanguageOption,
9-
isLanguageEnabled,
10-
languagesOptions,
11-
localize,
12-
} from '../utils/Language'
7+
import { isLanguageEnabled, localize } from '../utils/Language'
138
import StorageHelper from '../utils/StorageHelper'
149
import Toaster from '../utils/Toaster'
1510
import Utils from '../utils/Utils'
1611
import ApiComponent from './global/ApiComponent'
12+
import LanguageSelector from './global/LanguageSelector'
1713

1814
const NO_SESSION = 1
1915
const SESSION_STORAGE = 2
@@ -67,7 +63,7 @@ export default class Login extends ApiComponent<RouteComponentProps<any>, any> {
6763
.catch(Toaster.createCatcher())
6864
}
6965

70-
render(): ReactComponentElement<any> {
66+
render() {
7167
const self = this
7268

7369
if (ApiManager.isLoggedIn()) return <Redirect to="/" />
@@ -83,17 +79,7 @@ export default class Login extends ApiComponent<RouteComponentProps<any>, any> {
8379
style={{ width: 450 }}
8480
extra={
8581
isLanguageEnabled ? (
86-
<Select
87-
style={{ width: 150 }}
88-
options={languagesOptions}
89-
value={currentLanguageOption.value}
90-
onChange={(value) => {
91-
StorageHelper.setLanguageInLocalStorage(
92-
value
93-
)
94-
window.location.reload()
95-
}}
96-
/>
82+
<LanguageSelector forceReload={true} />
9783
) : undefined
9884
}
9985
>

src/containers/PageRoot.tsx

+37-30
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,14 @@ import OneClickAppConfigPage from './apps/oneclick/variables/OneClickAppConfigPa
3030
import ApiComponent from './global/ApiComponent'
3131
import ClickableLink from './global/ClickableLink'
3232
import DarkModeSwitch from './global/DarkModeSwitch'
33+
import LanguageSelector from './global/LanguageSelector'
3334
import NewTabLink from './global/NewTabLink'
3435
import Monitoring from './monitoring/Monitoring'
3536
import Cluster from './nodes/Cluster'
3637
import Settings from './settings/Settings'
3738

3839
const { Header, Content, Sider } = Layout
3940

40-
const MENU_ITEMS: MenuProps['items'] = [
41-
{
42-
key: 'dashboard',
43-
label: localize('menu_item.dashboard', 'Dashboard'),
44-
icon: <LaptopOutlined />,
45-
},
46-
{
47-
key: 'apps',
48-
label: localize('menu_item.app', 'Apps'),
49-
icon: <CodeOutlined />,
50-
},
51-
{
52-
key: 'monitoring',
53-
label: localize('menu_item.monitoring', 'Monitoring'),
54-
icon: <DashboardOutlined />,
55-
},
56-
{
57-
key: 'cluster',
58-
label: localize('menu_item.cluster', 'Cluster'),
59-
icon: <ClusterOutlined />,
60-
},
61-
{
62-
key: 'settings',
63-
label: localize('menu_item.settings', 'Settings'),
64-
icon: <SettingOutlined />,
65-
},
66-
]
67-
6841
interface RootPageInterface extends RouteComponentProps<any> {
6942
rootElementKey: string
7043
emitSizeChanged: () => void
@@ -184,8 +157,36 @@ class PageRoot extends ApiComponent<
184157

185158
render() {
186159
const self = this
160+
const MENU_ITEMS: MenuProps['items'] = [
161+
{
162+
key: 'dashboard',
163+
label: localize('menu_item.dashboard', 'Dashboard'),
164+
icon: <LaptopOutlined />,
165+
},
166+
{
167+
key: 'apps',
168+
label: localize('menu_item.app', 'Apps'),
169+
icon: <CodeOutlined />,
170+
},
171+
{
172+
key: 'monitoring',
173+
label: localize('menu_item.monitoring', 'Monitoring'),
174+
icon: <DashboardOutlined />,
175+
},
176+
{
177+
key: 'cluster',
178+
label: localize('menu_item.cluster', 'Cluster'),
179+
icon: <ClusterOutlined />,
180+
},
181+
{
182+
key: 'settings',
183+
label: localize('menu_item.settings', 'Settings'),
184+
icon: <SettingOutlined />,
185+
},
186+
]
187+
187188
return (
188-
<Layout className="full-screen">
189+
<Layout className="full-screen" key={self.props.rootElementKey}>
189190
<Header
190191
className="header"
191192
style={{
@@ -252,6 +253,13 @@ class PageRoot extends ApiComponent<
252253
>
253254
<DarkModeSwitch />
254255
</span>
256+
<span
257+
style={{
258+
marginInlineEnd: 50,
259+
}}
260+
>
261+
<LanguageSelector />
262+
</span>
255263
<span>
256264
<Button
257265
type="primary"
@@ -362,7 +370,6 @@ class PageRoot extends ApiComponent<
362370
</Sider>
363371
<Content>
364372
<div
365-
key={self.props.rootElementKey}
366373
ref={self.mainContainer}
367374
style={{
368375
paddingTop: 12,
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { Select } from 'antd'
2+
import { Component } from 'react'
3+
import { connect } from 'react-redux'
4+
import { emitRootKeyChanged } from '../../redux/actions/GlobalActions'
5+
import {
6+
getCurrentLanguageOption,
7+
languagesOptions,
8+
setCurrentLanguageOption,
9+
} from '../../utils/Language'
10+
11+
class LanguageSelector extends Component<
12+
{ forceReload?: boolean; emitRootKeyChanged: Function },
13+
{ currentLanguage: string }
14+
> {
15+
constructor(props: any) {
16+
super(props)
17+
this.state = {
18+
currentLanguage: getCurrentLanguageOption().value,
19+
}
20+
}
21+
22+
handleChange(value: string) {
23+
const self = this
24+
25+
setCurrentLanguageOption(value)
26+
self.setState({ currentLanguage: value })
27+
self.props.emitRootKeyChanged()
28+
if (self.props.forceReload) {
29+
window.location.reload()
30+
}
31+
}
32+
33+
render() {
34+
const self = this
35+
return (
36+
<Select
37+
style={{ width: 150 }}
38+
options={languagesOptions}
39+
value={self.state.currentLanguage}
40+
onChange={(v) => {
41+
self.handleChange(v)
42+
}}
43+
/>
44+
)
45+
}
46+
}
47+
48+
export default connect(
49+
undefined,
50+
{
51+
emitRootKeyChanged: emitRootKeyChanged,
52+
},
53+
null,
54+
{ forwardRef: true }
55+
)(LanguageSelector)

src/utils/Language.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,34 @@ const languagesOptions: LanguageOption[] = [
109109

110110
const defaultLanguageOptions = languagesOptions[0]
111111

112-
const currentLanguage = StorageHelper.getLanguageFromLocalStorage()
112+
const savedLanguageInBrowser = StorageHelper.getLanguageFromLocalStorage()
113113

114-
const currentLanguageOption: LanguageOption =
115-
languagesOptions.find((o) => {
116-
return o.value === currentLanguage || o.alias?.includes(currentLanguage)
117-
}) || defaultLanguageOptions
114+
function findLanguageOption(language: string): LanguageOption {
115+
return (
116+
languagesOptions.find((o) => {
117+
return o.value === language || o.alias?.includes(language)
118+
}) || defaultLanguageOptions
119+
)
120+
}
121+
122+
let currentLanguageOption = findLanguageOption(savedLanguageInBrowser)
118123

119124
export function localize(key: string, message: string) {
120125
return currentLanguageOption.messages[key] || message
121126
}
122127

123-
export { currentLanguageOption, languagesOptions }
128+
export function getCurrentLanguageOption() {
129+
return currentLanguageOption
130+
}
131+
132+
export function setCurrentLanguageOption(languageAcronym: string) {
133+
currentLanguageOption = findLanguageOption(languageAcronym)
134+
StorageHelper.setLanguageInLocalStorage(currentLanguageOption.value)
135+
}
136+
137+
setCurrentLanguageOption(savedLanguageInBrowser)
138+
139+
export { languagesOptions }
124140

125141
// Currently only enable language for dev mode or demo, until the vast majority of the content is translated
126142
export const isLanguageEnabled = true

0 commit comments

Comments
 (0)