Ensure client-side translations are correctly extracted into the message templates.

This commit is contained in:
Sarah McAlear
2017-03-27 13:24:47 -04:00
committed by Dave Page
parent 633d2ad28c
commit 82bd97aed0
9 changed files with 99 additions and 98 deletions

View File

@@ -0,0 +1,32 @@
define(["translations"], function (translations) {
/***
* This method behaves as a drop-in replacement for flask translation rendering.
* It uses the same translation file under the hood and uses flask to determine the language.
*
* ex. translate("some %(adjective)s text", {adjective: "cool"})
*
* @param {String} text
* @param {Object} substitutions
*/
return function gettext(text, substitutions) {
var rawTranslation = translations[text] ? translations[text] : text;
// captures things of the form %(substitutionName)s
var substitutionGroupsRegExp = /([^%]*)%\(([^\)]+)\)s(.*)/;
var matchFound;
var interpolated = rawTranslation;
do {
matchFound = false;
interpolated = interpolated.replace(substitutionGroupsRegExp, function (_, textBeginning, substitutionName, textEnd) {
matchFound = true;
return textBeginning + substitutions[substitutionName] + textEnd;
});
} while (matchFound);
return interpolated;
};
});