多语言 REST 资源 - URL 命名建议
是否有用于获取不同语言资源的 REST 最佳实践。目前,我知道
www.mysite.com/books?locale=en
我们可以使用接受语言标头,但是对我们来说这样做更好
www.mysite.com/books/en or www.mysite.com/books.en
还是无关紧要?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您尝试让服务器返回同一本书的不同翻译或本地化版本(换句话说,从 RESTful 角度来看相同的资源),请使用 Accept-Language 因为资源相同,但表示形式不同客户的需求。
但是,如果您尝试根据客户端的区域设置返回完全不同的书籍(例如,如果您知道用户位于法国,则返回用法语编写的书籍),那么 URI 应该不同,因为不同的资源会被退回。此时,您更多地讨论的是查询请求。就其价值而言,
/books/en
方法听起来很合理。另一种方法是将区域设置或语言作为资源参数添加到 GET 中,如/books?lang=en
。If you're trying to have your server return different translations or localized versions of the same books (in other words, the same resource from a RESTful perspective), then use Accept-Language because the resource is the same but the representation is different based on the client's needs.
However if you're trying to return completely different books based on the client's locale (say, returning books written in French if you know that the user is in France) then the URIs should be different since different resources would be returned. At this point, you're talking more about a query request more than anything else. For what it's worth, the
/books/en
approach sounds reasonable. Another approach would be to add the locale or language as a resource parameter to GET as/books?lang=en
.我认为最好的方法是实现以下方式:
换句话说,您可以接受来自两个来源的语言。实现如下:
通过这种方法,您可以确保 a) 路径路由与语言无关,并且您的系统将与路径统一工作; b) 语言处理/协商将与您的脚本完全分离。您可以在脚本中使用语言信息,甚至无需知道来源是什么以及如何请求它。
I think best way would be to implement this following way:
In other words you can accept language from both sources. Implementation will be following:
With this approach you can make sure that a) path routing will be language agnostic and your system will work uniformly with paths; b) language handling/negotiation will be completely separated from your scripts. You can use language information in your scripts without even knowing what was the source and how it was requested.
我同意 manuel-aldana 中的评论 问题 href="https://stackoverflow.com/questions/7892502/restful-url-where-should-i-put-locale-example-com-en-page-vs-example-com-page">RESTful URL:其中我应该输入语言环境吗? example.com/en/page 与 example.com/page?locale=en
首先检查参数(例如 locale=en ),以允许客户端显式指定语言并回退到 接受语言
I am agree with comment of manuel-aldana in the answer to the question RESTful URL: where should I put locale? example.com/en/page vs example.com/page?locale=en
Check for parameter (e.g. locale=en ) first to allow client explicitely specify language with fallback to Accept-Language