Flask-Babel working in development but not translating in production

Question:

I have got Flask-Babel set up and working correctly on my localhost development app (with two languages: en_GB and en_US).

However, when I try to switch languages on the production app, it does nothing. The get_locale() function is returning the correct language (and I can see it is being called multiple times on a page refresh, suggesting it is being called for each required translation), but no actual translations appear.

As I say, it is working perfectly in the development app and I can’t see any reason why the behaviour should be any different in production. Any ideas would be much appreciated.

The translation files (messages.mo and messages.po) are stored in translations/en_us/LC_MESSAGES.

config.py

LANGUAGES = ['en_GB', 'en_US']

babel.cfg

[jinja2: megaseatingplan/templates/**.html]
extensions=jinja2.ext.autoescape,jinja2.ext.with_

__init__.py

# Initialise flask-babel
babel = Babel(app)

@babel.localeselector
def get_locale():
    try:
        print("Language loaded from current_user: "+str(current_user.language))
        return current_user.language
    except:
        print("Language loaded from browser: "+str(request.accept_languages.best_match(app.config['LANGUAGES'])))
        return request.accept_languages.best_match(app.config['LANGUAGES'])

Function for manually choosing language

@app.route('/switch_language/<language>')
@login_required
def switch_language(language):
    current_user.language = language
    db.session.commit()

    track_event("User", "Switch language", label=language, value=0, userid=current_user.userid)

    if language == "en_GB":
        flash("Language switched to English (UK)")
    elif language == "en_US":
        flash("Language switched to English (US)")

    return redirect(url_for('home'))

EDIT:
Some extra info. I have confirmed that the BABEL_TRANSLATION_DIRECTORIES is pointing to the correct place and have also use list_translations() to confirm that it is finding my en_US translation file.

Asked By: Rob

||

Answers:

I had the issue you describe. For me, the problem was that the directories in BABEL_TRANSLATION_DIRECTORIES were upper case (‘EN’). Renaming the directory with lower case (‘en’) solved it for my production environment.

I hope this can help you if you did not already solve you problem

Answered By: Benedikt Ramsauer

It took me a couple of years, but I have finally figured out my problem.

The path to the translations file was en_us/LC_MESSAGES/messages.mo, whereas the locale on Babel was en_US. On my local Windows development server, this was not a problem because Windows paths are case insensitive. However, running on Linux on Heroku, the path could not be found (an error message would’ve helped resolve this long ago).

After deleting the folder from the remote repository and recreating it as en_US/LC_MESSAGES/messages.mo, the issue is resolved.

Answered By: Rob
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.