rubenv/angular-gettext

Adding a translation at runtime?

rdeslonde-eichleay opened this issue · 6 comments

Our customers can provide custom messages to their customers on our platform. So we want to enable them to provide their own translations of their custom messages. So at runtime, I would like to add their translation to the catalog. Is that possible?

I think this answers my question?

https://angular-gettext.rocketeer.be/dev-guide/manual-setstrings/

But will setStrings overwrite existing language translation, or just add them to the existing catalog for a given language?

@rdeslonde-eichleay yes it does, that's exactly the way to go!

If I already have a bunch of translations from a po file, and I use setStrings, will it clear the translations that already exist and set the new ones, or will it just set the new ones, adding them to the existing set?

Source is here:

/**
* @ngdoc method
* @name gettextCatalog#setStrings
* @public
* @param {String} language language name
* @param {Object.<String>} strings set of strings where the key is the translation `key` and `value` is the translated text
* @description Processes an object of string definitions. {@link guide:manual-setstrings More details here}.
*/
setStrings: function (language, strings) {
if (!this.strings[language]) {
this.strings[language] = {};
}
var defaultPlural = gettextPlurals(language, 1);
for (var key in strings) {
var val = strings[key];
if (isHTMLModified) {
// Use the DOM engine to render any HTML in the key (#131).
key = angular.element('<span>' + key + '</span>').html();
}
if (angular.isString(val) || angular.isArray(val)) {
// No context, wrap it in $$noContext.
var obj = {};
obj[noContext] = val;
val = obj;
}
if (!this.strings[language][key]) {
this.strings[language][key] = {};
}
for (var context in val) {
var str = val[context];
if (!angular.isArray(str)) {
// Expand single strings
this.strings[language][key][context] = [];
this.strings[language][key][context][defaultPlural] = str;
} else {
this.strings[language][key][context] = str;
}
}
}
broadcastUpdated();
},

It merges.

Sweet, thanks. This is an awesome library. Thanks for maintaining it.

Thanks, glad you like it. Don't have too much time for it nowadays, but it doesn't ask for much either.

Spread the word!