在sails js中加载JSON语言环境

时间:2016-12-01 02:14:02

标签: node.js sails.js

我在sails js

中设置了这样的英语语言环境文件
{
"api": {
"error": {
  "forbidden": "Access not granted",
  "notfound": "Could not find resource",
  "authentication": {
    "failure": "Authentication failure",
    "invalid_token": "Invalid token"
  }
 }
}

如何在控制器,服务中动态加载值? 我使用的是风帆版本0.11.2

1 个答案:

答案 0 :(得分:-1)

您可以使用内置国际化

Sails.js Locales

你把你的json放在config/locales/en.json。但它应该是简单的字典:

{
  "api.error.forbidden": "Access not granted",
  "api.error.notfound": "Could not find resource",
  "api.error.authentication.failure": "Authentication failure",
  "api.error.authentication.invalid_token": "Invalid token"
}

您可以在服务或控制器中使用它:

req.__('api.error.forbidden'); // => Access not granted

或在视图中

<%= __('api.error.forbidden') %>

还有一些选项,例如带有参数的语言环境。 Check out documentation