Neel
Neel

Reputation: 21315

flask babel not able to load the translated files

I am trying to translate my current flask project and I follow the step mentioned in flask-babel.

First I tried with new flask-restx app. It works fine. When I compile the messages, it get those messages when I pass Accept-Language header with that language.

When I try to integrate same with already developed project, it always give English translation even I pass Accept-Language header. I tried printing the headers in my function, it comes as non english.

My code is as below.

from flask import Flask, request, jsonify
from flask_babel import Babel, gettext
from flask_restx import Api, Resource, fields

def get_locale():
    # otherwise try to guess the language from the user accept
    # header the browser transmits.  We support de/fr/en in this
    # example.  The best match wins.
    print("*"*80)
    return request.accept_languages.best_match(['en', 'ar'])

app = Flask(__name__)
babel = Babel(app,
              locale_selector=get_locale)

api = Api(app, version='1.0', title='My API', doc='/api-swagger')
...
...

One thing I notice between new app and already developed app was, print statement in the get_locale is executing in new app, but it wont execute in my developed app.

I tried enabling debug mode and remove all pyc files. Also check the file permission of messages.mo, those are same between new app and already developed app.

Is there any other way to know, why Accept-Language headers wont tell flask to use translation files ?

Upvotes: 1

Views: 61

Answers (0)

Related Questions