[英]How to translate strings in javascript written in script tags of html in django?
Hi I am translating a website in hungarian, I have problems with alerts and confirm strings that i have in my templates.嗨,我正在翻译一个匈牙利语网站,我遇到了警报问题,并确认了我模板中的字符串。 I am using
gettext('')
but these strings are not appearing in po files我正在使用
gettext('')
但这些字符串没有出现在 po 文件中
my urls.py我的网址.py
urlpatterns = patterns('',
url(r'^jsi18n/$', javascript_catalog, js_info_dict, name='javascript-catalog'),
I have created po file django.po by running makemessages it has all the strings marked as trans in templates and also strings from *.py
files.我通过运行 makemessages 创建了 po 文件 django.po,它在模板中标记为 trans 的所有字符串以及来自
*.py
文件的字符串。 then I have run following command然后我运行了以下命令
django-admin.py makemessages -d djangojs -l hu_HU
its creating djangojs.po
.它正在创建
djangojs.po
。
The strings appearing in this file are all from *.js files in my static folder.此文件中出现的字符串均来自我的静态文件夹中的 *.js 文件。
But how do i have my strings used in alerts and confirms that are written in my templates.但是我如何在警报中使用我的字符串并确认写在我的模板中。
here are snippets from my templates.这是我模板中的片段。
<script>
if($('#id_action').val()=='DEL'){
if(confirm(gettext('Are you sure you want to delete selected author(s) ?'))){
flag_action=true;
}
}
</script>
In my template i also have something like this which is not appearing in po files either.在我的模板中,我也有类似的东西,它也没有出现在 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>
The string in gettext is not appearing in any po. gettext 中的字符串未出现在任何 po 中。
I have included the following in my templates我在我的模板中包含了以下内容
<script type="text/javascript" src="{% url 'javascript-catalog' %}"></script>
Use this
用这个
var alert_var = {% blocktrans %}"Are you sure you want to delete selected author(s) ?" {% endblocktrans %};
<!-- begin snippet: js hide: false console: true babel: false -->
then use this variable where ever you need
然后在任何需要的地方使用这个变量
As Arhot said {% blocktrans %}"Text..."{% endblocktrans %}
works perfectly.正如 Arhot 所说
{% blocktrans %}"Text..."{% endblocktrans %}
完美运行。
If you use this inside the <script>
tags in the html file, the words ids will be generated in the file .po
corresponding to html traslations and not javascript traslations.如果您在 html 文件中的
<script>
标签中使用它,单词 ids 将在文件.po
生成,对应于 html traslations 而不是 javascript traslations。 I mean you will find the new words in the file django.po
and not inside the djangojs.po
.我的意思是你会在文件
django.po
而不是在djangojs.po
找到新词。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.