Closed epochslee closed 5 years ago
Can you please provide a minimal setup which reproduces the error?
Here the function that compiles the text to use in an email.
const {
promisify
} = require('util');
const fs = require('fs');
const Handlebars = require('Handlebars');
const readFileAsync = promisify(fs.readFile);
const mailerGet = {
method: 'GET',
path: '/mailer',
handler: async () => {
let compiledTemplate = null;
const content = (await readFileAsync('./templates/velocityNotification.hbs')).toString();
compiledTemplate = Handlebars.compile(content);
const body = compiledTemplate({
name: 'John Doe'
});
console.log(`body: ${body}`);
}
};
This is the Handlebars template I use to compile the body of the email. I have tried both {{{__ and {{#i18n to use the helper.
<div style='padding: 20px'>
<div style='padding: 10px'>
{{{__ "Thank you, %s, for starting the process." name}}}
</div>
</div>
I have this in my helper
Handlebars.registerHelper('i18n', function (context) {
return this.__(context);
});
Can you pls create a repository or zip file that I can use to reproduce the error? I'm not really confident in setting up Handlebars. Would be helpful if I only need to run npm install
and npm start
.
Run npm install navigate to localhost:3050/emailer
You will see the error catch in the try/catch printed in the logs:
{ Error: Missing helper: "__mf" at Object.<anonymous> (/Users/user/git/node/emailer_test/node_modules/Handlebars/dist/cjs/handlebars/helpers/helper-missing.js:19:13) at Object.eval [as main] (eval at createFunctionContext (/Users/user/git/node/emailer_test/node_modules/Handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:257:23), <anonymous>:6:85) at main (/Users/user/git/node/emailer_test/node_modules/Handlebars/dist/cjs/handlebars/runtime.js:175:32) at ret (/Users/user/git/node/emailer_test/node_modules/Handlebars/dist/cjs/handlebars/runtime.js:178:12) at ret (/Users/user/git/node/emailer_test/node_modules/Handlebars/dist/cjs/handlebars/compiler/compiler.js:526:21) at handler (/Users/user/git/node/emailer_test/server/routes/emailer.js:21:26) at <anonymous> description: undefined, fileName: undefined, lineNumber: undefined, message: 'Missing helper: "__mf"', name: 'Error', number: undefined } Debug: internal, implementation, error Error: handler method did not return a value, a promise, or throw an error at module.exports.internals.Manager.execute (/Users/user/git/node/emailer_test/node_modules/hapi/lib/toolkit.js:48:29) at <anonymous>
Thanks for providing a sample.. After a few modifications I got the error message you mentioned in your first comment:
this.__ is not a function
To me it seems like the problem is that you are losing the hapi context when using the Handlebars.compile
method (correct me if I'm wrong). I'm afraid I can't really help you here as this is not an i18n related issue and I'm not really an expert on Handlebars :(
I understand. Thanks for giving it a shot though Kay. I'll give it another go at it and will let you know if I find any solution. Will close for now.
No worries.. Would be interested in the solution though :)
Hi Kay,
Just wanted to get back to you about the solution I found for the handlebars compile I came up with. Thought it might be helpful to you and your users:
https://github.com/oprogfrogo/handlebars-hapi-compile-i18n-example
Just npm install
, run it and navigate to http://localhost:4321/mailer. You will see the output in the console.
This is my personal git account. Cheers!
Great, thanks for sharing it :)
👍
Hi Kay,
Just wanted to get back to you about the solution I found for the handlebars compile I came up with. Thought it might be helpful to you and your users:
https://github.com/oprogfrogo/handlebars-hapi-compile-i18n-example
Just
npm install
, run it and navigate to http://localhost:4321/mailer. You will see the output in the console.This is my personal git account. Cheers!
I'm having the same issue. I see that you translate the messages in a variable and then pass it to the file as an option. I have some emails with a lot of text and I prefer to don't translate it before compile the template.
The helper seems that for routes work ok, but when you try to compile with Handlebars doesn't work.
Any help?
My temporally solution was send the __
function in the data. So when the helper access to this
object it has the variable.
Mailer.send('password-reset', user, request.i18n.('EMAIL.SUBJECT'), { resetURL, : request.i18n.__ })
There is any way to access the i18n without the request object?
I use Handlebars.compile to render the template for sending emails. When I use
compile
I the following error:this.__ is not a function
Which I believe comes from the helper.