serving i18n js using babel, django, & jinja2

▼魔方 西西 提交于 2020-01-15 10:43:37

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/10647449/serving-i18n-js-using-babel-django-jinja2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!