New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug 1998347: fix user preference for language and sync with local storage #9902
Merged
openshift-merge-robot
merged 1 commit into
openshift:master
from
nemesis09:fix-user-preference-language
Sep 2, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import { | |
} from '@console/shared'; | ||
import { useTelemetry } from '@console/shared/src/hooks/useTelemetry'; | ||
import { useUserSettings } from '@console/shared/src/hooks/useUserSettings'; | ||
import { getLastLanguage } from '../../user-preferences/language/getLastLanguage'; | ||
|
||
export { QuickStartContext }; | ||
export { QuickStartContextProvider }; | ||
|
@@ -26,7 +27,7 @@ export const getProcessedResourceBundle = (resourceBundle, lng) => { | |
const params = new URLSearchParams(window.location.search); | ||
const pseudolocalizationEnabled = params.get('pseudolocalization') === 'true'; | ||
|
||
const language = lng || localStorage.getItem('bridge/language') || 'en'; | ||
const language = lng || getLastLanguage() || 'en'; | ||
let consoleBundle = resourceBundle; | ||
if (pseudolocalizationEnabled && language === 'en') { | ||
consoleBundle = {}; | ||
|
@@ -174,7 +175,7 @@ export const useValuesForQuickStartContext = (): QuickStartContextValues => { | |
[activeQuickStartID, setAllQuickStartStates, fireTelemetryEvent], | ||
); | ||
|
||
const language = localStorage.getItem('bridge/language') || 'en'; | ||
const language = getLastLanguage() || 'en'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not new, but There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
const resourceBundle = i18n.getResourceBundle(language, 'console-app'); | ||
const processedResourceBundle = getProcessedResourceBundle(resourceBundle, language); | ||
|
||
|
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-language-dropdown__system-default-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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did we lose the query parameter detection for language?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
i18next-browser-languageDetector
handles that. The old language preference modal didn't seem to detect language from query params as well.The
Detect Language
just populates the local storage on first load if there is a preferred language in configmap but the same does not reflect in local storage.This will be useful if the user changes browser after setting a language preference.