问题描述
我想在cronjob Scheduler中将语言环境设置为默认语言以外的其他语言. https://github.com/ghaiklor/sails-hook-cron
I want to set the locale to other language than the default one, in cronjob scheduler.https://github.com/ghaiklor/sails-hook-cron
cronjob调度程序代码如下:
The cronjob scheduler code looks like that:
// ['seconds', 'minutes', 'hours', 'dayOfMonth', 'month', 'dayOfWeek']
module.exports.cron = {
job: {
schedule: '0 0 12 * * *',
onTick: function() {
SomeService.sendSms()
},
timezone: 'Asia/Jerusalem'
}
}
但是我无法设置语言环境,因为它不是控制器而是服务,并且我无法全局访问req.setLocale.
But I cant set the locale because its not a controller but a service and I don't have access to req.setLocale globally.
推荐答案
这取决于您使用的Sails版本.
This depends on which version of Sails you're using.
对于Sails v0.12.x,动态指定语言环境的唯一方法是使用字典作为sails.__
的参数:
For Sails v0.12.x, the only way to specify a locale dynamically is by using a dictionary as the argument to sails.__
:
sails.__({ phrase: 'Welcome', locale: 'fr' })
将为您提供Bienvenue
默认的Sails应用.
will give you Bienvenue
with a default Sails app.
此语法在Sails 1.0中不可用,但是您可以使用sails.hooks.i18n.setLocale()
更改当前语言环境:
This syntax isn't available in Sails 1.0, but you can change the current locale with sails.hooks.i18n.setLocale()
:
var curLocale = sails.hooks.i18n.getLocale();
sails.hooks.i18n.setLocale('fr');
sails.__('Welcome');
sails.hooks.i18n.setLocale(curLocale);
将再次为您提供Bienvenue
默认的Sails应用程序,同时确保随后将区域设置重新设置为默认设置.这样,您就不会意外更改__
的所有后续调用的语言环境.
will again give you Bienvenue
with a default Sails app, while ensuring that the locale is set back to the default afterwards. This way you don't accidentally change the locale for all subsequent invocations of __
.
这篇关于Sails.js req.setLocale外部控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!