Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MOBILE-4390 lang: Format LMS lang #3751

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/classes/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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';
import { CoreLang, CoreLangFormat } from '@services/lang';
import { CoreSites, CoreSitesReadingStrategy } from '@services/sites';
import { asyncInstance, AsyncInstance } from '../utils/async-instance';
import { CoreDatabaseTable } from './database/database-table';
Expand Down Expand Up @@ -970,7 +970,7 @@ export class CoreSite {
// Moodle uses underscore instead of dash.
data = {
...data,
moodlewssettinglang: (preSets.lang ?? await CoreLang.getCurrentLanguage()).replace('-', '_'),
moodlewssettinglang: CoreLang.formatLanguage(preSets.lang ?? await CoreLang.getCurrentLanguage(), CoreLangFormat.LMS),
};

try {
Expand Down
4 changes: 2 additions & 2 deletions src/core/components/recaptcha/recaptcha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import { Component, Input, OnInit } from '@angular/core';

import { CoreLang } from '@services/lang';
import { CoreLang, CoreLangFormat } from '@services/lang';
import { CoreSites } from '@services/sites';
import { CoreUtils } from '@services/utils/utils';
import { CorePath } from '@singletons/path';
Expand Down Expand Up @@ -52,7 +52,7 @@ export class CoreRecaptchaComponent implements OnInit {
* Initialize the lang property.
*/
protected async initLang(): Promise<void> {
this.lang = await CoreLang.getCurrentLanguage();
this.lang = await CoreLang.getCurrentLanguage(CoreLangFormat.LMS);
}

/**
Expand Down
14 changes: 9 additions & 5 deletions src/core/features/mainmenu/services/mainmenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import { Injectable } from '@angular/core';

import { CoreLang, CoreLangLanguage } from '@services/lang';
import { CoreLang, CoreLangFormat, CoreLangLanguage } from '@services/lang';
import { CoreSites } from '@services/sites';
import { CoreConstants } from '@/core/constants';
import { CoreMainMenuDelegate, CoreMainMenuHandlerToDisplay } from './mainmenu-delegate';
Expand Down Expand Up @@ -139,15 +139,19 @@ export class CoreMainMenuProvider {
return result;
}

const currentLang = await CoreLang.getCurrentLanguage();

const currentLangApp = await CoreLang.getCurrentLanguage();
const currentLangLMS = CoreLang.formatLanguage(currentLangApp, CoreLangFormat.LMS);
const fallbackLang = CoreConstants.CONFIG.default_lang || 'en';

// Get the right label for each entry and add it to the result.
for (const id in map) {
const entry = map[id];
let data = entry.labels[currentLang] || entry.labels[currentLang + '_only'] ||
entry.labels.none || entry.labels[fallbackLang];
let data = entry.labels[currentLangApp]
?? entry.labels[currentLangLMS]
?? entry.labels[currentLangApp + '_only']
?? entry.labels[currentLangLMS + '_only']
?? entry.labels.none
?? entry.labels[fallbackLang];

if (!data) {
// No valid label found, get the first one that is not "_only".
Expand Down
4 changes: 2 additions & 2 deletions src/core/features/siteplugins/services/siteplugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { CoreCourseAnyModuleData } from '@features/course/services/course';
import { CoreCourses } from '@features/courses/services/courses';
import { CoreApp } from '@services/app';
import { CoreFilepool } from '@services/filepool';
import { CoreLang } from '@services/lang';
import { CoreLang, CoreLangFormat } from '@services/lang';
import { CoreSites } from '@services/sites';
import { CoreTextUtils } from '@services/utils/text';
import { CoreUtils } from '@services/utils/utils';
Expand Down Expand Up @@ -81,7 +81,7 @@ export class CoreSitePluginsProvider {
args = args || {};
site = site || CoreSites.getCurrentSite();

const lang = await CoreLang.getCurrentLanguage();
const lang = await CoreLang.getCurrentLanguage(CoreLangFormat.LMS);

const defaultArgs: CoreSitePluginsDefaultArgs = {
userid: <number> args.userid ?? site?.getUserId(),
Expand Down
29 changes: 24 additions & 5 deletions src/core/services/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,28 @@ export class CoreLangProvider {
*
* @returns Promise resolved with the current language.
*/
async getCurrentLanguage(): Promise<string> {
if (this.currentLanguage !== undefined) {
return this.currentLanguage;
async getCurrentLanguage(format?: CoreLangFormat): Promise<string> {
if (this.currentLanguage === undefined) {
this.currentLanguage = await this.detectLanguage();
}

this.currentLanguage = await this.detectLanguage();
return format ? this.formatLanguage(this.currentLanguage, format) : this.currentLanguage;
}

return this.currentLanguage;
/**
* Update a language code to the given format.
*
* @param lang Language code.
* @param format Format to use.
* @returns Formatted language code.
*/
formatLanguage(lang: string, format: CoreLangFormat): string {
switch (format) {
case CoreLangFormat.App:
return lang.replace('_', '-');
case CoreLangFormat.LMS:
return lang.replace('-', '_');
}
}

/**
Expand Down Expand Up @@ -568,6 +582,11 @@ export class CoreLangProvider {

export const CoreLang = makeSingleton(CoreLangProvider);

export const enum CoreLangFormat {
LMS = 'lms',
App = 'app'
}

/**
* Language code. E.g. 'au', 'es', etc.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/core/services/sites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import { asyncInstance, AsyncInstance } from '../utils/async-instance';
import { CoreConfig } from './config';
import { CoreNetwork } from '@services/network';
import { CoreUserGuestSupportConfig } from '@features/user/classes/support/guest-support-config';
import { CoreLang } from '@services/lang';
import { CoreLang, CoreLangFormat } from '@services/lang';

export const CORE_SITE_SCHEMAS = new InjectionToken<CoreSiteSchema[]>('CORE_SITE_SCHEMAS');
export const CORE_SITE_CURRENT_SITE_ID_CONFIG = 'current_site_id';
Expand Down Expand Up @@ -419,7 +419,7 @@ export class CoreSitesProvider {
siteUrl = CoreUrlUtils.removeUrlParams(siteUrl);

try {
const lang = await CoreLang.getCurrentLanguage();
const lang = await CoreLang.getCurrentLanguage(CoreLangFormat.LMS);

data = await Http.post(`${siteUrl}/login/token.php?lang=${lang}`, { appsitecheck: 1 })
.pipe(timeout(CoreWS.getRequestTimeout()))
Expand Down Expand Up @@ -484,7 +484,7 @@ export class CoreSitesProvider {
}

service = service || CoreConstants.CONFIG.wsservice;
const lang = await CoreLang.getCurrentLanguage();
const lang = await CoreLang.getCurrentLanguage(CoreLangFormat.LMS);
const params = {
username,
password,
Expand Down
4 changes: 2 additions & 2 deletions src/core/services/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import { Injectable } from '@angular/core';

import { CoreLang } from '@services/lang';
import { CoreLang, CoreLangFormat } from '@services/lang';
import { CoreTextUtils } from '@services/utils/text';
import { CoreConstants } from '@/core/constants';
import { makeSingleton } from '@singletons';
Expand Down Expand Up @@ -260,7 +260,7 @@ export class CoreUrlUtilsProvider {
}

try {
let lang = await CoreLang.getCurrentLanguage();
let lang = await CoreLang.getCurrentLanguage(CoreLangFormat.LMS);
lang = CoreLang.getParentLanguage() || lang;

return docsUrl.replace('/en/', '/' + lang + '/');
Expand Down
4 changes: 2 additions & 2 deletions src/core/services/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { CorePlatform } from '@services/platform';
import { CoreSiteError, CoreSiteErrorOptions } from '@classes/errors/siteerror';
import { CoreUserGuestSupportConfig } from '@features/user/classes/support/guest-support-config';
import { CoreSites } from '@services/sites';
import { CoreLang } from './lang';
import { CoreLang, CoreLangFormat } from './lang';

/**
* This service allows performing WS calls and download/upload files.
Expand Down Expand Up @@ -445,7 +445,7 @@ export class CoreWSProvider {
args: this.convertValuesToString(data),
}];

const lang = await CoreLang.getCurrentLanguage();
const lang = await CoreLang.getCurrentLanguage(CoreLangFormat.LMS);

// The info= parameter has no function. It is just to help with debugging.
// We call it info to match the parameter name use by Moodle's AMD ajax module.
Expand Down
Loading