MOBILE-2333 siteplugins: Use lang at plugin level and fix {$a}

main
Dani Palou 2018-03-12 12:52:58 +01:00
parent db3fb84547
commit f209da8ec3
3 changed files with 35 additions and 23 deletions

View File

@ -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<any>} Promise resolved when done.
*/
registerHandler(plugin: any, handlerName: string, handlerSchema: any): Promise<any> {
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));

View File

@ -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] = {

View File

@ -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) => {