-
Notifications
You must be signed in to change notification settings - Fork 76
feat(i18n): add language preference management functionality #786
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/change-user-language-preference
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.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a17955e
feat(i18n): add language preference management functionality
Ang-m4 1c310d3
fix: force page reload to ensure complete translation application
Ang-m4 d83e228
feat: conditional page reloading and module rename
Ang-m4 a3c25d1
refactor(i18n): move language preference functions to languageApi module
Ang-m4 c2d269c
test(i18n): add unit tests for languageApi and languageManager functions
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* LanguageManager.js | ||
* | ||
* Provides utility functions for updating the session language preferences for users. | ||
*/ | ||
|
||
import { getConfig } from '../config'; | ||
import { getAuthenticatedHttpClient, getAuthenticatedUser } from '../auth'; | ||
import { convertKeyNames, snakeCaseObject } from '../utils'; | ||
import { getCookies, handleRtl, LOCALE_CHANGED } from './lib'; | ||
import { logError } from '../logging'; | ||
import { publish } from '../pubSub'; | ||
|
||
/** | ||
* Updates user language preferences via the preferences API. | ||
* | ||
* This function converts preference data to snake_case and formats specific keys | ||
* according to backend requirements before sending the PATCH request. | ||
* | ||
* @param {string} username - The username of the user whose preferences to update. | ||
* @param {Object} preferenceData - The preference parameters to update (e.g., { prefLang: 'en' }). | ||
* @returns {Promise} - A promise that resolves when the API call completes successfully, | ||
* or rejects if there's an error with the request. | ||
*/ | ||
export async function updateUserPreferences(username, preferenceData) { | ||
const snakeCaseData = snakeCaseObject(preferenceData); | ||
const formattedData = convertKeyNames(snakeCaseData, { | ||
pref_lang: 'pref-lang', | ||
}); | ||
|
||
return getAuthenticatedHttpClient().patch( | ||
`${getConfig().LMS_BASE_URL}/api/user/v1/preferences/${username}`, | ||
formattedData, | ||
{ headers: { 'Content-Type': 'application/merge-patch+json' } }, | ||
); | ||
} | ||
|
||
/** | ||
* Sets the language for the current session using the setlang endpoint. | ||
* | ||
* This function sends a POST request to the LMS setlang endpoint to change | ||
* the language for the current user session. | ||
* | ||
* @param {string} languageCode - The language code to set (e.g., 'en', 'es', 'ar'). | ||
* Should be a valid ISO language code supported by the platform. | ||
* @returns {Promise} - A promise that resolves when the API call completes successfully, | ||
* or rejects if there's an error with the request. | ||
*/ | ||
export async function setSessionLanguage(languageCode) { | ||
const formData = new FormData(); | ||
formData.append('language', languageCode); | ||
|
||
const url = `${getConfig().LMS_BASE_URL}/i18n/setlang/`; | ||
return getAuthenticatedHttpClient().post(url, formData, { | ||
headers: { | ||
Accept: 'application/json', | ||
'X-Requested-With': 'XMLHttpRequest', | ||
}, | ||
}); | ||
} | ||
|
||
/** | ||
* Changes the user's language preference and applies it to the current session. | ||
* | ||
* This comprehensive function handles the complete language change process: | ||
* 1. Sets the language cookie with the selected language code | ||
* 2. If a user is authenticated, updates their server-side preference in the backend | ||
* 3. Updates the session language through the setlang endpoint | ||
* 4. Publishes a locale change event to notify other parts of the application | ||
* | ||
* @param {string} languageCode - The selected language locale code (e.g., 'en', 'es', 'ar'). | ||
* Should be a valid ISO language code supported by the platform. | ||
* @returns {Promise} - A promise that resolves when all operations complete. | ||
* | ||
*/ | ||
export async function changeUserSessionLanguage(languageCode) { | ||
const cookies = getCookies(); | ||
const cookieName = getConfig().LANGUAGE_PREFERENCE_COOKIE_NAME; | ||
cookies.set(cookieName, languageCode); | ||
|
||
try { | ||
const user = getAuthenticatedUser(); | ||
if (user) { | ||
await updateUserPreferences(user.username, { prefLang: languageCode }); | ||
} | ||
|
||
await setSessionLanguage(languageCode); | ||
handleRtl(languageCode); | ||
publish(LOCALE_CHANGED, languageCode); | ||
} catch (error) { | ||
logError(error); | ||
} | ||
|
||
// Force page reload to ensure complete translation application. | ||
// While some translations update via the publish event, many sections | ||
// of the platform are not configured to receive these events or | ||
// handle translations dynamically, requiring a full reload for consistency. | ||
window.location.reload(); | ||
Ang-m4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.