Django JavaScript Translation gettext未定义 [英] Django JavaScript Translation gettext is not defined

查看:36
本文介绍了Django JavaScript Translation gettext未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的javascript函数包含以下内容:

My javascript function contains the following:

document.getElementById("example").innerHTML = gettext("This is an example");

我的urls.py如下:

My urls.py looks like:

urlpatterns = [
    url(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog'),
    url(r'^admin/', admin.site.urls),
    url(r'^', include('project.urls')),
    url(r'^login/$', auth_views.login, {'template_name': 'login.html', 'authentication_form': LoginForm}, name = 'login'),
    url(r'^logout/$', auth_views.logout, {'next_page': '/login'}),
    url(r'^i18n/', include('django.conf.urls.i18n')),
]

在我的模板中,我有:

<script type="text/javascript" src="{% url 'javascript-catalog' %}"></script>

上面使用gettext()进行的翻译不起作用.出现参考错误,提示未定义gettext().但是,在同一个javascript文件中,我有:

The translation above using gettext() does not work. A Reference Error comes up saying gettext() is not defined. However, in the same javascript file i have:

var monthNames =  [gettext("January"), gettext("February"), gettext("March"), gettext("April"), gettext("May"), gettext("June"), gettext("July"), gettext("August"), gettext("September"), gettext("October"), gettext("November"), gettext("December")];

并且不会提示参考错误.月份翻译有效,但示例翻译无效.

And that does not prompt a reference error. The month translations work but the example one does not.

推荐答案

我不确定,但是请尝试检查脚本的顺序.请参阅,如果您在加载javascript-catalog的script标记之前使用了上面的示例的gettext()函数.

I am not sure, but try checking the order of your script. See, if you are using the gettext() function for the example above before the script tag where you load the javascript-catalog.

这篇关于Django JavaScript Translation gettext未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆