Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix user preference for language and sync with local storage
- Loading branch information
Showing
15 changed files
with
197 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
frontend/packages/console-app/src/components/detect-language/DetectLanguage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import * as React from 'react'; | ||
import { usePreferredLanguage, useLanguage } from '../user-preferences/language'; | ||
|
||
const DetectLanguage: React.MemoExoticComponent<() => any> = React.memo(() => { | ||
const [preferredLanguage, , preferredLanguageLoaded] = usePreferredLanguage(); | ||
useLanguage(preferredLanguage, preferredLanguageLoaded); | ||
return null; | ||
}); | ||
|
||
export default DetectLanguage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
frontend/packages/console-app/src/components/user-preferences/language/LanguageDropdown.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.co-user-preference-language-checkbox { | ||
margin-bottom: var(--pf-global--spacer--sm) !important; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
frontend/packages/console-app/src/components/user-preferences/language/getLastLanguage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { LAST_LANGUAGE_LOCAL_STORAGE_KEY } from './const'; | ||
|
||
export const getLastLanguage = (): string => localStorage.getItem(LAST_LANGUAGE_LOCAL_STORAGE_KEY); |
2 changes: 2 additions & 0 deletions
2
frontend/packages/console-app/src/components/user-preferences/language/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export { default as LanguageDropdown } from './LanguageDropdown'; | ||
export * from './usePreferredLanguage'; | ||
export * from './useLanguage'; |
38 changes: 38 additions & 0 deletions
38
frontend/packages/console-app/src/components/user-preferences/language/useLanguage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import * as React from 'react'; | ||
import { QuickStartContext, QuickStartContextValues } from '@patternfly/quickstarts'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { getProcessedResourceBundle } from '../../quick-starts/utils/quick-start-context'; | ||
import { LAST_LANGUAGE_LOCAL_STORAGE_KEY } from './const'; | ||
import { getLastLanguage } from './getLastLanguage'; | ||
|
||
export const useLanguage = (preferredLanguage: string, preferredLanguageLoaded: boolean) => { | ||
const { i18n } = useTranslation(); | ||
const { setResourceBundle } = React.useContext<QuickStartContextValues>(QuickStartContext); | ||
|
||
React.useEffect(() => { | ||
const onLanguageChange = (lng: string) => { | ||
if (setResourceBundle) { | ||
// Update language resource of quick starts components | ||
const resourceBundle = i18n.getResourceBundle(lng, 'console-app'); | ||
const processedBundle = getProcessedResourceBundle(resourceBundle, lng); | ||
setResourceBundle(processedBundle, lng); | ||
} | ||
}; | ||
const preferredLanguageInStorage: string = getLastLanguage(); | ||
|
||
i18n.on('languageChanged', onLanguageChange); | ||
|
||
if (preferredLanguageLoaded && preferredLanguage !== preferredLanguageInStorage) { | ||
if (preferredLanguage) { | ||
localStorage.setItem(LAST_LANGUAGE_LOCAL_STORAGE_KEY, preferredLanguage); | ||
i18n.changeLanguage(preferredLanguage); | ||
} else { | ||
preferredLanguageInStorage && localStorage.removeItem(LAST_LANGUAGE_LOCAL_STORAGE_KEY); | ||
} | ||
} | ||
|
||
return () => { | ||
i18n.off('languageChanged', onLanguageChange); | ||
}; | ||
}, [i18n, preferredLanguage, preferredLanguageLoaded, setResourceBundle]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.