View layer of Sails foundation -- i18n

Keywords: JSON curl

The View layer of Sails provides the solution of i18n. Please refer to https://sailsjs.com/documentation/concepts/internationalization/locales
We can achieve an international case in Chinese / English:
Create Chinese support: config/locales/zh.json:

{
    "Welcome": "Welcome"
}

Modify config/i18n.js to add support for Chinese, and set the default language environment to Chinese:

/**
 * Internationalization / Localization Settings
 * (sails.config.i18n)
 *
 * If your app will touch people from all over the world, i18n (or internationalization)
 * may be an important part of your international strategy.
 *
 * For a complete list of options for Sails' built-in i18n support, see:
 * https://sailsjs.com/config/i-18-n
 *
 * For more info on i18n in Sails in general, check out:
 * https://sailsjs.com/docs/concepts/internationalization
 */

module.exports.i18n = {

  /***************************************************************************
  *                                                                          *
  * Which locales are supported?                                             *
  *                                                                          *
  ***************************************************************************/

  locales: ['en', 'zh'],

  /****************************************************************************
  *                                                                           *
  * What is the default locale for the site? Note that this setting will be   *
  * overridden for any request that sends an "Accept-Language" header (i.e.   *
  * most browsers), but it's still useful if you need to localize the         *
  * response for requests made by non-browser clients (e.g. cURL).            *
  *                                                                           *
  ****************************************************************************/

  defaultLocale: 'zh',

  /****************************************************************************
  *                                                                           *
  * Path (relative to app root) of directory to store locale (translation)    *
  * files in.                                                                 *
  *                                                                           *
  ****************************************************************************/

  // localesDirectory: 'config/locales'

};

Create a test template: i18n.ejs:

<h1> <%= __('Welcome') %>  xreztento!</h1>

route:

  '/i18n': {
    view: 'pages/i18n'
  },

If you want to change the locale, you need to set the req object locale:

req.setLocale('en');

We create an i18nController:

sails generate controller i18n

Add the following to it:

module.exports = {
  i18n: function(req, res){

    req.setLocale('en');
    return res.view('pages/i18n');
  }

};

Let's modify route:

  '/i18n': 'i18nController.i18n',

Posted by asa_carter on Sat, 09 Nov 2019 06:53:35 -0800