简体   繁体   English

Django JavaScript 未定义翻译gettext

[英]Django JavaScript Translation gettext is not defined

My javascript function contains the following:我的 javascript function 包含以下内容:

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

My urls.py looks like:我的 urls.py 看起来像:

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')),
]

And in my template I have:在我的模板中,我有:

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

The translation above using gettext() does not work.上面使用 gettext() 的翻译不起作用。 A Reference Error comes up saying gettext() is not defined.出现参考错误,指出未定义 gettext()。 However, in the same javascript file i have:但是,在同一个 javascript 文件中,我有:

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.月份翻译有效,但示例无效。

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.请看,如果您在加载 javascript-catalog 的脚本标记之前使用上述示例的 gettext() 函数。

I literally had the same exact issue, even when I had the script tag come before the gettext().我确实遇到了完全相同的问题,即使我在 gettext() 之前有 script 标签。

The simple fix was to make sure the catalog came before the app in urlpatterns:简单的修复是确保目录在 urlpatterns 中的应用程序之前:

urlpatterns = [

    path('jsi18n/', JavaScriptCatalog.as_view(), name='javascript-catalog'),
     ...
    <your app>
]

Here is complete guideline to add Admin Date Picker in Front end.这是在前端添加管理日期选择器的完整指南。

forms.py forms.py

from django.contrib.admin import widgets
from django import forms
from .models import Student

class StudentForm(forms.ModelForm):

    class Media:
        css = {
            'all': (
                '/static/admin/css/widgets.css',
            )
        }
        js = [
            # '/admin/jsi18n/',
            '/static/admin/js/core.js',
        ]
    
    class Meta:
        model = Student
        fields = [
            "first_name",  
            "last_name", 
            "birth_date", 
        ]
        widgets = {
            'birth_date': widgets.AdminDateWidget()
        }

urls.py网址.py

from django.views.i18n import JavaScriptCatalog
from django.urls import path

urlpatterns = [
    path('jsi18n/', JavaScriptCatalog.as_view(), name='javascript-catalog'),
]

add_student.html add_student.html

<form action="{% url 'some_url' %}" method="post">
    {% csrf_token %}
    <table>
        {{ form.as_table }}
    </table>
    <input type="submit" value="Submit">
</form>

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

add this before jquery block.在 jquery 块之前添加它。 Hope this might help希望这可能会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM