2017-08-28 4 views
1

私のDjangoプロジェクトでは、私は翻訳する必要があるテキスト付きのJSファイルを持っています。私は次のコードを使用します。 makemessagesの節compilemessages私は djangojs.po を作成コマンドと djangojs.moファイルの助けを借りてJSコードでgettextを正しく使うには?

。問題は、gettextを追加した後、JSコードが正しく動作しないことです。ブラウザのコンソールでエラーが発生します。この問題を解決するには?

urls.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); 
}); 

ERROR:

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

答えて

1

jsスクリプトの前にbase.htmlに<script src="/jsi18n/"></script>が追加されていますか?

+1

あなたは絶対に正しいです!わたしにはできる。ありがとうございました! =) –

0

folloを作ってみますあなたの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を作成してください。

それとも

    は、システムの言語設定を変更し、それが動作するかどうかを確認
  • 。つまり、あなたのランゲージはまだサポートされていません。
+0

私もそれを試しましたが、残念ながら仕事をしません。他のアイデアはありますか? –

関連する問題