RESTful服务中的本地化 [英] localisation in RESTful services

查看:41
本文介绍了RESTful服务中的本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RESTful服务,该服务返回一个枚举.

I have a RESTful service that returns an enum.

我在考虑是否返回整数或字符串作为枚举值,当我想到返回字符串将取决于客户端的语言环境时.

I was pondering whether to return integers or strings for an enum value, when it occurred to me that returning a string would depend on the locale of the client.

那么您应该如何在REST中处理本地化?语言环境是conneg的一部分吗?

So how should you handle localisation in REST? Is locale part of conneg?

推荐答案

本地化 是内容协商的一部分,并且使用 Accept-Language 标头指定语言首选项( RFC ).

Localisation is part of content negotiation, and language preferences are specified using the Accept-Language header (RFC).

RFC对于允许服务在此处返回的内容非常严格-例如,如果未发送标头,则可以选择任何语言,但是如果包含标头,则只能返回所请求的一种语言,并且不允许使用其他语言.

The RFC is quite strict about what the service is allowed to return here — for instance, if no header is sent, then you may choose any language, but if a header is included, then you may only return one of the requested languages, and are not allowed to fall back to a different language.

换句话说,如果资源仅以英语提供,则使用 Accept-Language:de 请求的资源应返回 406 Unacceptable ,而不是默认显示英文版.

In other words, if a resource is only available in English, then a resource requested with Accept-Language: de should return 406 Unacceptable rather than default to presenting the English version.

其他未使用规范但允许回退选项的其他替代方案在其他几个问题中进行了讨论( 7892502 ).

Other alternatives that don't use the specification but do allow for fallback options are discussed in a couple of other questions (8204816, 7892502).

这篇关于RESTful服务中的本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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