Django 1.2.3——国际化——makemessages并没有检测到所有字符串。

时间:2021-08-21 12:42:52

One more question about Django concerning localization of javascript files.

关于Django关于javascript文件本地化的另一个问题。

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

Django提供了一个小而方便的javascript库,可以像gettext一样在javascript文件中国际化字符串。

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.

我成功地设置了它(至少插值函数可以工作),并且我可以为法语生成po文件。然而,并不是所有的字符串都被检测到。我不知道为什么,因为它们看起来都一样。我在Django Trac和官方文档中找不到任何东西。

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.

javascript代码位于模板中包含的一个外部文件中,Django显然找到了它,因为它在po文件中放入了两个字符串。

The inclusion in the HTML template :

在HTML模板中包含:

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

The javascript code :

javascript代码:

/* ---------------
 * 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.

第一部分是jquery的标准使用。JQuery的uploadprogress模块和第二部分只是确认弹出的两个函数。

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.

我使用了“django-admin”命令。py -d djangojs -l fr"它生成了一个djangojs。有这两个字符串的po文件。我翻译它们。不幸的是,它们在运行时没有被翻译。看来我终于有两个问题了。

Any idea ?

任何想法?

3 个解决方案

#1


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? :)

Django的Javascript消息解析非常脆弱。我已经写了为什么会这样的细节。我还有一个关于Django 1.3的修正,是关于Django 7704的。Django可能不接受这个补丁,也许您可以帮助他们解释为什么他们应该接受?:)

#2


1  

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.

我刚刚解决了在运行时不翻译字符串的问题。这是因为我的“locale”目录在项目根目录下。我必须在设置中添加“my_project_root_dir”。INSTALLED_APPS接收正确的javascript目录。

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.

对于未检测到的字符串的问题,我仍然不知道如何让django makemessage找到所有的字符串,但是我有一个临时的解决方案。我手动将字符串添加到mo文件中,它可以工作。然而,django makemessages删除了字符串,因此不能再使用它。

#3


0  

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

javascript文件的机制是不同的。翻译不是生成到常规的po文件中,而是生成到javascript目录中。

Look at this explanation in the Django book.

看看《被解救的姜戈》一书中的解释。

I hope it helps

我希望这有助于

#1


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? :)

Django的Javascript消息解析非常脆弱。我已经写了为什么会这样的细节。我还有一个关于Django 1.3的修正,是关于Django 7704的。Django可能不接受这个补丁,也许您可以帮助他们解释为什么他们应该接受?:)

#2


1  

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.

我刚刚解决了在运行时不翻译字符串的问题。这是因为我的“locale”目录在项目根目录下。我必须在设置中添加“my_project_root_dir”。INSTALLED_APPS接收正确的javascript目录。

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.

对于未检测到的字符串的问题,我仍然不知道如何让django makemessage找到所有的字符串,但是我有一个临时的解决方案。我手动将字符串添加到mo文件中,它可以工作。然而,django makemessages删除了字符串,因此不能再使用它。

#3


0  

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

javascript文件的机制是不同的。翻译不是生成到常规的po文件中,而是生成到javascript目录中。

Look at this explanation in the Django book.

看看《被解救的姜戈》一书中的解释。

I hope it helps

我希望这有助于