MOBILE-4292 multilang2: Create new multilang filter
parent
61809c06c8
commit
772864245e
|
@ -25,6 +25,7 @@ import { AddonFilterGlossaryModule } from './glossary/glossary.module';
|
||||||
import { AddonFilterMathJaxLoaderModule } from './mathjaxloader/mathjaxloader.module';
|
import { AddonFilterMathJaxLoaderModule } from './mathjaxloader/mathjaxloader.module';
|
||||||
import { AddonFilterMediaPluginModule } from './mediaplugin/mediaplugin.module';
|
import { AddonFilterMediaPluginModule } from './mediaplugin/mediaplugin.module';
|
||||||
import { AddonFilterMultilangModule } from './multilang/multilang.module';
|
import { AddonFilterMultilangModule } from './multilang/multilang.module';
|
||||||
|
import { AddonFilterMultilang2Module } from './multilang2/multilang2.module';
|
||||||
import { AddonFilterTexModule } from './tex/tex.module';
|
import { AddonFilterTexModule } from './tex/tex.module';
|
||||||
import { AddonFilterTidyModule } from './tidy/tidy.module';
|
import { AddonFilterTidyModule } from './tidy/tidy.module';
|
||||||
import { AddonFilterUrlToLinkModule } from './urltolink/urltolink.module';
|
import { AddonFilterUrlToLinkModule } from './urltolink/urltolink.module';
|
||||||
|
@ -42,6 +43,7 @@ import { AddonFilterUrlToLinkModule } from './urltolink/urltolink.module';
|
||||||
AddonFilterMathJaxLoaderModule,
|
AddonFilterMathJaxLoaderModule,
|
||||||
AddonFilterMediaPluginModule,
|
AddonFilterMediaPluginModule,
|
||||||
AddonFilterMultilangModule,
|
AddonFilterMultilangModule,
|
||||||
|
AddonFilterMultilang2Module,
|
||||||
AddonFilterTexModule,
|
AddonFilterTexModule,
|
||||||
AddonFilterTidyModule,
|
AddonFilterTidyModule,
|
||||||
AddonFilterUrlToLinkModule,
|
AddonFilterUrlToLinkModule,
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
import { APP_INITIALIZER, NgModule } from '@angular/core';
|
||||||
|
|
||||||
|
import { CoreFilterDelegate } from '@features/filter/services/filter-delegate';
|
||||||
|
import { AddonFilterMultilang2Handler } from './services/handlers/multilang2';
|
||||||
|
|
||||||
|
@NgModule({
|
||||||
|
providers: [
|
||||||
|
{
|
||||||
|
provide: APP_INITIALIZER,
|
||||||
|
multi: true,
|
||||||
|
useValue: () => CoreFilterDelegate.registerHandler(AddonFilterMultilang2Handler.instance),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
export class AddonFilterMultilang2Module {}
|
|
@ -0,0 +1,104 @@
|
||||||
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
|
||||||
|
import { CoreLang } from '@services/lang';
|
||||||
|
import { CoreFilterDefaultHandler } from '@features/filter/services/handlers/default-filter';
|
||||||
|
import { makeSingleton } from '@singletons';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler to support the Multilang filter.
|
||||||
|
*/
|
||||||
|
@Injectable({ providedIn: 'root' })
|
||||||
|
export class AddonFilterMultilang2HandlerService extends CoreFilterDefaultHandler {
|
||||||
|
|
||||||
|
name = 'AddonFilterMultilang2Handler';
|
||||||
|
filterName = 'multilang2';
|
||||||
|
replacementDone = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function filters the received text based on the language
|
||||||
|
* tags embedded in the text, and the current user language or
|
||||||
|
* 'other', if present.
|
||||||
|
*
|
||||||
|
* @param text The text to filter.
|
||||||
|
* @returns string The filtered text for this multilang block.
|
||||||
|
*/
|
||||||
|
async filter(text: string): Promise<string> {
|
||||||
|
if (text.indexOf('mlang') === -1) {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentLang = await CoreLang.getCurrentLanguage();
|
||||||
|
this.replacementDone = false;
|
||||||
|
const parentLanguage = CoreLang.getParentLanguage(currentLang);
|
||||||
|
|
||||||
|
const search = /{\s*mlang\s+((?:[a-z0-9_-]+)(?:\s*,\s*[a-z0-9_-]+\s*)*)\s*}(.*?){\s*mlang\s*}/gim;
|
||||||
|
const result = text.replace(
|
||||||
|
search,
|
||||||
|
(subString, language, content) => this.replaceLangs(currentLang, [subString, language, content], parentLanguage),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result === null) {
|
||||||
|
return text; // Error during regex processing, keep original text.
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.replacementDone) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result2 = text.replace(
|
||||||
|
search,
|
||||||
|
(subString, language, content) => this.replaceLangs('other', [subString, language, content], parentLanguage),
|
||||||
|
);
|
||||||
|
|
||||||
|
return result2 ?? text;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function filters the current block of multilang tag. If
|
||||||
|
* any of the tag languages (or their parent languages) match the
|
||||||
|
* specified filtering language, it returns the text of the
|
||||||
|
* block. Otherwise it returns an empty string.
|
||||||
|
*
|
||||||
|
* @param replaceLang A string that specifies the language used to filter the matches.
|
||||||
|
* @param langBlock An array containing the matching captured pieces of the
|
||||||
|
* regular expression. They are the languages of the tag, and the text associated with those languages.
|
||||||
|
* @param parentLanguage A string that contains the parent language.
|
||||||
|
*
|
||||||
|
* @returns replaced string.
|
||||||
|
*/
|
||||||
|
protected replaceLangs(replaceLang: string, langBlock: string[], parentLanguage: string | undefined): string {
|
||||||
|
// Normalize languages.
|
||||||
|
const blockLangs = (langBlock[1] ?? '').replace(/ /g, '').replace(/_/g, '-').toLowerCase().split(',');
|
||||||
|
const blockText = langBlock[2] ?? '';
|
||||||
|
|
||||||
|
for (const blockLang of blockLangs) {
|
||||||
|
/* We don't check for empty values of blockLang as they simply don't
|
||||||
|
* match any language and they don't produce any errors or warnings.
|
||||||
|
*/
|
||||||
|
if (blockLang === replaceLang || parentLanguage === blockLang) {
|
||||||
|
this.replacementDone = true;
|
||||||
|
|
||||||
|
return blockText;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export const AddonFilterMultilang2Handler = makeSingleton(AddonFilterMultilang2HandlerService);
|
|
@ -12,6 +12,8 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
import { AddonFilterMultilangHandler } from '@addons/filter/multilang/services/handlers/multilang';
|
||||||
|
import { AddonFilterMultilang2Handler } from '@addons/filter/multilang2/services/handlers/multilang2';
|
||||||
import { Component, Input, OnInit, Type } from '@angular/core';
|
import { Component, Input, OnInit, Type } from '@angular/core';
|
||||||
import { FormGroup } from '@angular/forms';
|
import { FormGroup } from '@angular/forms';
|
||||||
|
|
||||||
|
@ -51,6 +53,11 @@ export class CoreUserProfileFieldComponent implements OnInit {
|
||||||
|
|
||||||
this.componentClass = await CoreUserProfileFieldDelegate.getComponent(this.field, this.signup);
|
this.componentClass = await CoreUserProfileFieldDelegate.getComponent(this.field, this.signup);
|
||||||
|
|
||||||
|
if ('param1' in this.field && this.field.param1) {
|
||||||
|
this.field.param1 = await AddonFilterMultilangHandler.filter(<string> this.field.param1);
|
||||||
|
this.field.param1 = await AddonFilterMultilang2Handler.filter(<string> this.field.param1);
|
||||||
|
}
|
||||||
|
|
||||||
this.data.field = this.field;
|
this.data.field = this.field;
|
||||||
this.data.edit = CoreUtils.isTrueOrOne(this.edit);
|
this.data.edit = CoreUtils.isTrueOrOne(this.edit);
|
||||||
if (this.edit) {
|
if (this.edit) {
|
||||||
|
|
|
@ -58,6 +58,7 @@ import { CoreSiteError } from '@classes/errors/siteerror';
|
||||||
import { CoreUserSupport } from '@features/user/services/support';
|
import { CoreUserSupport } from '@features/user/services/support';
|
||||||
import { CoreErrorInfoComponent } from '@components/error-info/error-info';
|
import { CoreErrorInfoComponent } from '@components/error-info/error-info';
|
||||||
import { CorePlatform } from '@services/platform';
|
import { CorePlatform } from '@services/platform';
|
||||||
|
import { AddonFilterMultilang2Handler } from '@addons/filter/multilang2/services/handlers/multilang2';
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* "Utils" service with helper functions for UI, DOM elements and HTML code.
|
* "Utils" service with helper functions for UI, DOM elements and HTML code.
|
||||||
|
@ -1170,6 +1171,7 @@ export class CoreDomUtilsProvider {
|
||||||
if (hasHTMLTags && !CoreSites.getCurrentSite()?.isVersionGreaterEqualThan('3.7')) {
|
if (hasHTMLTags && !CoreSites.getCurrentSite()?.isVersionGreaterEqualThan('3.7')) {
|
||||||
// Treat multilang.
|
// Treat multilang.
|
||||||
options.message = await AddonFilterMultilangHandler.filter(<string> options.message);
|
options.message = await AddonFilterMultilangHandler.filter(<string> options.message);
|
||||||
|
options.message = await AddonFilterMultilang2Handler.filter(<string> options.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
const alertId = <string> Md5.hashAsciiStr((options.header || '') + '#' + (options.message || ''));
|
const alertId = <string> Md5.hashAsciiStr((options.header || '') + '#' + (options.message || ''));
|
||||||
|
|
Loading…
Reference in New Issue