Skip to content

Commit 2a3be05

Browse files
committed
refactor(i18n): move language preference functions to languageApi module
1 parent a5c70f5 commit 2a3be05

File tree

2 files changed

+57
-57
lines changed

2 files changed

+57
-57
lines changed

src/i18n/languageApi.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { getConfig } from '../config';
2+
import { getAuthenticatedHttpClient } from '../auth';
3+
import { convertKeyNames, snakeCaseObject } from '../utils';
4+
5+
/**
6+
* Updates user language preferences via the preferences API.
7+
*
8+
* This function converts preference data to snake_case and formats specific keys
9+
* according to backend requirements before sending the PATCH request.
10+
*
11+
* @param {string} username - The username of the user whose preferences to update.
12+
* @param {Object} preferenceData - The preference parameters to update (e.g., { prefLang: 'en' }).
13+
* @returns {Promise} - A promise that resolves when the API call completes successfully,
14+
* or rejects if there's an error with the request.
15+
*/
16+
export async function updateUserPreferences(username, preferenceData) {
17+
const snakeCaseData = snakeCaseObject(preferenceData);
18+
const formattedData = convertKeyNames(snakeCaseData, {
19+
pref_lang: 'pref-lang',
20+
});
21+
22+
return getAuthenticatedHttpClient().patch(
23+
`${getConfig().LMS_BASE_URL}/api/user/v1/preferences/${username}`,
24+
formattedData,
25+
{ headers: { 'Content-Type': 'application/merge-patch+json' } },
26+
);
27+
}
28+
29+
/**
30+
* Sets the language for the current session using the setlang endpoint.
31+
*
32+
* This function sends a POST request to the LMS setlang endpoint to change
33+
* the language for the current user session.
34+
*
35+
* @param {string} languageCode - The language code to set (e.g., 'en', 'es', 'ar').
36+
* Should be a valid ISO language code supported by the platform.
37+
* @returns {Promise} - A promise that resolves when the API call completes successfully,
38+
* or rejects if there's an error with the request.
39+
*/
40+
export async function setSessionLanguage(languageCode) {
41+
const formData = new FormData();
42+
formData.append('language', languageCode);
43+
44+
return getAuthenticatedHttpClient().post(
45+
`${getConfig().LMS_BASE_URL}/i18n/setlang/`,
46+
formData,
47+
{
48+
headers: {
49+
Accept: 'application/json',
50+
'X-Requested-With': 'XMLHttpRequest',
51+
},
52+
},
53+
);
54+
}

src/i18n/languageManager.js

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,9 @@
1-
/**
2-
* LanguageManager.js
3-
*
4-
* Provides utility functions for updating the session language preferences for users.
5-
*/
6-
71
import { getConfig } from '../config';
8-
import { getAuthenticatedHttpClient, getAuthenticatedUser } from '../auth';
9-
import { convertKeyNames, snakeCaseObject } from '../utils';
2+
import { getAuthenticatedUser } from '../auth';
103
import { getCookies, handleRtl, LOCALE_CHANGED } from './lib';
11-
import { logError } from '../logging';
124
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-
}
5+
import { logError } from '../logging';
6+
import { updateUserPreferences, setSessionLanguage } from './languageApi';
617

628
/**
639
* Changes the user's language preference and applies it to the current session.

0 commit comments

Comments
 (0)