-
Notifications
You must be signed in to change notification settings - Fork 175
feat: add language selector component to header #586
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
Open
Ang-m4
wants to merge
5
commits into
openedx:master
Choose a base branch
from
eduNEXT:afg/language-selector-imp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+557
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7587f09
feat: add language selector component to header
Ang-m4 263910e
fix: update language selector to maintain state after switch
Ang-m4 4c639c3
style: add trailing newline
Ang-m4 8b9142e
refactor: simplify imports and use getSupportedLocaleList
Ang-m4 c46b248
feat: add language selector to Desktop header
Ang-m4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,101 @@ | ||
| import PropTypes from 'prop-types'; | ||
| import React, { useState } from 'react'; | ||
|
|
||
| import { | ||
| changeUserSessionLanguage, | ||
| getSupportedLocaleList, | ||
| getPrimaryLanguageSubtag, | ||
| injectIntl, | ||
| } from '@edx/frontend-platform/i18n'; | ||
| import { getLocale } from '@edx/frontend-platform/i18n/lib'; | ||
| import { Dropdown } from '@openedx/paragon'; | ||
| import { Language } from '@openedx/paragon/icons'; | ||
|
|
||
| /** | ||
| * Gets the localized display name of a language in its own language. | ||
| * | ||
| * @function getDisplayName | ||
| * @param {string} locale - The locale code (e.g., 'en', 'es', 'ar') | ||
| * @returns {string} The capitalized display name of the language in its native form | ||
| * @example | ||
| */ | ||
| const getDisplayName = (locale) => { | ||
| const langName = new Intl.DisplayNames([locale], { type: 'language', languageDisplay: 'standard' }).of(locale); | ||
| return langName.charAt(0).toUpperCase() + langName.slice(1); | ||
| }; | ||
|
|
||
| /** | ||
| * Language Selector component that displays a dropdown allowing users to change the site language. | ||
| * | ||
| * The component is responsive and adapts to different screen sizes: | ||
| * - On large screens: Shows the full language name (e.g., "English") | ||
| * - On medium screens: Shows the language code (e.g., "EN") | ||
| * - On small screens: Shows only the language icon | ||
| * | ||
| * @component | ||
| * @param {Object} props - Component props | ||
| * @param {string} [props.className=''] - Additional CSS class names to apply to the component | ||
| * @returns {React.Element|null} The rendered component or null if disabled or no supported languages | ||
| * | ||
| * @requires config.SITE_SUPPORTED_LANGUAGES - Must be a non-empty array of locale codes | ||
| * @requires config.LANGUAGE_PREFERENCE_COOKIE_NAME - Cookie name for storing language preference | ||
| */ | ||
| const LanguageSelector = ({ className }) => { | ||
| const languageOptions = getSupportedLocaleList(); | ||
| const [currentLocale, setCurrentLocale] = useState(getLocale()); | ||
|
|
||
| /** | ||
| * Handles the selection of a language from the dropdown. | ||
| * Only triggers language change if the selected language is different from the current one. | ||
| * | ||
| * @param {string} selectedLocale - The locale code selected by the user | ||
| */ | ||
| const handleSelect = (selectedLocale) => { | ||
| if (currentLocale !== selectedLocale) { | ||
| changeUserSessionLanguage(selectedLocale); | ||
| setCurrentLocale(selectedLocale); | ||
| } | ||
| }; | ||
|
|
||
| const currentLangCode = getPrimaryLanguageSubtag(currentLocale).toUpperCase(); | ||
| const currentlangDisplayName = getDisplayName(currentLocale); | ||
|
|
||
| // Don't render the component if there are no language options | ||
| if (!Array.isArray(languageOptions) | ||
| || languageOptions.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <div className={`${className} language-selector`} id="language-selector"> | ||
| <Dropdown onSelect={handleSelect}> | ||
| <Dropdown.Toggle | ||
| id="lang-selector-dropdown" | ||
| iconBefore={Language} | ||
| variant="outline-primary" | ||
| size="sm" | ||
| > | ||
| <span className="lang-label-medium">{currentLangCode}</span> | ||
| <span className="lang-label-large">{currentlangDisplayName}</span> | ||
| </Dropdown.Toggle> | ||
| <Dropdown.Menu> | ||
| {languageOptions.map((locale) => ( | ||
| <Dropdown.Item key={`lang-selector-${locale}`} eventKey={locale}> | ||
| {getDisplayName(locale)} | ||
| </Dropdown.Item> | ||
| ))} | ||
| </Dropdown.Menu> | ||
| </Dropdown> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| LanguageSelector.propTypes = { | ||
| className: PropTypes.string, | ||
| }; | ||
|
|
||
| LanguageSelector.defaultProps = { | ||
| className: '', | ||
| }; | ||
|
|
||
| export default injectIntl(LanguageSelector); | ||
This file contains hidden or 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,22 @@ | ||
| .language-selector { | ||
| padding: .75rem; | ||
|
|
||
| .dropdown-toggle { | ||
| .lang-label-medium, | ||
| .lang-label-large { | ||
| display: none; | ||
| } | ||
|
|
||
| @media (min-width: 576px) and (max-width: 767px) { | ||
| .lang-label-medium { | ||
| display: inline; | ||
| } | ||
| } | ||
|
|
||
| @media (min-width: 768px) { | ||
| .lang-label-large { | ||
| display: inline; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,120 @@ | ||
| import React from 'react'; | ||
| import { mergeConfig } from '@edx/frontend-platform'; | ||
| import { getLocale } from '@edx/frontend-platform/i18n/lib'; | ||
| import { changeUserSessionLanguage, getSupportedLocaleList } from '@edx/frontend-platform/i18n'; | ||
| import { | ||
| act, fireEvent, initializeMockApp, render, screen, | ||
| } from '../setupTest'; | ||
| import LanguageSelector from './LanguageSelector'; | ||
|
|
||
| jest.mock('@edx/frontend-platform/i18n', () => ({ | ||
| ...jest.requireActual('@edx/frontend-platform/i18n'), | ||
| changeUserSessionLanguage: jest.fn().mockResolvedValue({}), | ||
| getSupportedLocaleList: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('@edx/frontend-platform/i18n/lib', () => ({ | ||
| ...jest.requireActual('@edx/frontend-platform/i18n/lib'), | ||
| getLocale: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('@openedx/paragon/icons', () => ({ | ||
| Language: () => <div>LanguageIcon</div>, | ||
| })); | ||
|
|
||
| jest.mock('@openedx/paragon', () => ({ | ||
| ...jest.requireActual('@openedx/paragon'), | ||
| useWindowSize: () => ({ width: global.innerWidth }), | ||
| })); | ||
|
|
||
| const LANGUAGE_PREFERENCE_COOKIE_NAME = 'language-preference'; | ||
|
|
||
| describe('LanguageSelector', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
|
|
||
| mergeConfig({ | ||
| ENABLE_HEADER_LANG_SELECTOR: true, | ||
| LANGUAGE_PREFERENCE_COOKIE_NAME, | ||
| }); | ||
| getSupportedLocaleList.mockReturnValue(['es', 'en']); | ||
| initializeMockApp(); | ||
|
|
||
| global.innerWidth = 1200; | ||
| }); | ||
|
|
||
| it('should not render when no supported languages are available', () => { | ||
| getSupportedLocaleList.mockReturnValue([]); | ||
|
|
||
| const { container } = render(<LanguageSelector />); | ||
| // expect(container).toMatchSnapshot('no-supported-languages'); | ||
| expect(container.querySelector('#language-selector')).toBeNull(); | ||
| }); | ||
|
|
||
| it('should change the language when different language is selected', async () => { | ||
| getLocale.mockReturnValue('en'); | ||
|
|
||
| const { container } = render(<LanguageSelector />); | ||
| expect(container).toMatchSnapshot('before-language-change'); | ||
|
|
||
| const langDropdown = screen.getByRole('button', { id: 'lang-selector-dropdown' }); | ||
| fireEvent.click(langDropdown); | ||
|
|
||
| const spanishOption = screen.getByRole('button', { name: 'Español' }); | ||
|
|
||
| await act(async () => { | ||
| fireEvent.click(spanishOption); | ||
| }); | ||
|
|
||
| expect(container).toMatchSnapshot('after-language-change'); | ||
| expect(changeUserSessionLanguage).toHaveBeenCalledWith('es'); | ||
| }); | ||
|
|
||
| it('should not change language if the same language is selected', async () => { | ||
| getLocale.mockReturnValue('en'); | ||
|
|
||
| const { container } = render(<LanguageSelector />); | ||
| expect(container).toMatchSnapshot('before-same-language-selection'); | ||
|
|
||
| const langDropdown = screen.getByRole('button', { id: 'lang-selector-dropdown' }); | ||
| fireEvent.click(langDropdown); | ||
|
|
||
| const englishOption = screen.getByRole('button', { name: 'English' }); | ||
| await act(async () => { | ||
| fireEvent.click(englishOption); | ||
| }); | ||
|
|
||
| expect(container).toMatchSnapshot('after-same-language-selection'); | ||
| expect(changeUserSessionLanguage).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should display full language name on large screens', () => { | ||
| getLocale.mockReturnValue('en'); | ||
|
|
||
| global.innerWidth = 1200; | ||
| render(<LanguageSelector />); | ||
|
|
||
| const button = screen.getByRole('button', { id: 'lang-selector-dropdown' }); | ||
| expect(button).toMatchSnapshot('large-screen-button'); | ||
| }); | ||
|
|
||
| it('should display language code on medium screens', () => { | ||
| getLocale.mockReturnValue('en'); | ||
|
|
||
| global.innerWidth = 700; | ||
| render(<LanguageSelector />); | ||
|
|
||
| const button = screen.getByRole('button', { id: 'lang-selector-dropdown' }); | ||
| expect(button).toMatchSnapshot('medium-screen-button'); | ||
| }); | ||
|
|
||
| it('should display only icon on small screens', () => { | ||
| getLocale.mockReturnValue('en'); | ||
|
|
||
| global.innerWidth = 500; | ||
| render(<LanguageSelector />); | ||
|
|
||
| const button = screen.getByRole('button', { id: 'lang-selector-dropdown' }); | ||
| expect(button).toMatchSnapshot('small-screen-button'); | ||
| }); | ||
| }); |
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.
I don't see SITE_SUPPORTED_LANGUAGES implemented anywhere. Did I miss something?
See my other comment on the frontend-platform PR.