多语言 REST 资源 - URL 命名建议 [英] Multi-lingual REST resources - URL naming suggestions

查看:35
本文介绍了多语言 REST 资源 - URL 命名建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有获取不同语言资源的 REST 最佳实践.目前,我们有

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

www.mysite.com/books?locale=en

我知道我们可以使用 accept-language 标头,但对我们来说这样做更好吗

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

还是没关系?

推荐答案

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

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

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

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

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

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

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

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.

这篇关于多语言 REST 资源 - URL 命名建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆