Node.js 在sailsjs中加载JSON语言环境
我在sails js中设置了这样的英语语言环境文件Node.js 在sailsjs中加载JSON语言环境,node.js,sails.js,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" } } } 如何动态加载控制器、服务等中
{
"api": {
"error": {
"forbidden": "Access not granted",
"notfound": "Could not find resource",
"authentication": {
"failure": "Authentication failure",
"invalid_token": "Invalid token"
}
}
}
如何动态加载控制器、服务等中的值?
我使用的是sails版本0.11.2,您可以使用内置国际化 您将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') %>
还有一些选项,比如带有参数的locale 谢谢。我在文档中看到过这一点,但我想知道是否可以这样做。