[英]Flask RESTful API Translations
I have an API which returns a JSON list when navigating to the url per say http://www.example.com/list .
示例 JSON 列表如下:
{
"name": "This is a name"
}
但我也想以另一种语言提供这个确切的 JSON 列表,例如法语:
{
"name": "C'est un nom"
}
我设法做的是有2个不同的网址:
1 为英文: http://www.example.com/en/list 。 1 代表法语: http://www.example.com/fr/list 。
然后在我的代码中,我有两个类,又是 1 个英语和 1 个法语:
class ItemList_En(Resource):
def get(self):
return {"name": "This is a name"}
class ItemList_Fr(Resource):
def get(self):
return {"name": "C'est un nom"}
api.add_resource(ItemList_En, "/en/list")
api.add_resource(ItemList_Fr, "/fr/list")
我想知道这是否是这样做的唯一方法? 有没有更好的方法我不知道,因为我是 Python 和 Flask 的新手。 如果有人可以帮助我,将不胜感激。
您可以使用 Flask-Babel package 来支持多语言。 https://pythonhosted.org/Flask-Babel/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.