diff --git a/src/core/classes/site.ts b/src/core/classes/site.ts index b22ad2110..5cd67966d 100644 --- a/src/core/classes/site.ts +++ b/src/core/classes/site.ts @@ -40,6 +40,7 @@ import { CoreWSError } from '@classes/errors/wserror'; import { CoreLogger } from '@singletons/logger'; import { Translate } from '@singletons'; import { CoreIonLoadingElement } from './ion-loading'; +import { CoreLang } from '@services/lang'; /** * QR Code type enumeration. @@ -574,9 +575,14 @@ export class CoreSite { // Call the WS. try { + // Send the language to use. Do it after checking cache to prevent losing offline data when changing language. + data.moodlewssettinglang = preSets.lang ?? await CoreLang.getCurrentLanguage(); + data.moodlewssettinglang = data.moodlewssettinglang.replace('-', '_'); // Moodle uses underscore instead of dash. + const response = await this.callOrEnqueueRequest(method, data, preSets, wsPreSets); if (preSets.saveToCache) { + delete data.moodlewssettinglang; this.saveToCache(method, data, response, preSets); } @@ -786,7 +792,8 @@ export class CoreSite { return; } - const requestsData = { + let lang: string | undefined; + const requestsData: Record = { requests: requests.map((request) => { const args = {}; const settings = {}; @@ -799,6 +806,11 @@ export class CoreSite { if (match[1] == 'settingfilter' || match[1] == 'settingfileurl') { // Undo special treatment of these settings in CoreWSProvider.convertValuesToString. value = (value == 'true' ? '1' : '0'); + } else if (match[1] == 'settinglang') { + // Use the lang globally to avoid exceptions with languages not installed. + lang = value; + + return; } settings[match[1]] = value; } else { @@ -813,6 +825,7 @@ export class CoreSite { }; }), }; + requestsData.moodlewssettinglang = lang; const wsPresets: CoreWSPreSets = { siteUrl: this.siteUrl, @@ -1942,6 +1955,11 @@ export type CoreSiteWSPreSets = { */ rewriteurls?: boolean; + /** + * Language to send to the WebService (moodlewssettinglang). Defaults to app's language. + */ + lang?: string; + /** * Defaults to true. Set to false when the expected response is null. */ diff --git a/src/core/features/settings/pages/general/general.ts b/src/core/features/settings/pages/general/general.ts index 344164a37..e8ef89874 100644 --- a/src/core/features/settings/pages/general/general.ts +++ b/src/core/features/settings/pages/general/general.ts @@ -23,6 +23,8 @@ import { CoreSettingsHelper, CoreColorScheme, CoreZoomLevel } from '../../servic import { CoreApp } from '@services/app'; import { CoreIframeUtils } from '@services/utils/iframe'; import { Diagnostic } from '@singletons'; +import { CoreSites } from '@services/sites'; +import { CoreUtils } from '@services/utils/utils'; /** * Page that displays the general settings. @@ -109,7 +111,11 @@ export class CoreSettingsGeneralPage { * Called when a new language is selected. */ languageChanged(): void { - CoreLang.changeCurrentLanguage(this.selectedLanguage).finally(() => { + CoreLang.changeCurrentLanguage(this.selectedLanguage).finally(async () => { + // Invalidate cache for all sites to get the content in the right language. + const sites = await CoreSites.getSitesInstances(); + await CoreUtils.ignoreErrors(Promise.all(sites.map((site) => site.invalidateWsCache()))); + CoreEvents.trigger(CoreEvents.LANGUAGE_CHANGED, this.selectedLanguage); }); } diff --git a/src/core/services/sites.ts b/src/core/services/sites.ts index fd3d4c85b..702ca7a50 100644 --- a/src/core/services/sites.ts +++ b/src/core/services/sites.ts @@ -1145,6 +1145,17 @@ export class CoreSitesProvider { return sites.map((site) => site.id); } + /** + * Get instances of all stored sites. + * + * @return Promise resolved when the sites are retrieved. + */ + async getSitesInstances(): Promise { + const siteIds = await this.getSitesIds(); + + return await Promise.all(siteIds.map(async (siteId) => await this.getSite(siteId))); + } + /** * Login the user in a site. *