Luis Miguel Morillas
Luis Miguel Morillas

Reputation: 75

Transparent Internationalization API with Python EVE

We need an i18n API that supports a lot of languages in its queries. What is the best way of use the "Accept-Languages" header key? I'm thinking on having a collection for each language and do a transparent query on the correspondant collection. Or do you think it's better to hardcode the language in the uri?

Upvotes: 1

Views: 111

Answers (1)

Eugene Tsakh
Eugene Tsakh

Reputation: 2879

You can try this flask snippet: http://flask.pocoo.org/snippets/128/ Anyway using Accept-Language header is not good for this and better to use cookie, but you can make fallback to this header's information if cookie does not exists, to receive header content you can use flask.request.headers.get('Accept-Language') but keep in mind that this header might contain not single language, but for example something like this: da, en-gb;q=0.8, en;q=0.7

You can use before_request decorator to recognize language before request. Something like this:

@app.before_request
def before_request():
    flask.request.lang = flask.request.cookies.get('lang')
    if lang is None:
        flask.request.lang = flask.request.headers.get('Accept-Language', 'en').split(' ')[0]

And than you can use flask.request.lang anywhere you need this.

Upvotes: 2

Related Questions