REST API,多语言设计

时间:2017-06-06 20:16:10

标签: rest api-design

我正在设计REST API(第一次),我对如何在POST请求中处理多语言抱有很多疑问。

该示例是广告提交。

  • 我的选择1:

参数(...,...,title_en,title_pt,description_en,description_pt)

POST请求(名称,值)

some_field1, "some text1"
some_field2, "some text2"
title_en, "Some Title"
title_pt, "Algum Titulo"
description_en, "Some Description"
description_pt, "Alguma Descrição"
  • 我的选择2:

参数(......,...,标题,说明)

POST请求(名称,值)

some_field1, "some text1"
some_field2, "some text2"
titles, [{"en":"Some Title","pt":"Algum Titulo"}]
descriptions, [{"en":"Some Description","pt":"Alguma Descrição"}]

我的问题:在REST API中执行此操作的正确方法是什么?

最诚挚的问候, 安德烈

1 个答案:

答案 0 :(得分:0)

没有'正确'的方法 - 相反,您最简单的查询数据的格式就是最简单的。

从面向对象的角度看,选项2将更容易使用,因为你不必对字段名称进行任何字符串操作来确定你想要使用哪种语言。

据说,从面向对象的角度来看,第三个选项将更加可用:

some_field1, "some text1"
some_field2, "some text2"
titles, [{"language":"en","value":"Some Title"},{"language":"pt",value":"Algum Titulo"}]
descriptions, [{"language":"en","value":"Some Description"},{"language":"pt","value":"Alguma Descrição"}]