From 7f9cc3c1875e45b4d7b6a75bc6aa2ff00e2634ac Mon Sep 17 00:00:00 2001 From: Kasra Bigdeli Date: Thu, 10 Oct 2024 23:39:05 -0700 Subject: [PATCH] Added language selector to logged in state --- src/App.tsx | 4 +- src/containers/Login.tsx | 26 ++------- src/containers/PageRoot.tsx | 67 ++++++++++++---------- src/containers/global/LanguageSelector.tsx | 55 ++++++++++++++++++ src/utils/Language.ts | 28 +++++++-- 5 files changed, 123 insertions(+), 57 deletions(-) create mode 100644 src/containers/global/LanguageSelector.tsx diff --git a/src/App.tsx b/src/App.tsx index 405577bc..d7a9f5ed 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -10,7 +10,7 @@ import DarkModeContext from './contexts/DarkModeContext' import reducers from './redux/reducers' import './styles/style.css' import CrashReporter from './utils/CrashReporter' -import { currentLanguageOption } from './utils/Language' +import { getCurrentLanguageOption } from './utils/Language' import StorageHelper from './utils/StorageHelper' CrashReporter.getInstance().init() @@ -37,6 +37,8 @@ function App() { StorageHelper.getDarkModeFromLocalStorage() ) + const currentLanguageOption = getCurrentLanguageOption() + return ( , any> { .catch(Toaster.createCatcher()) } - render(): ReactComponentElement { + render() { const self = this if (ApiManager.isLoggedIn()) return @@ -83,17 +79,7 @@ export default class Login extends ApiComponent, any> { style={{ width: 450 }} extra={ isLanguageEnabled ? ( - { + self.handleChange(v) + }} + /> + ) + } +} + +export default connect( + undefined, + { + emitRootKeyChanged: emitRootKeyChanged, + }, + null, + { forwardRef: true } +)(LanguageSelector) diff --git a/src/utils/Language.ts b/src/utils/Language.ts index 9a3f5654..3c9a273a 100644 --- a/src/utils/Language.ts +++ b/src/utils/Language.ts @@ -109,18 +109,34 @@ const languagesOptions: LanguageOption[] = [ const defaultLanguageOptions = languagesOptions[0] -const currentLanguage = StorageHelper.getLanguageFromLocalStorage() +const savedLanguageInBrowser = StorageHelper.getLanguageFromLocalStorage() -const currentLanguageOption: LanguageOption = - languagesOptions.find((o) => { - return o.value === currentLanguage || o.alias?.includes(currentLanguage) - }) || defaultLanguageOptions +function findLanguageOption(language: string): LanguageOption { + return ( + languagesOptions.find((o) => { + return o.value === language || o.alias?.includes(language) + }) || defaultLanguageOptions + ) +} + +let currentLanguageOption = findLanguageOption(savedLanguageInBrowser) export function localize(key: string, message: string) { return currentLanguageOption.messages[key] || message } -export { currentLanguageOption, languagesOptions } +export function getCurrentLanguageOption() { + return currentLanguageOption +} + +export function setCurrentLanguageOption(languageAcronym: string) { + currentLanguageOption = findLanguageOption(languageAcronym) + StorageHelper.setLanguageInLocalStorage(currentLanguageOption.value) +} + +setCurrentLanguageOption(savedLanguageInBrowser) + +export { languagesOptions } // Currently only enable language for dev mode or demo, until the vast majority of the content is translated export const isLanguageEnabled = true