How to work with i18n in Sails.js' bootstrap?
Here is "res.i18n" in controller's actions, but I don't find some global instance (already initialized) like sails.i18n.
I guess code below should work in bootstrap.js:
i18n = require('i18n'),
i18n.configure({
locales: sails.config.i18n.locales,
directory: sails.config.appPath + sails.config.i18n.localesDirectory,
defaultLocale: sails.config.i18n.defaultLocale,
updateFiles: false,
extension: '.json'
});
__('Hello');
But may be there is some better way?
In sails, i18n has been set up for you already. You can configure i18n in config/i18n.js
and create locales in the config/locales/
directory.
As to usage, for your views you can use either i18n()
or __()
. In your controller using res.i18n()
. More on that can be found here.
As to the global method, you can call sails.__()
.
sails sets up most variables for you see source: hooks/i18n
right now you can only config the directory but there is a pull request
you can access i18n with __('Hello')
in your view.
to test it change your browser language
i18n with sails uses the header Accept-Language
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With