Flask Babel不起作用 [英] Flask Babel doesn't work

查看:527
本文介绍了Flask Babel不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Flask Babel为flask admin项目做I18N。但是,即使使用简单的模板,也无法使其工作。
这里是代码,首先初始化babel,

  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'

添加结构管理页面并添加I18N,我也尝试了lazy_gettext(),但是没有成功。

$ p $ #创建管理员
admin = admin.Admin(app,'iInvest:',index_view = MyAdminIndexView(),base_template ='my_master.html')
#添加视图
admin.add_view(UserManageView User,db.session,gettext(u'User Management')))

使用babel获得I18N并添加翻译,

  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')

它找到所有的文本和手动添加翻译,然后编译,

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

重新启动服务器并检查页面,没有任何内容会被翻译。我尝试了其他的语言,但没有一个能起作用。可能的原因是什么?
下面是完整的代码 https://github.com/XiaokunHou/FlaskProject

解决方案

我发现我错了。翻译文件夹在错误的地方。它应该在iInvest(这是在其他情况下的应用程序).​​.

  pybabel摘要-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


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

解决方案

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

这篇关于Flask Babel不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆