服务i18n js使用babel,django和& Jinja2的 [英] serving i18n js using babel, django, & jinja2
问题描述
使用django,用jinja2来渲染& babel for message extraction
我有一些需要国际化的js文件。我没有能够找出从他们提取消息的语法,这也会让jinja2呈现它们。 jinja2要学习读取可抽取的语法,否则我必须从jinja2可以呈现的东西中提取出来。 (或者,完全这样做)
提取
如果我用js标记消息, p>
gettext('message')
它提取很好。
呈现
但是jinja2不会替换gettext调用js(我在使用jinja2渲染js模板,然后返回) - 它需要像
{%trans% }消息{%endtrans%}
但是,该语法不能用于提取消息。 / p>
Babel正在使用babel.messages中的extract_javascript函数来提取消息,这些消息看起来并不适合处理这种类型的标签。
嗯,看起来我可以做:
{{gettext(message)}}
定义gettext)
在JS和babel中将提取& jinja2将替换它。
注意报价。你不能这样做:
'{{gettext(message)}}'
因为extract_javascript不会读取它。但是,只要你把它放在里面就可以了,只要你安全地提交它们:
{{gettext('message' )| safe}}
所以你的翻译确保离开报价,无论他们在原来的。
Using django, with jinja2 for rendering & babel for message extraction
I have some js files that need to be internationalized. I haven't been able to figure out a syntax for extracting messages from them which would also let jinja2 render them. Either jinja2 has to learn to read an extractable syntax, or I have to extract from something jinja2 can render. (Or, do this another way entirely)
Extracting
If I mark messages in the js with
gettext('message')
It extracts just fine.
Rendering
But jinja2 won't replace gettext calls in js (I'm rendering the js templates with jinja2 before returning them) - it needs something like
{% trans %}message{% endtrans %}
But, that syntax can't be used to extract messages.
Babel is using the function extract_javascript from babel.messages to extract messages, which doesn't look equipeed to handle this type of tag.
Well, it looks like I can just do:
{{gettext("message")}}
(without defining gettext)
in the JS and babel will extract & jinja2 will replace it ok.
Watch out for quotes, though. You can't do:
'{{gettext("message")}}'
because extract_javascript will not read it. But, you can just put the quotes inside, as long as you render them safely:
{{gettext("'message'")|safe}}
So have your translators make sure to leave quotations wherever they find them in the original.
这篇关于服务i18n js使用babel,django和& Jinja2的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!