External Sailsjs controller req.setLocale

I want to set the language standard to a language other than the standard one in the cronjob scheduler. https://github.com/ghaiklor/sails-hook-cron

The cronjob scheduler code is as follows:

// ['seconds', 'minutes', 'hours', 'dayOfMonth', 'month', 'dayOfWeek'] module.exports.cron = { job: { schedule: '0 0 12 * * *', onTick: function() { SomeService.sendSms() }, timezone: 'Asia/Jerusalem' } } 

But I can’t set the locale because it’s not a controller, but a service, and I don’t have access to req.setLocale globally.

+1
source share
1 answer

It depends on which version of Sails you are using.

For Sails v0.12.x, the only way to specify a dynamic language is to use a dictionary as an argument to sails.__ :

 sails.__({ phrase: 'Welcome', locale: 'fr' }) 

will provide you with Bienvenue using the default Sails app.

This syntax is not available in Sails 1.0, but you can change the current language using sails.hooks.i18n.setLocale() :

 var curLocale = sails.hooks.i18n.getLocale(); sails.hooks.i18n.setLocale('fr'); sails.__('Welcome'); sails.hooks.i18n.setLocale(curLocale); 

will again provide you with Bienvenue using the default Sails application, ensuring that after that the locale will revert to the default. This way you do not accidentally change the locale for all subsequent __ calls.

+2
source

All Articles