将可翻译文本从外部源获取到目录中
可能我忽略了一个明显的解决方案或以错误的方式思考...
我在数据库中的文本、单词数量有限,我想在 Flask/jinja/babel webapp 中向用户显示翻译后的内容。例如。 “running”是“activity”列的一个可能值,对于我的德国用户来说应该是“laufen”。
模板和代码中的单词被提取并放入目录中,但如何将其他单词放入目录中?有没有简单的文本文件提取器?
我唯一能想到的是,只需创建一个 .py 文件并在其中放入大量 _('...') 行,但这感觉不对......是吗?
Possibly i am overlooking an obvious solution or thinking the wrong way...
I have a limited amount of text, words in a database, that I want to display translated to users in a flask/jinja/babel webapp. eg. "running" is a possible value of an "activity" column and that should be "laufen" for my german users.
Words in templates and code are extracted and put into the catalog, but how do i get additional words into the catalog? Is there a simple text file extractor?
The only thing i could think of is, just create a .py file and put lots of _('...') lines in them, but that feels just wrong... is it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我用我的“单词”(如 gettext 函数调用)创建了一个 messages.txt:
并将其作为 python 源添加到我的 babel.cfg 中:
简单、简单、愚蠢,但有效。
I created a messages.txt with my "words" like gettext function calls:
and added it to my babel.cfg as python source:
plain, simple, stupid, but works.
首先,从 http://flask.pocoo.org/snippets/4/ 开始。
其次,您需要将这些“有限”值作为整数或枚举存储在数据库中,然后在代码中为所有这些枚举创建查找表(以便 Babel 了解它们):
并且从模板访问翻译后的字符串现在非常简单:
为了使
i18n_val
和i18n_all
变量可用于所有模板,只需将它们注册为 上下文处理器。First, start with http://flask.pocoo.org/snippets/4/.
Secondly, you need to store these 'limited' values as integers or enums in database and then create the lookup table for all these enums in code (so Babel knows about them):
And accessing the translated string from template is now as simple as:
In order to make the
i18n_val
andi18n_all
variables available for all the templates, just register them with context processors.