简体   繁体   English

Flask RESTful API 翻译

[英]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 . I have an API which returns a JSON list when navigating to the url per say http://www.example.com/list .

The sample JSON list is as such:示例 JSON 列表如下:

{
   "name": "This is a name"
}

But i also want to provide this exact JSON list in another language per say french as such:但我也想以另一种语言提供这个确切的 JSON 列表,例如法语:

{
   "name": "C'est un nom"
}

What i managed to do is having 2 different urls:我设法做的是有2个不同的网址:

1 for English: http://www.example.com/en/list . 1 为英文: http://www.example.com/en/list 1 for French: http://www.example.com/fr/list . 1 代表法语: http://www.example.com/fr/list

And then in my code i have two classes, again 1 for English and 1 for French:然后在我的代码中,我有两个类,又是 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")

I wanted to know if this is the only way of doing this?我想知道这是否是这样做的唯一方法? Is there a better way that i don't know of since i am new to Python and Flask.有没有更好的方法我不知道,因为我是 Python 和 Flask 的新手。 Grateful if someone could help me out.如果有人可以帮助我,将不胜感激。

You can use Flask-Babel package for multi-language support.您可以使用 Flask-Babel package 来支持多语言。 https://pythonhosted.org/Flask-Babel/ https://pythonhosted.org/Flask-Babel/

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

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