2016-12-01 99 views
0

我已经这样设置英语语言环境文件中的js帆负载JSON区域

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

我如何动态地加载在我的控制器,服务的价值,......? 我使用船帆版本0.11.2

回答

0

您可以使用内置的国际化

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

+0

谢谢。我在文档中看到过这个,但想知道是否可以这样做。 – iwooli