【发布时间】:2021-08-03 22:19:28
【问题描述】:
我正在尝试使用 Microsoft Translator API 将文本从波兰语翻译成任何其他语言。在波兰语中,有几个特殊字符,如“ą”、“ś”、“ż”等。当我发送没有特殊字符的 HTTP 请求时:
POST /translate?api-version=3.0&from=pl&to=en HTTP/1.1
Ocp-Apim-Subscription-Key: ********
Ocp-Apim-Subscription-Region: ******
Content-Length: 21
Host: api.cognitive.microsofttranslator.com
Connection: close
User-Agent: Apache-HttpClient/4.5.10 (Java/15.0.2)
Accept-Encoding: gzip, deflate
[{"Text": "Gramatyka"}]
我收到正确的翻译:
[{"translations":[{"text":"grammar","to":"en"}]}]
但是,波兰语单词或句子很可能包含特殊字符:
POST /translate?api-version=3.0&from=pl&to=en HTTP/1.1
Ocp-Apim-Subscription-Key: ********
Ocp-Apim-Subscription-Region: ********
Content-Length: 21
Host: api.cognitive.microsofttranslator.com
Connection: close
User-Agent: Apache-HttpClient/4.5.10 (Java/15.0.2)
Accept-Encoding: gzip, deflate
[{"Text": "Roślina"}]
此请求导致错误代码 400000:
{"error":{"code":400000,"message":"One of the request inputs is not valid."}}
如果我将特殊字符更改为标准字符(例如将“ś”更改为“s”),API 不会提供正确的翻译。例如:
[{"Text": "Roslina"}]
结果:
[{"translations":[{"text":"Roslina","to":"en"}]}]
而“roślina”应该翻译成“植物”。
这个问题也适用于其他语言。例如德语:
[{"Text": "Wörterbuch"}]
也会导致 400000 错误。
有人找到解决办法了吗?
【问题讨论】:
-
这能回答你的问题吗? How to send UTF8 to the WordPress REST-API?
-
注意 dup 讲的是 WordPress,但问题几乎可以肯定是一样的,你需要将
ContentType标头设置为application/json; charset=utf-8。 -
我之前尝试过,但没有成功,但在您意识到所需的标头不是
ContentType而是Content-Type之后。没有破折号它不起作用,令人惊讶的是它也区分大小写。谢谢!
标签: azure api microsoft-cognitive azure-cognitive-services microsoft-translator