Sails基础之View层——i18n

Sails的View层提供了i18n的解决方案,参考https://sailsjs.com/documentation/concepts/internationalization/locales
我们可以实现一个中文/英文的国际化案例:
创建中文支持:config/locales/zh.json:

{
    "Welcome": "欢迎"
}

修改config/i18n.js,增加对中文的支持,并设置默认语言环境为中文:

/**
 * 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'

};

创建一个测试模板:i18n.ejs:

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

route:

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

如果想要改变语言环境需要设置req对象locale:

req.setLocale('en');

我们创建一个i18nController:

sails generate controller i18n

为其添加以下内容:

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

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

};

我们这里修改route:

  '/i18n': 'i18nController.i18n',

你可能感兴趣的:(Node,Sails,v1.0)