带有电子邮件模板的 i18n 模块
i18n module with email-templates
我已经设置了 nodemailer to work with the email-templates 模块,使用 ejs
作为渲染引擎。
到目前为止它按预期工作,但是,与我的应用程序的其他部分一样,我还想使用 i18n 模块以与我的应用程序呈现的视图相同的方式翻译我的邮件文本.
不幸的是,它不起作用。这是我尝试做的:
例子html.ejs:
<h1><%= __('Hi') %>, <%= user.name %>!</h1>
路线中的节点代码:
// requires at the top
var i18n = require('i18n');
// (.....)
// use template based sender to send a message
sendMailTemplate({
to: user.email,
// EmailTemplate renders html and text but no subject so we need to
// set it manually either here or in the defaults section of templateSender()
subject: i18n.__('translatable subject')
}, {
user: user,
__: i18n.__
}, function(err, info){
if(err){
console.log('Error');
console.log(err);
}else{
console.log('email sent');
req.flash('info', i18n.__('mail sent to %s', user.email));
done(err, 'done');
}
});
// other stuff...
只有几件事:
- 在我的邮件输出中,没有任何问题 - 只是字符串没有被翻译。
- 我认为只需将对象中的
i18n
的 __
函数传递给渲染引擎就足以让 ejs
可以使用它并按预期执行,但是我不确定。对此有什么想法吗?
- 我没有
i18n.setLocale
,因此当 req.
时它应该默认为英语(顺便说一句,这是我在收到的电子邮件中看到的语言)。这可能是它没有按预期翻译的原因吗?
欢迎任何想法!
嗯,事实证明我是对的。 i18n
确实默认为英语。我认为我应该在将语言环境传递给模板引擎之前设置语言环境并且它起作用了。
我使用 express.js 的 req.accceptsLanguages() 方法获得了首选的接受语言。这不是万无一失的,因为无法确保这确实是用户的母语(尽管在大多数情况下,它应该是)。
此外,由于我在其全局范围内使用 i18n
,这就是它默认为英语的原因。因此,我也将它传递给模板引擎,然后按指示使用它 here:
__({phrase: 'Hello', locale: 'fr'}); // Salut
所以,最后一点是这样的:
// requires at the top
var i18n = require('i18n');
// (.....)
// THIS IS NEW - it's from express.js
// returns a shorthand for the user's preferred locale: en, es, de, fr, pt, ...
var userLocale = req.acceptsLanguages()[1];
// use template based sender to send a message
sendMailTemplate({
to: user.email,
// EmailTemplate renders html and text but no subject so we need to
// set it manually either here or in the defaults section of templateSender()
subject: i18n.__({phrase: 'translatable subject', locale: userLocale}) // note the syntax here
}, {
user: user,
__: i18n.__,
locale: userLocale // THIS IS ALSO NEW
}, function(err, info){
if(err){
console.log('Error');
console.log(err);
}else{
console.log('email sent');
// also note the syntax here:
req.flash('info', i18n.__({phrase: 'mail sent to %s', locale: userLocale}, user.email));
done(err, 'done');
}
});
// other stuff...
我已经设置了 nodemailer to work with the email-templates 模块,使用 ejs
作为渲染引擎。
到目前为止它按预期工作,但是,与我的应用程序的其他部分一样,我还想使用 i18n 模块以与我的应用程序呈现的视图相同的方式翻译我的邮件文本.
不幸的是,它不起作用。这是我尝试做的:
例子html.ejs:
<h1><%= __('Hi') %>, <%= user.name %>!</h1>
路线中的节点代码:
// requires at the top
var i18n = require('i18n');
// (.....)
// use template based sender to send a message
sendMailTemplate({
to: user.email,
// EmailTemplate renders html and text but no subject so we need to
// set it manually either here or in the defaults section of templateSender()
subject: i18n.__('translatable subject')
}, {
user: user,
__: i18n.__
}, function(err, info){
if(err){
console.log('Error');
console.log(err);
}else{
console.log('email sent');
req.flash('info', i18n.__('mail sent to %s', user.email));
done(err, 'done');
}
});
// other stuff...
只有几件事:
- 在我的邮件输出中,没有任何问题 - 只是字符串没有被翻译。
- 我认为只需将对象中的
i18n
的__
函数传递给渲染引擎就足以让ejs
可以使用它并按预期执行,但是我不确定。对此有什么想法吗? - 我没有
i18n.setLocale
,因此当req.
时它应该默认为英语(顺便说一句,这是我在收到的电子邮件中看到的语言)。这可能是它没有按预期翻译的原因吗?
欢迎任何想法!
嗯,事实证明我是对的。 i18n
确实默认为英语。我认为我应该在将语言环境传递给模板引擎之前设置语言环境并且它起作用了。
我使用 express.js 的 req.accceptsLanguages() 方法获得了首选的接受语言。这不是万无一失的,因为无法确保这确实是用户的母语(尽管在大多数情况下,它应该是)。
此外,由于我在其全局范围内使用 i18n
,这就是它默认为英语的原因。因此,我也将它传递给模板引擎,然后按指示使用它 here:
__({phrase: 'Hello', locale: 'fr'}); // Salut
所以,最后一点是这样的:
// requires at the top
var i18n = require('i18n');
// (.....)
// THIS IS NEW - it's from express.js
// returns a shorthand for the user's preferred locale: en, es, de, fr, pt, ...
var userLocale = req.acceptsLanguages()[1];
// use template based sender to send a message
sendMailTemplate({
to: user.email,
// EmailTemplate renders html and text but no subject so we need to
// set it manually either here or in the defaults section of templateSender()
subject: i18n.__({phrase: 'translatable subject', locale: userLocale}) // note the syntax here
}, {
user: user,
__: i18n.__,
locale: userLocale // THIS IS ALSO NEW
}, function(err, info){
if(err){
console.log('Error');
console.log(err);
}else{
console.log('email sent');
// also note the syntax here:
req.flash('info', i18n.__({phrase: 'mail sent to %s', locale: userLocale}, user.email));
done(err, 'done');
}
});
// other stuff...