1

在我的 Django 项目中,我有需要翻译的带有文本的 JS 文件。我在下面使用下一个代码。

在makemessagescompilemessages命令的帮助下,我创建了djangojs.podjangojs.mo文件。问题是添加gettext后 JS 代码无法正常工作。它在浏览器控制台中引发错误。如何解决这个问题?

网址.py:

from django.views.i18n import javascript_catalog

js_info_dict = {
    'domain': 'djangojs',
    'packages': ('slider',),  # my app name
}

urlpatterns = [
   [OTHER URLs]

    # Internationalization in Javascript Code
    url(r'^jsi18n/$',
        javascript_catalog,
        js_info_dict,
        name='javascript-catalog'),
]

JS:

$("#slideModalBox").on("click", "#imageFieldClearBtn", function() {
    $('#imageFieldFileName').val("");
    $('#imageFieldClearBtn').hide();
    $('#imageFieldInput input:file').val("");
    $("#imageFieldInputTitle").text(gettext("Add new image"););
});

$("#slideModalBox").on("change", "#imageFieldInput input:file", function() {
    var file = this.files[0];
    var reader = new FileReader();
    reader.onload = function (e) {
        $("#imageFieldInputTitle").text(gettext("Change Image")); <-- This place raise error
        $("#imageFieldClearBtn").show();
        $("#imageFieldFileName").val(file.name);            
    }
    reader.readAsDataURL(file);
});

浏览器控制台中的错误:

ReferenceError: gettext is not defined
reader.onload http://127.0.0.1:8000/static/js/slider/image_field.js:12:9
4

2 回答 2

2

您是否<script src="/jsi18n/"></script>在 js 脚本之前添加了 base.html?

于 2017-08-28T14:40:56.503 回答
0

尝试在您的 urls.py 中执行以下命令

urlpatterns = [

 # Internationalization in Javascript Code
    url(r'^jsi18n/$',
    javascript_catalog,
    js_info_dict,
    name='javascript-catalog'),

# then the other urls
   [OTHER URLs]
]

基本上,先制作 javascript_catalog 。

或者

  • 更改您的系统语言设置,看看它是否有效。这意味着您的语言尚不支持
于 2017-08-28T12:54:06.350 回答