Skip to content

Instantly share code, notes, and snippets.

@ozten
Created January 10, 2012 22:25
Show Gist options
  • Select an option

  • Save ozten/1591584 to your computer and use it in GitHub Desktop.

Select an option

Save ozten/1591584 to your computer and use it in GitHub Desktop.
Mustache to EJS
diff --git a/lib/browserid/prove_template.ejs b/lib/browserid/prove_template.ejs
index d2a4ccc..489e30f 100644
--- a/lib/browserid/prove_template.ejs
+++ b/lib/browserid/prove_template.ejs
@@ -1,9 +1,10 @@
-Thanks for verifying your email address. This message is being sent to you to complete your sign-in to {{site}}.
+<%= gettext(
+ strargs('Thanks for verifying your email address. This message is being sent to you to complete your sign-in to %
-Finish registration by clicking this link: {{link}}
+<%= gettext('Finish registration by clicking this link:') %> <%= link %>
-If you are NOT trying to sign into this site, just ignore this email.
+<%= gettext('If you are NOT trying to sign into this site, just ignore this email.') %>
-Thanks,
-BrowserID
-(A better way to sign in)
+<%= gettext('Thanks,') %>
+<%= gettext('BrowserID') %>
+<%= gettext('(A better way to sign in)') %>
diff --git a/lib/configuration.js b/lib/configuration.js
index 5ef08ec..facfd68 100644
--- a/lib/configuration.js
+++ b/lib/configuration.js
@@ -120,7 +120,7 @@ g_configs.production = {
// local development configuration
g_configs.local = {
URL: 'http://127.0.0.1:10002',
- email_to_console: true, // don't send email, just dump verification URLs to console.
+ email_to_console: false, // don't send email, just dump verification URLs to console.
use_minified_resources: false,
var_path: path.join(__dirname, "..", "var"),
database: {
diff --git a/lib/email.js b/lib/email.js
index ee72c08..4e7095a 100644
--- a/lib/email.js
+++ b/lib/email.js
@@ -37,7 +37,7 @@ const
emailer = require('nodemailer'),
fs = require('fs'),
path = require('path'),
-mustache = require('mustache'),
+ejs = require('ejs'),
config = require('./configuration.js'),
logger = require('./logging.js').logger;
@@ -55,7 +55,8 @@ if (smtp_params && smtp_params.host) {
}
}
-const template = fs.readFileSync(path.join(__dirname, "browserid", "prove_template.txt")).toString();
+const template = ejs.compile(
+ fs.readFileSync(path.join(__dirname, "browserid", "prove_template.ejs")).toString());
var interceptor = undefined;
@@ -74,8 +75,15 @@ exports.setInterceptor = function(callback) {
interceptor = callback;
};
-function doSend(landing_page, email, site, secret) {
- var url = config.get('URL') + "/" + landing_page + "?token=" + encodeURIComponent(secret);
+//TODO send in localeContext
+function doSend(landing_page, email, site, secret, langContext) {
+ var url = config.get('URL') + "/" + landing_page + "?token=" + encodeURIComponent(secret),
+ _ = langContext.gettext,
+ strargs = langContext.strargs;
+
+ console.log(langContext);
+ console.log('_', _);
+ console.log('strargs', strargs);
if (interceptor) {
interceptor(email, site, secret);
@@ -86,8 +94,8 @@ function doSend(landing_page, email, site, secret) {
emailer.send_mail({
sender: "[email protected]",
to: email,
- subject : "Complete Login to " + site + " using BrowserID",
- body: mustache.to_html(template, { email: email, link: url, site: site })
+ subject : _(strargs("Complete Login to %1 using BrowserID", site)),
+ body: template({ link: url, site: site, gettext: _, strargs: strargs })
}, function(err, success){
if(!success) {
logger.error("error sending email: " + err);
@@ -97,10 +105,10 @@ function doSend(landing_page, email, site, secret) {
};
};
-exports.sendNewUserEmail = function(email, site, secret) {
- doSend('verify_email_address', email, site, secret);
+exports.sendNewUserEmail = function(email, site, secret, langContext) {
+ doSend('verify_email_address', email, site, secret, langContext);
};
-exports.sendAddAddressEmail = function(email, site, secret) {
- doSend('add_email_address', email, site, secret);
+exports.sendAddAddressEmail = function(email, site, secret, langContext) {
+ doSend('add_email_address', email, site, secret, langContext);
};
diff --git a/lib/i18n.js b/lib/i18n.js
index 5d9516d..291c826 100644
--- a/lib/i18n.js
+++ b/lib/i18n.js
@@ -100,6 +100,9 @@ exports.abide = function (options) {
mo_path = path.join(__dirname, '..', options.locale_directory, locale,
'LC_MESSAGES', 'messages.mo');
+ resp.local('strargs', strargs);
+ req.strargs = strargs;
+
if (path.existsSync(mo_path)) {
gt.addTextdomain(locale, fs.readFileSync(mo_path));
@@ -199,4 +202,60 @@ exports.localeFrom = localeFrom = function (language) {
logger.error(util.format("Unable to map a local from language code [%s]", language));
return language;
}
+};
+
+/**
+ * Copied from `resources/static/shared/gettext.js`, so we can use the
+ * same method name on node.js and client-side.
+ */
+exports.strargs = strargs = function (str, args) {
+ // make sure args is an array
+ if ( null == args ||
+ 'undefined' == typeof(args) ) {
+ args = [];
+ } else if (args.constructor != Array) {
+ args = [args];
+ }
+
+ // NOTE: javascript lacks support for zero length negative look-behind
+ // in regex, so we must step through w/ index.
+ // The perl equiv would simply be:
+ // $string =~ s/(?<!\%)\%([0-9]+)/$args[$1]/g;
+ // $string =~ s/\%\%/\%/g; # restore escaped percent signs
+
+ var newstr = "";
+ while (true) {
+ var i = str.indexOf('%');
+ var match_n;
+
+ // no more found. Append whatever remains
+ if (i == -1) {
+ newstr += str;
+ break;
+ }
+
+ // we found it, append everything up to that
+ newstr += str.substr(0, i);
+
+ // check for escpaed %%
+ if (str.substr(i, 2) == '%%') {
+ newstr += '%';
+ str = str.substr((i+2));
+
+ // % followed by number
+ } else if ( match_n = str.substr(i).match(/^%(\d+)/) ) {
+ var arg_n = parseInt(match_n[1]);
+ var length_n = match_n[1].length;
+ if ( arg_n > 0 && args[arg_n -1] != null && typeof(args[arg_n -1]) != 'undefined' )
+ newstr += args[arg_n -1];
+ str = str.substr( (i + 1 + length_n) );
+
+ // % followed by some other garbage - just remove the %
+ } else {
+ newstr += '%';
+ str = str.substr((i+1));
+ }
+ }
+
+ return newstr;
};
\ No newline at end of file
diff --git a/lib/wsapi/stage_email.js b/lib/wsapi/stage_email.js
index 81d2f2c..6b388ca 100644
--- a/lib/wsapi/stage_email.js
+++ b/lib/wsapi/stage_email.js
@@ -27,14 +27,19 @@ exports.process = function(req, res) {
try {
// on failure stageEmail may throw
db.stageEmail(req.session.userid, req.body.email, function(secret) {
-
+ var langContext = {
+ lang: req.lang,
+ locale: req.locale,
+ gettext: req.gettext,
+ ngettext: req.ngettext
+ };
// store the email being added in session data
req.session.pendingAddition = secret;
res.json({ success: true });
// let's now kick out a verification email!
- email.sendAddAddressEmail(req.body.email, req.body.site, secret);
+ email.sendAddAddressEmail(req.body.email, req.body.site, secret, langContext);
});
} catch(e) {
// we should differentiate tween' 400 and 500 here.
diff --git a/lib/wsapi/stage_user.js b/lib/wsapi/stage_user.js
index 7830bb8..2338a09 100644
--- a/lib/wsapi/stage_user.js
+++ b/lib/wsapi/stage_user.js
@@ -17,6 +17,14 @@ exports.authed = false;
exports.args = ['email','site'];
exports.process = function(req, resp) {
+ var langContext = {
+ gettext: req.gettext,
+ lang: req.lang,
+ locale: req.locale,
+ ngettext: req.ngettext,
+ strargs: req.strargs
+ };
// staging a user logs you out.
wsapi.clearAuthenticatedUser(req.session);
@@ -31,6 +39,7 @@ exports.process = function(req, resp) {
// upon success, stage_user returns a secret (that'll get baked into a url
// and given to the user), on failure it throws
db.stageUser(req.body.email, function(secret) {
+
// store the email being registered in the session data
if (!req.session) req.session = {};
@@ -41,8 +50,8 @@ exports.process = function(req, resp) {
resp.json({ success: true });
- // let's now kick out a verification email!
- email.sendNewUserEmail(req.body.email, req.body.site, secret);
+ // let's now kick out a verification email!
+ email.sendNewUserEmail(req.body.email, req.body.site, secret, langContext);
});
} catch(e) {
// we should differentiate tween' 400 and 500 here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment