|
| 1 | +/** |
| 2 | + * LanguageManager.js |
| 3 | + * |
| 4 | + * Provides utility functions for updating the session language preferences for users. |
| 5 | + */ |
| 6 | + |
| 7 | +import { getConfig } from '../config'; |
| 8 | +import { getAuthenticatedHttpClient, getAuthenticatedUser } from '../auth'; |
| 9 | +import { convertKeyNames, snakeCaseObject } from '../utils'; |
| 10 | +import { getCookies, handleRtl, LOCALE_CHANGED } from './lib'; |
| 11 | +import { logError } from '../logging'; |
| 12 | +import { publish } from '../pubSub'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Updates user language preferences via the preferences API. |
| 16 | + * |
| 17 | + * This function converts preference data to snake_case and formats specific keys |
| 18 | + * according to backend requirements before sending the PATCH request. |
| 19 | + * |
| 20 | + * @param {string} username - The username of the user whose preferences to update. |
| 21 | + * @param {Object} preferenceData - The preference parameters to update (e.g., { pref_lang: 'en' }). |
| 22 | + * @returns {Promise} - A promise that resolves when the API call completes successfully, |
| 23 | + * or rejects if there's an error with the request. |
| 24 | + */ |
| 25 | +export async function updateUserPreferences(username, preferenceData) { |
| 26 | + let formattedData = snakeCaseObject(preferenceData); |
| 27 | + formattedData = convertKeyNames(formattedData, { |
| 28 | + pref_lang: 'pref-lang', |
| 29 | + }); |
| 30 | + |
| 31 | + await getAuthenticatedHttpClient().patch( |
| 32 | + `${getConfig().LMS_BASE_URL}/api/user/v1/preferences/${username}`, |
| 33 | + formattedData, |
| 34 | + { headers: { 'Content-Type': 'application/merge-patch+json' } }, |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Sets the language for the current session using the setlang endpoint. |
| 40 | + * |
| 41 | + * This function sends a POST request to the LMS setlang endpoint to change |
| 42 | + * the language for the current user session. |
| 43 | + * |
| 44 | + * @param {string} languageCode - The language code to set (e.g., 'en', 'es', 'ar'). |
| 45 | + * Should be a valid ISO language code supported by the platform. |
| 46 | + * @returns {Promise} - A promise that resolves when the API call completes successfully, |
| 47 | + * or rejects if there's an error with the request. |
| 48 | + */ |
| 49 | +export async function setSessionLanguage(languageCode) { |
| 50 | + const formData = new FormData(); |
| 51 | + const requestConfig = { |
| 52 | + headers: { |
| 53 | + Accept: 'application/json', |
| 54 | + 'X-Requested-With': 'XMLHttpRequest', |
| 55 | + }, |
| 56 | + }; |
| 57 | + const url = `${getConfig().LMS_BASE_URL}/i18n/setlang/`; |
| 58 | + formData.append('language', languageCode); |
| 59 | + |
| 60 | + await getAuthenticatedHttpClient().post(url, formData, requestConfig); |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Changes the user's language preference and applies it to the current session. |
| 65 | + * |
| 66 | + * This comprehensive function handles the complete language change process: |
| 67 | + * 1. Sets the language cookie with the selected language code |
| 68 | + * 2. If a user is authenticated, updates their server-side preference in the backend |
| 69 | + * 3. Updates the session language through the setlang endpoint |
| 70 | + * 4. Reloads the page to apply the language changes across the interface |
| 71 | + * |
| 72 | + * @param {string} languageCode - The selected language locale code (e.g., 'en', 'es', 'ar'). |
| 73 | + * Should be a valid ISO language code supported by the platform. |
| 74 | + * @returns {Promise} - A promise that resolves when all operations complete (before page reload). |
| 75 | + * Note: After completion, the page will reload automatically. |
| 76 | + */ |
| 77 | +export async function changeUserSessionLanguage(languageCode) { |
| 78 | + const cookies = getCookies(); |
| 79 | + const authenticatedUser = getAuthenticatedUser(); |
| 80 | + |
| 81 | + const languageCookieName = getConfig().LANGUAGE_PREFERENCE_COOKIE_NAME; |
| 82 | + |
| 83 | + cookies.set(languageCookieName, languageCode); |
| 84 | + |
| 85 | + try { |
| 86 | + if (authenticatedUser) { |
| 87 | + await updateUserPreferences(authenticatedUser.username, { |
| 88 | + pref_lang: languageCode, |
| 89 | + }); |
| 90 | + } |
| 91 | + await setSessionLanguage(languageCode); |
| 92 | + } catch (error) { |
| 93 | + logError(error); |
| 94 | + } |
| 95 | + publish(LOCALE_CHANGED, languageCode); |
| 96 | + handleRtl(languageCode); |
| 97 | +} |
0 commit comments