简体   繁体   English

Python3、Jinja2、预编译模板、本地化

[英]Python3, Jinja2, pre-compiled template, localization

I have some old project .po/.mo files with placeholders like %s and it was used to translate python strings But now I have these strings in jinja2 templates我有一些旧的项目 .po/.mo 文件,带有像 %s 这样的占位符,它被用来翻译 python 字符串但是现在我在 jinja2 模板中有这些字符串

Problem is that in .po file %s meanwhile in template {{ some_var }}问题是在 .po 文件 %s 中同时在模板 {{ some_var }}

eg I have template:例如我有模板:

{% if some_var %}
Some text {{ some_var }}
{% else %}
Some another text {{ some_var1 }}
{% endif %}

I want to execute only conditions, filters, etc. So I want to get string我只想执行条件、过滤器等,所以我想获取字符串

Some another text {{ some_var1 }}

or或者

Some text {{ some_var }}

Next, I will get placeholders order and replace them with %s, use gettext and finally render the template接下来,我将获取占位符顺序并用 %s 替换它们,使用 gettext 并最终渲染模板

Any suggestions?有什么建议? Thank you谢谢

There is no automatic way to do that but you can just use the old .po files.没有自动方法可以做到这一点,但您可以只使用旧的.po文件。 If your build system uses msgmerge for updating the .po files, the old translations will be reused but as "fuzzy" entries.如果你的构建系统使用msgmerge来更新.po文件,旧的翻译将被重用,但作为“模糊”条目。 You can then update the translations in the .po files.然后您可以更新.po文件中的翻译。

You can also do that by hand.您也可以手动执行此操作。 Assuming that your .pot file is myproject.pot and you want to merge that into the existing (old) file ru.po you do:假设您的.pot文件是myproject.pot并且您想将其合并到现有的(旧)文件ru.po

$ msgmerge --previous --verbose ru.po myproject.pot

If you wonder why you cannot do that automatically: You want to replace %s with something like {{some_var}} .如果您想知道为什么不能自动执行此操作:您想用{{some_var}}类的内容替换%s Where should the information that the placeholder is some_var come from?占位符是some_var的信息应该从哪里来? In your old files, you always used %s and in the new files, the placeholders depend on the context.在旧文件中,您总是使用%s而在新文件中,占位符取决于上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM