From f209da8ec3efdebbb07467e7c241413e0751c17e Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 12 Mar 2018 12:52:58 +0100 Subject: [PATCH] MOBILE-2333 siteplugins: Use lang at plugin level and fix {$a} --- src/core/siteplugins/providers/helper.ts | 45 +++++++++++++----------- src/providers/lang.ts | 9 +++-- src/providers/utils/utils.ts | 4 +++ 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/core/siteplugins/providers/helper.ts b/src/core/siteplugins/providers/helper.ts index 8ecdac53a..435ead192 100644 --- a/src/core/siteplugins/providers/helper.ts +++ b/src/core/siteplugins/providers/helper.ts @@ -167,28 +167,28 @@ export class CoreSitePluginsHelperProvider { } /** - * Given a handler's unique name, return the prefix to add to its string keys. + * Given an addon name, return the prefix to add to its string keys. * - * @param {string} handlerName Handler's unique name (result of getHandlerUniqueName). + * @param {string} addon Name of the addon (plugin.addon). * @return {string} Prefix. */ - protected getHandlerPrefixForStrings(handlerName: string): string { - if (handlerName) { - return 'plugin.' + handlerName + '.'; + protected getPrefixForStrings(addon: string): string { + if (addon) { + return 'plugin.' + addon + '.'; } return ''; } /** - * Given a handler's unique name and the key of a string, return the full string key (prefixed). + * Given an addon name and the key of a string, return the full string key (prefixed). * - * @param {string} handlerName Handler's unique name (result of getHandlerUniqueName). + * @param {string} addon Name of the addon (plugin.addon). * @param {string} key The key of the string. * @return {string} Full string key. */ - protected getHandlerPrefixedString(handlerName: string, key: string): string { - return this.getHandlerPrefixForStrings(handlerName) + key; + protected getPrefixedString(addon: string, key: string): string { + return this.getPrefixForStrings(addon) + key; } /** @@ -216,21 +216,19 @@ export class CoreSitePluginsHelperProvider { } /** - * Load the lang strings for a handler. + * Load the lang strings for a plugin. * * @param {any} plugin Data of the plugin. - * @param {string} handlerName Name of the handler in the plugin. - * @param {any} handlerSchema Data about the handler. */ - loadHandlerLangStrings(plugin: any, handlerName: string, handlerSchema: any): void { - if (!handlerSchema.lang) { + loadLangStrings(plugin: any): void { + if (!plugin.parsedLang) { return; } - for (const lang in handlerSchema.lang) { - const prefix = this.getHandlerPrefixForStrings(this.sitePluginsProvider.getHandlerUniqueName(plugin, handlerName)); + for (const lang in plugin.parsedLang) { + const prefix = this.getPrefixForStrings(plugin.addon); - this.langProvider.addSitePluginsStrings(lang, handlerSchema.lang[lang], prefix); + this.langProvider.addSitePluginsStrings(lang, plugin.parsedLang[lang], prefix); } } @@ -249,9 +247,15 @@ export class CoreSitePluginsHelperProvider { if (!plugin.parsedHandlers) { plugin.parsedHandlers = JSON.parse(plugin.handlers); } + if (!plugin.parsedLang && plugin.lang) { + plugin.parsedLang = JSON.parse(plugin.lang); + } this.sitePluginsProvider.hasSitePluginsLoaded = true; + // Register lang strings. + this.loadLangStrings(plugin); + // Register all the handlers. for (const name in plugin.parsedHandlers) { promises.push(this.registerHandler(plugin, name, plugin.parsedHandlers[name])); @@ -288,7 +292,6 @@ export class CoreSitePluginsHelperProvider { * @return {Promise} Promise resolved when done. */ registerHandler(plugin: any, handlerName: string, handlerSchema: any): Promise { - this.loadHandlerLangStrings(plugin, handlerName, handlerSchema); // Wait for the bootstrap JS to be executed. return this.bootstrapHandler(plugin, handlerSchema).then((result) => { @@ -380,7 +383,7 @@ export class CoreSitePluginsHelperProvider { // Create and register the handler. const uniqueName = this.sitePluginsProvider.getHandlerUniqueName(plugin, handlerName), - prefixedTitle = this.getHandlerPrefixedString(uniqueName, handlerSchema.displaydata.title); + prefixedTitle = this.getPrefixedString(plugin.addon, handlerSchema.displaydata.title); this.courseOptionsDelegate.registerHandler(new CoreSitePluginsCourseOptionHandler(uniqueName, prefixedTitle, plugin, handlerSchema, bootstrapResult, this.sitePluginsProvider)); @@ -409,7 +412,7 @@ export class CoreSitePluginsHelperProvider { // Create and register the handler. const uniqueName = this.sitePluginsProvider.getHandlerUniqueName(plugin, handlerName), - prefixedTitle = this.getHandlerPrefixedString(uniqueName, handlerSchema.displaydata.title); + prefixedTitle = this.getPrefixedString(plugin.addon, handlerSchema.displaydata.title); this.mainMenuDelegate.registerHandler( new CoreSitePluginsMainMenuHandler(uniqueName, prefixedTitle, plugin, handlerSchema, bootstrapResult)); @@ -471,7 +474,7 @@ export class CoreSitePluginsHelperProvider { // Create and register the handler. const uniqueName = this.sitePluginsProvider.getHandlerUniqueName(plugin, handlerName), - prefixedTitle = this.getHandlerPrefixedString(uniqueName, handlerSchema.displaydata.title); + prefixedTitle = this.getPrefixedString(plugin.addon, handlerSchema.displaydata.title); this.userDelegate.registerHandler(new CoreSitePluginsUserProfileHandler(uniqueName, prefixedTitle, plugin, handlerSchema, bootstrapResult, this.sitePluginsProvider)); diff --git a/src/providers/lang.ts b/src/providers/lang.ts index a83aa8668..9f4dfd699 100644 --- a/src/providers/lang.ts +++ b/src/providers/lang.ts @@ -63,14 +63,19 @@ export class CoreLangProvider { } for (const key in strings) { - const prefixedKey = prefix + key, - value = strings[key]; + const prefixedKey = prefix + key; + let value = strings[key]; if (this.customStrings[lang] && this.customStrings[lang][prefixedKey]) { // This string is overridden by a custom string, ignore it. continue; } + // Add another curly bracket to string params ({$a} -> {{$a}}). + value = value.replace(/{([^ ]+)}/gm, '{{$1}}'); + // Make sure we didn't add to many brackets in some case. + value = value.replace(/{{{([^ ]+)}}}/gm, '{{$1}}'); + if (!this.sitePluginsStrings[lang][prefixedKey]) { // It's a new site plugin string. Store the original value. this.sitePluginsStrings[lang][prefixedKey] = { diff --git a/src/providers/utils/utils.ts b/src/providers/utils/utils.ts index ec57e20d4..f482b279d 100644 --- a/src/providers/utils/utils.ts +++ b/src/providers/utils/utils.ts @@ -836,6 +836,10 @@ export class CoreUtilsProvider { * @return {object} Object. */ objectToKeyValueMap(objects: object[], keyName: string, valueName: string, keyPrefix?: string): object { + if (!objects) { + return; + } + const prefixSubstr = keyPrefix ? keyPrefix.length : 0, mapped = {}; objects.forEach((item) => {