Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask Babel doesn't work

I use Flask Babel to do I18N for flask admin project. But cannot make it works, even with simple template. Here are the codes, initialize babel first,

app=Flask(__name__,template_folder='templates')
app.config.from_object('config')
csrf=CsrfProtect(app)
bcrypt=Bcrypt(app)
db=SQLAlchemy(app)
babel=Babel(app) 
@babel.localeselector
def get_locale():
    return 'zh_Hans_CN'

Add construct admin page and add I18N, I also tries lazy_gettext().Not work.

# Create admin
admin = admin.Admin(app, 'iInvest:', index_view=MyAdminIndexView(), base_template='my_master.html')  
# Add view 
admin.add_view(UserManageView(User, db.session, gettext(u'User Management'))) 

Use babel to get I18N and add translations,

os.system(pybabel + ' extract -F babel.cfg -k lazy_gettext -o messages.pot iInvest/')
os.system(pybabel + ' init -i messages.pot -d translations -l zh_Hans_CN')
os.unlink('messages.pot')

It finds all texts and add translations manually, then compile it,

os.system(pybabel + ' compile -f -d translations')

Restart server and check page, nothing get translated. I tried other languages, none of them works. What's the possible reason? Here is the full code https://github.com/XiaokunHou/FlaskProject

like image 740
Xiaokun Avatar asked Mar 16 '23 02:03

Xiaokun


1 Answers

I find where I was wrong. The translations folder is in the wrong place. It should under iInvest(which is app in other cases)..

pybabel extract -F babel.cfg -k lazy_gettext -o messages.pot iInvest/
pybabel init -i messages.pot -d iInvest/translations -l zh_Hans_CN
pybabel compile -f -d iInvest/translations
like image 132
Xiaokun Avatar answered Mar 25 '23 02:03

Xiaokun