Once the app is deployed on Google AppEngine, pages show the msgid and not the translated text. I can't see any error either. But the locale seem to be ignored on GAE (see different output in debug messages below).
Other similar questions on SO mention that sometimes is due to case sensitive folders names, or different references to the translations file paths. But I double checked everything.
What else can I debug?
babel.cfg
[python: app/**.py]
[jinja2: app/templates/**.html]
[jinja2: app/main/templates/**.html]
extensions = jinja2.ext.autoescape, jinja2.ext.with_
encoding = utf-8
config.py
"""Flask config class."""
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config(object):
"""Babel config"""
LANGUAGES = ['en','es']
BABEL_TRANSLATION_DIRECTORIES = os.path.join(basedir, 'app/translations')
__init.py__
from flask import Flask
from flask import request
from flask import current_app
from config import Config
from flask_babel import Babel
babel = Babel()
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)
babel.init_app(app, locale_selector=get_locale)
from app.main import main_bp
app.register_blueprint(main_bp)
return app
def get_locale():
print('\n\n--> Babel debug')
print(current_app.config['LANGUAGES'])
print(babel.list_translations())
print('<-- Babel debug\n\n')
return request.accept_languages.best_match(current_app.config['LANGUAGES'])
OUTPUT on DEV
--> Babel debug
/Users/???/???/???/???/app
/Users/???/???/???/???/app/translations
['en', 'es']
[Locale('es'), Locale('en'), Locale('en')]
<-- Babel debug
OUTPUT on GAE
--> Babel debug
/srv/app
/srv/app/translations
['en', 'es']
[Locale('en')]
<-- Babel debug
CodePudding user response:
Just for future reference, the problem was that the localisation files where not uploaded to the server and Babel gives no errors or warnings and just fails silently in this case.
The reason why files where not uploaded is that the default Github .gitignore
file for Python projects, does not includes .mo
and .pot
files in the repository.
Subsequently the default .gcloudignore
incorporates all the rules from the Github file hence excluding the localisation files from the deployment process.
Therefore the issue here is not related to Babel (although failing silently is not a good pattern) but rather to the combo of Git/Github and GAE deployment defaults.