MOBILE-2848 lang: Send language to WebServices

main
Dani Palou 2021-10-14 13:13:27 +02:00
parent 0ba56b1558
commit d7e6e04f65
3 changed files with 37 additions and 2 deletions

View File

@ -40,6 +40,7 @@ import { CoreWSError } from '@classes/errors/wserror';
import { CoreLogger } from '@singletons/logger'; import { CoreLogger } from '@singletons/logger';
import { Translate } from '@singletons'; import { Translate } from '@singletons';
import { CoreIonLoadingElement } from './ion-loading'; import { CoreIonLoadingElement } from './ion-loading';
import { CoreLang } from '@services/lang';
/** /**
* QR Code type enumeration. * QR Code type enumeration.
@ -574,9 +575,14 @@ export class CoreSite {
// Call the WS. // Call the WS.
try { 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<T>(method, data, preSets, wsPreSets); const response = await this.callOrEnqueueRequest<T>(method, data, preSets, wsPreSets);
if (preSets.saveToCache) { if (preSets.saveToCache) {
delete data.moodlewssettinglang;
this.saveToCache(method, data, response, preSets); this.saveToCache(method, data, response, preSets);
} }
@ -786,7 +792,8 @@ export class CoreSite {
return; return;
} }
const requestsData = { let lang: string | undefined;
const requestsData: Record<string, unknown> = {
requests: requests.map((request) => { requests: requests.map((request) => {
const args = {}; const args = {};
const settings = {}; const settings = {};
@ -799,6 +806,11 @@ export class CoreSite {
if (match[1] == 'settingfilter' || match[1] == 'settingfileurl') { if (match[1] == 'settingfilter' || match[1] == 'settingfileurl') {
// Undo special treatment of these settings in CoreWSProvider.convertValuesToString. // Undo special treatment of these settings in CoreWSProvider.convertValuesToString.
value = (value == 'true' ? '1' : '0'); 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; settings[match[1]] = value;
} else { } else {
@ -813,6 +825,7 @@ export class CoreSite {
}; };
}), }),
}; };
requestsData.moodlewssettinglang = lang;
const wsPresets: CoreWSPreSets = { const wsPresets: CoreWSPreSets = {
siteUrl: this.siteUrl, siteUrl: this.siteUrl,
@ -1942,6 +1955,11 @@ export type CoreSiteWSPreSets = {
*/ */
rewriteurls?: boolean; 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. * Defaults to true. Set to false when the expected response is null.
*/ */

View File

@ -23,6 +23,8 @@ import { CoreSettingsHelper, CoreColorScheme, CoreZoomLevel } from '../../servic
import { CoreApp } from '@services/app'; import { CoreApp } from '@services/app';
import { CoreIframeUtils } from '@services/utils/iframe'; import { CoreIframeUtils } from '@services/utils/iframe';
import { Diagnostic } from '@singletons'; import { Diagnostic } from '@singletons';
import { CoreSites } from '@services/sites';
import { CoreUtils } from '@services/utils/utils';
/** /**
* Page that displays the general settings. * Page that displays the general settings.
@ -109,7 +111,11 @@ export class CoreSettingsGeneralPage {
* Called when a new language is selected. * Called when a new language is selected.
*/ */
languageChanged(): void { 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); CoreEvents.trigger(CoreEvents.LANGUAGE_CHANGED, this.selectedLanguage);
}); });
} }

View File

@ -1145,6 +1145,17 @@ export class CoreSitesProvider {
return sites.map((site) => site.id); return sites.map((site) => site.id);
} }
/**
* Get instances of all stored sites.
*
* @return Promise resolved when the sites are retrieved.
*/
async getSitesInstances(): Promise<CoreSite[]> {
const siteIds = await this.getSitesIds();
return await Promise.all(siteIds.map(async (siteId) => await this.getSite(siteId)));
}
/** /**
* Login the user in a site. * Login the user in a site.
* *