[英]How to translate strings in javascript written in script tags of html in django?
嗨,我正在翻译一个匈牙利语网站,我遇到了警报问题,并确认了我模板中的字符串。 我正在使用gettext('')
但这些字符串没有出现在 po 文件中
我的网址.py
urlpatterns = patterns('',
url(r'^jsi18n/$', javascript_catalog, js_info_dict, name='javascript-catalog'),
我通过运行 makemessages 创建了 po 文件 django.po,它在模板中标记为 trans 的所有字符串以及来自*.py
文件的字符串。 然后我运行了以下命令
django-admin.py makemessages -d djangojs -l hu_HU
它正在创建djangojs.po
。
此文件中出现的字符串均来自我的静态文件夹中的 *.js 文件。
但是我如何在警报中使用我的字符串并确认写在我的模板中。
这是我模板中的片段。
<script>
if($('#id_action').val()=='DEL'){
if(confirm(gettext('Are you sure you want to delete selected author(s) ?'))){
flag_action=true;
}
}
</script>
在我的模板中,我也有类似的东西,它也没有出现在 po 文件中。
<li>
<a onclick="if(confirm(gettext('Are you sure you want to delete the selected author?'))){filter_content({{auth.id}},'DEL');return false;}" href="javascript:void(0)">
{% trans 'Delete' %}
</a>
</li>
gettext 中的字符串未出现在任何 po 中。
我在我的模板中包含了以下内容
<script type="text/javascript" src="{% url 'javascript-catalog' %}"></script>
用这个
var alert_var = {% blocktrans %}"Are you sure you want to delete selected author(s) ?" {% endblocktrans %};
<!-- begin snippet: js hide: false console: true babel: false -->
然后在任何需要的地方使用这个变量
正如 Arhot 所说{% blocktrans %}"Text..."{% endblocktrans %}
完美运行。
如果您在 html 文件中的<script>
标签中使用它,单词 ids 将在文件.po
生成,对应于 html traslations 而不是 javascript traslations。 我的意思是你会在文件django.po
而不是在djangojs.po
找到新词。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.