多语言 REST 资源 - URL 命名建议

发布于 2024-12-17 06:36:15 字数 222 浏览 4 评论 0 原文

是否有用于获取不同语言资源的 REST 最佳实践。目前,我知道

www.mysite.com/books?locale=en

我们可以使用接受语言标头,但是对我们来说这样做更好

www.mysite.com/books/en or www.mysite.com/books.en

还是无关紧要?

Is there a REST best practice for GETting resources in different languages. Currently, we have

www.mysite.com/books?locale=en

I know we can use the accept-language header but is it better for us to do

www.mysite.com/books/en or www.mysite.com/books.en

or does it not matter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

从﹋此江山别 2024-12-24 06:36:15

如果您尝试让服务器返回同一本书的不同翻译或本地化版本(换句话说,从 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.

臻嫒无言 2024-12-24 06:36:15

我认为最好的方法是实现以下方式:

  1. HTTP 的 Accept-Language 标头
  2. URI 中的语言前缀,例如 /en/books/...

换句话说,您可以接受来自两个来源的语言。实现如下:

  1. 检查是否提供了 Accept-Language 标头并将其保存在变量中;
  2. 如果请求 URI 以 /en、/fr 或其他已知语言代码(您的系统支持)开头,则使用此新值覆盖语言变量,并将其从 URI 中剥离,即如果 URI 为 / en/books 你最终会得到/books
  3. 如果没有提供语言,请将默认语言保留在变量中,例如“en”

通过这种方法,您可以确保 a) 路径路由与语言无关,并且您的系统将与路径统一工作; b) 语言处理/协商将与您的脚本完全分离。您可以在脚本中使用语言信息,甚至无需知道来源是什么以及如何请求它。

I think best way would be to implement this following way:

  1. HTTP's Accept-Language header
  2. Language prefix in URI such as /en/books/...

In other words you can accept language from both sources. Implementation will be following:

  1. Check if Accept-Language header is provided and keep this in the variable;
  2. If request URI starts with /en, /fr or other known language codes(that are supported by your system) Overwrite language variable with this new value, strip it from URI i.e. if URI is /en/books you will end up with /books.
  3. If there is no language provided, keep default language in variable for example "en"

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.

以歌曲疗慰 2024-12-24 06:36:15

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文