关于 Django 关于 javascript 文件本地化的问题。
Django 提供了一个小而方便的 javascript 库,它像 gettext 一样用于将 javascript 文件中的字符串国际化。
我成功地设置了它(至少插值函数有效)并且我可以生成法语的 po 文件。但是,并非所有字符串都被检测到。我真的不知道为什么,因为它们看起来都一样。我在 Django Trac 和官方文档中找不到任何内容。
javascript 代码位于模板中包含的外部文件中,Django 显然找到了它,因为它在 po 文件中放入了两个字符串。
包含在 HTML 模板中:
<script src="{{MEDIA_URL|default:'/media/'}}js/list.js" type="text/javascript"></script>
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)
}
第一部分是 JQuery 的 jquery.uploadprogress 模块的标准使用,第二部分只是确认弹出窗口的两个函数。
检测到的字符串都在第一部分:
- '正在上传 %(filename)s...'
- “正在保存 %(filename)s...”
我使用命令“django-admin.py -d djangojs -l fr”,它生成了一个包含这两个字符串的 djangojs.po 文件。我翻译了它们。不幸的是,它们不会在运行时被翻译。看来我终于有两个问题了。
有什么想法吗?
最佳答案
Django 的 Javascript 消息解析非常脆弱。我有 written up the details为什么会这样。我还有一个附加到 Django ticket 7704 的 Django 1.3 修复程序, 2012 年被录用。
关于javascript - Django 1.2.3 - 国际化 - makemessages 不检测所有字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3879177/