【问题标题】:Load JSON locale in sails js在sails js中加载JSON语言环境
【发布时间】:2016-12-01 02:14:01
【问题描述】:

我在sails js中设置了这样的英语语言环境文件

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

如何动态加载控制器、服务等中的值? 我正在使用sails 0.11.2版

【问题讨论】:

    标签: node.js sails.js


    【解决方案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

    【讨论】:

    • 谢谢。我在文档中看到了这一点,但想知道是否可以这样做。
    猜你喜欢
    • 1970-01-01
    • 2013-05-01
    • 2019-01-29
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    相关资源
    最近更新 更多