Mervin Hemaraju
Mervin Hemaraju

Reputation: 2127

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.

Upvotes: 0

Views: 421

Answers (1)

Dogukan Altay
Dogukan Altay

Reputation: 85

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

Upvotes: 1

Related Questions