Skip to content

Commit a17955e

Browse files
committed
feat(i18n): add language preference management functionality
1 parent e0656d1 commit a17955e

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/i18n/LanguageManager.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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., { prefLang: '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+
const snakeCaseData = snakeCaseObject(preferenceData);
27+
const formattedData = convertKeyNames(snakeCaseData, {
28+
pref_lang: 'pref-lang',
29+
});
30+
31+
return 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+
formData.append('language', languageCode);
52+
53+
const url = `${getConfig().LMS_BASE_URL}/i18n/setlang/`;
54+
return getAuthenticatedHttpClient().post(url, formData, {
55+
headers: {
56+
Accept: 'application/json',
57+
'X-Requested-With': 'XMLHttpRequest',
58+
},
59+
});
60+
}
61+
62+
/**
63+
* Changes the user's language preference and applies it to the current session.
64+
*
65+
* This comprehensive function handles the complete language change process:
66+
* 1. Sets the language cookie with the selected language code
67+
* 2. If a user is authenticated, updates their server-side preference in the backend
68+
* 3. Updates the session language through the setlang endpoint
69+
* 4. Publishes a locale change event to notify other parts of the application
70+
*
71+
* @param {string} languageCode - The selected language locale code (e.g., 'en', 'es', 'ar').
72+
* Should be a valid ISO language code supported by the platform.
73+
* @returns {Promise} - A promise that resolves when all operations complete.
74+
*
75+
*/
76+
export async function changeUserSessionLanguage(languageCode) {
77+
const cookies = getCookies();
78+
const cookieName = getConfig().LANGUAGE_PREFERENCE_COOKIE_NAME;
79+
cookies.set(cookieName, languageCode);
80+
81+
try {
82+
const user = getAuthenticatedUser();
83+
if (user) {
84+
await updateUserPreferences(user.username, { prefLang: languageCode });
85+
}
86+
87+
await setSessionLanguage(languageCode);
88+
handleRtl(languageCode);
89+
publish(LOCALE_CHANGED, languageCode);
90+
} catch (error) {
91+
logError(error);
92+
}
93+
}

src/i18n/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,7 @@ export {
122122
getLanguageList,
123123
getLanguageMessages,
124124
} from './languages';
125+
126+
export {
127+
changeUserSessionLanguage,
128+
} from './LanguageManager';

0 commit comments

Comments
 (0)