views:

45

answers:

2

Hello ! 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 ?

A: 

The mechanism is different for javascript files. The translations are not generated into the regular po file but into a javascript catalog.

Look at this explanation in the Django book.

I hope it helps

luc
They say to use the command "django-admin.py makemessages -d djangojs -l [lang_code]". I did this and it generated a djangojs.po file (a normal po file) but it has not all the strings, only two of them.
Marc Demierre
I added precisions to avoid misunderstandings.
Marc Demierre
A: 

I've just solved the problem of strings not translated at runtime. It came from the fact that my "locale" directory was under the project root. I add to add "my_project_root_dir" to settings.INSTALLED_APPS to receive a correct javascript catalog.

For the problem of not detected strings, I still have no idea about how to make django makemessages find all the strings but I have a temporary solution. I added the strings to the mo file manually and it works. However, django makemessages remove the strings so it can't be used anymore.

Marc Demierre