简体   繁体   中英

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 .

The sample JSON list is as such:

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

But i also want to provide this exact JSON list in another language per say french as such:

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

What i managed to do is having 2 different urls:

1 for English: http://www.example.com/en/list . 1 for French: http://www.example.com/fr/list .

And then in my code i have two classes, again 1 for English and 1 for French:

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. Grateful if someone could help me out.

You can use Flask-Babel package for multi-language support. https://pythonhosted.org/Flask-Babel/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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