Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails.js: How to work with i18n in bootstrap

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?

like image 553
anatolly Avatar asked Sep 30 '13 07:09

anatolly


2 Answers

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.__().

like image 116
Wesley Overdijk Avatar answered Nov 05 '22 15:11

Wesley Overdijk


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

like image 33
yellowsir Avatar answered Nov 05 '22 17:11

yellowsir