简体   繁体   English

带有语言和区域的 REST API 请求

[英]REST API request with language and region

I'm developing a REST API and I need to implement a method which needs language and country to produce result in the correct format since the result contains numbers and dates.我正在开发一个 REST API,我需要实现一种需要语言和国家/地区才能以正确格式生成结果的方法,因为结果包含数字和日期。

I was using the HTTP Accept-Language header to get the language.我使用 HTTP Accept-Language标头来获取语言。 The specs define the header as a language specifier, but now I'm not sure if it is correct to use this header for getting the country.规范将标题定义为语言说明符,但现在我不确定使用此标题获取国家/地区是否正确。 For example, I want to allow a result in Spanish language but with numbers in English format (with commas instead of dots).例如,我想允许结果为西班牙语,但数字为英语格式(用逗号而不是点)。

Is es-US an accepted value for the Accept-Language header? es-USAccept-Language标头的可接受值吗?

I was thinking that I could develop a new custom header like X-Country for my REST API.我在想我可以为我的 REST API 开发一个新的自定义标头,比如X-Country

Any thoughts?有什么想法吗? Thanks.谢谢。

There are good documents out there on how to localize your APIs .有关于如何本地化 API 的好文档。 There is even a stack overflow response about it .甚至还有一个关于它堆栈溢出响应

Mostly it revolves around content negotiation and the "Accept-Language" header.它主要围绕 内容协商和“接受语言”标头。 If you need to have currency managed separately, the general consensus seems to keep in in the payload rather then in headers, but if you are going to do headers I would do X-Accept-Currency (behaving akin to the other HTTP Accept headers, but with ISO 4217 currency codes) on the request, and X-Content-Currency on the response.如果您需要单独管理货币,一般共识似乎保留在有效负载中而不是在标头中,但是如果您要处理标头,我会使用X-Accept-Currency (行为类似于其他 HTTP Accept 标头,但在请求中使用 ISO 4217 货币代码),在响应中使用X-Content-Currency

UPDATE: Things have changed - if you intend to submit your header for standardization, you should not prefix it with X.更新:事情发生了变化——如果你打算提交你的标头以进行标准化,你不应该在它前面加上 X。

I was thinking that I could develop a new custom header like X-Country for my REST API.我在想我可以为我的 REST API 开发一个新的自定义标头,比如X-Country

I would avoid custom headers if one of the standard HTTP headers suit your needs.如果标准 HTTP 标头之一满足您的需求,我会避免使用自定义标头。

Is es-US an accepted value for Accept-Language header? es-USAccept-Language标头的可接受值吗?

Yes, es-US (Spanish / United States) is a valid locale (see the notes below) and it's a suitable value for the Accept-Language header:是的, es-US (西班牙语/美国)是一个有效的语言环境(请参阅下面的注释)并且它是Accept-Language标头的合适值:

5.3.5. 5.3.5. Accept-Language接受语言

The Accept-Language header field can be used by user agents to indicate the set of natural languages that are preferred in the response.用户代理可以使用Accept-Language标头字段来指示响应中首选的自然语言集。 Language tags are defined in Section 3.1.3.1 .语言标签在第 3.1.3.1 节中定义。 [...] [...]

The relevant parts of the section 3.1.3.1 are quoted below: 3.1.3.1节相关部分引用如下:

3.1.3.1. 3.1.3.1. Language Tags 语言标签

A language tag, as defined in RFC 5646 , identifies a natural language spoken, written, or otherwise conveyed by human beings for communication of information to other human beings. RFC 5646 中定义的语言标签标识人类口头、书面或以其他方式传达的用于与其他人交流信息的自然语言。 Computer languages are explicitly excluded.计算机语言被明确排除在外。 [...] [...]

A language tag is a sequence of one or more case-insensitive subtags, each separated by a hyphen character ( - , %x2D ).语言标签是一个或多个不区分大小写的子标签的序列,每个子标签由连字符( -%x2D%x2D In most cases, a language tag consists of a primary language subtag that identifies a broad family of related languages (eg, en = English), which is optionally followed by a series of subtags that refine or narrow that language's range (eg, en-CA = the variety of English as communicated in Canada).在大多数情况下,语言标签由一个主要语言子标签组成,该子标签标识广泛的相关语言系列(例如, en = 英语),其后可选地跟随一系列细化或缩小该语言范围的子标签(例如, en-CA = 在加拿大交流的各种英语)。 Whitespace is not allowed within a language tag.语言标签中不允许有空格。 Example tags include:示例标签包括:

 fr, en-US, es-419, az-Arab, x-pig-latin, man-Nkoo-GN

See RFC 5646 for further information.有关详细信息,请参阅RFC 5646


Note 1: The combinations of language and territory codes that can be considered valid (in the sense that a given population of a given territory is able to read and write a given language, and is comfortable enough to use it with computers) can be found here .注 1:可以找到可以被认为有效的语言和领土代码的组合(在某种意义上,给定领土的给定人口能够读写给定语言,并且足够舒适地使用计算机) 在这里

Note 2: Not sure which programming language you are using, but here 's the list of locales available in Java.注 2:不确定您使用的是哪种编程语言,但这里是 Java 中可用的语言环境列表。

Any thoughts?有什么想法吗? Thanks.谢谢。

My recommendation would be to start from having a resource for each (language, country) that you support, then worry about whether you want to have a single resource that does content negotiation .我的建议是从为您支持的每个(语言、国家/地区)提供一个资源开始,然后担心您是否想要一个进行内容协商的资源。

A simple pattern for a resource that uses content negotiation would be to use the Content-Location header to point back to the specific resource for the negotiated language/country pair.使用内容协商的资源的一个简单模式是使用Content-Location标头指向协商的语言/国家/地区对的特定资源。

See also Tom Christie's answer from 2012, especially the link to Fielding's comment in 2006另请参阅Tom Christie在 2012 年 的回答,尤其是指向Fielding 在 2006 年的评论的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM