Django 1.2.3 - Internationalization - makemessages does not detect all strings

[亡魂溺海] 提交于 2019-12-06 11:37:55

问题


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 ?


回答1:


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. Django may not accept the patch, maybe you can help explain to them why they should? :)




回答2:


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 had 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.




回答3:


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



来源:https://stackoverflow.com/questions/3879177/django-1-2-3-internationalization-makemessages-does-not-detect-all-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!