Search code examples
javascriptpythondjangointernationalizationgettext

Django 1.2.3 - Internationalization - makemessages does not detect all strings


One more question about Django concerning localization of javascript files.

Django provides a small and convenient javascript library which is used like gettext to internationalize strings in javascript files.

I set it up successfully (at least the interpolate function works) and I could generate the po file for the French language. However, not all strings are detected. I don't really know why because they all look the same. I couldn't find anything on the Django Trac and the official docs.

The javascript code is in an external file included in the template and Django apparently found it because it put two strings in the po file.

The inclusion in the HTML template :

<script src="{{MEDIA_URL|default:'/media/'}}js/list.js" type="text/javascript"></script>

The javascript code :

/* ---------------
 * Upload progress
 * --------------- */
$(document).ready(function() {
    $(function() {
        $('#upload_form').uploadProgress({
            //...

            /* function called just before starting the upload */
            start: function() {
                $("#upload_form").hide();
                filename = $("#id_file").val().split(/[\/\\]/).pop();
                fmts = gettext('Uploading %(filename)s...');
                dat = {
                    filename: filename
                };
                s = interpolate(fmts,dat,true);
                $("#progress_filename").html(s);
                $("#progress_container").show();
            },

            /* function called each time bar is updated */
            uploading: function(upload) {
                if (upload.percents >= 100) {
                    window.clearTimeout(this.timer);
                    fmts = gettext("Saving %(filename)s...");
                    dat = {
                        filename: filename
                    };
                    s = interpolate(fmts,dat,true);
                    $("#progress_filename").html(s);
                } else {
                    fmts = gettext('Uploading %(filename)s : %(percents)s%...');
                    dat = {
                        filename: filename,
                        percents: upload.percents
                    };
                    s = interpolate(fmts,dat,true);
                    $("#progress_filename").html(s);
                }
            },

            //...

        });
    });
});


/* --------------------
 * Confirmation dialogs
 * -------------------- */
function delVid(title) {
    fmts = gettext('Do you really want to delete the video "%(title)s"?');
    dat = {
        title: title
    };
    s = interpolate(fmts,dat,true);
    return confirm(s)
}

function abortVid(title) {
    fmts = gettext('Do you really want to abort the processing of the video "%(title)s"?');
    dat = {
        title: title
    };
    s = interpolate(fmts,dat,true);
    return confirm(s)
}

The first part is a standard use of the jquery.uploadprogress module for JQuery and the second part is just two functions for confirmation popups.

The detected strings are both in the first part :

  • 'Uploading %(filename)s...'
  • 'Saving %(filename)s...'

I used the command "django-admin.py -d djangojs -l fr" and it generated a djangojs.po file with these two strings. I translated them. Unfortunately, they are not translated at runtime. It seems that I have two problems finally.

Any idea ?


Solution

  • Django's Javascript message parsing is quite fragile. I've written up the details why this is so. I also have a fix for Django 1.3 attached to Django ticket 7704, which was accepted in 2012.