PDF editor: Allow multi-lingual text

This commit is contained in:
Raphael Michel
2022-03-13 15:06:18 +01:00
committed by Raphael Michel
parent 02f8bcbe23
commit ebae275a2d
3 changed files with 35 additions and 2 deletions

View File

@@ -55,6 +55,7 @@ from django.utils.functional import SimpleLazyObject
from django.utils.html import conditional_escape from django.utils.html import conditional_escape
from django.utils.timezone import now from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from i18nfield.strings import LazyI18nString
from PyPDF2 import PdfFileReader from PyPDF2 import PdfFileReader
from pytz import timezone from pytz import timezone
from reportlab.graphics import renderPDF from reportlab.graphics import renderPDF
@@ -660,8 +661,11 @@ class Renderer:
if not o['content']: if not o['content']:
return '(error)' return '(error)'
if o['content'] == 'other': if o['content'] == 'other' or o['content'] == 'other_i18n':
text = o['text'] if o['content'] == 'other_i18n':
text = str(LazyI18nString(o['text_i18n']))
else:
text = o['text']
def replace(x): def replace(x):
if x.group(1) not in self.variables: if x.group(1) not in self.variables:

View File

@@ -384,10 +384,16 @@
{% trans "Item attribute:" %} {{ p.name }} {% trans "Item attribute:" %} {{ p.name }}
</option> </option>
{% endfor %} {% endfor %}
<option value="other_i18n">{% trans "Other… (multilingual)" %}</option>
<option value="other">{% trans "Other…" %}</option> <option value="other">{% trans "Other…" %}</option>
</select> </select>
<textarea type="text" value="" class="input-block-level form-control" <textarea type="text" value="" class="input-block-level form-control"
id="toolbox-content-other"></textarea> id="toolbox-content-other"></textarea>
<div class="i18n-form-group" id="toolbox-content-other-i18n">
{% for l in request.event.settings.locales %}
<textarea id="toolbox-content-other-{{ l }}" rows="3" class="input-block-level form-control" title="{{ l }}" lang="{{ l }}"></textarea>
{% endfor %}
</div>
</div> </div>
</div> </div>
</div> </div>

View File

@@ -169,6 +169,7 @@ var editor = {
downward: o.downward || false, downward: o.downward || false,
content: o.content, content: o.content,
text: o.text, text: o.text,
text_i18n: o.text_i18n || {},
rotation: o.angle, rotation: o.angle,
align: o.textAlign, align: o.textAlign,
}); });
@@ -246,6 +247,9 @@ var editor = {
} }
if (d.content === "other") { if (d.content === "other") {
o.setText(d.text); o.setText(d.text);
} else if (d.content === "other_i18n") {
o.text_i18n = d.text_i18n
o.setText(d.text_i18n[Object.keys(d.text_i18n)[0]]);
} else { } else {
o.setText(editor._get_text_sample(d.content)); o.setText(editor._get_text_sample(d.content));
} }
@@ -391,6 +395,7 @@ var editor = {
editor._update_toolbox(); editor._update_toolbox();
$("#toolbox-content-other").hide(); $("#toolbox-content-other").hide();
$("#toolbox-content-other-i18n").hide();
$(".add-buttons button").prop('disabled', false); $(".add-buttons button").prop('disabled', false);
if (dump) { if (dump) {
@@ -489,8 +494,13 @@ var editor = {
} }
$("#toolbox-content").val(o.content); $("#toolbox-content").val(o.content);
$("#toolbox-content-other").toggle($("#toolbox-content").val() === "other"); $("#toolbox-content-other").toggle($("#toolbox-content").val() === "other");
$("#toolbox-content-other-i18n").toggle($("#toolbox-content").val() === "other_i18n");
if (o.content === "other") { if (o.content === "other") {
$("#toolbox-content-other").val(o.text); $("#toolbox-content-other").val(o.text);
} else if (o.content === "other_i18n") {
$("#toolbox-content-other-i18n textarea").each(function () {
$(this).val(o.text_i18n[$(this).attr("lang")] || '');
});
} else { } else {
$("#toolbox-content-other").val(""); $("#toolbox-content-other").val("");
} }
@@ -526,9 +536,15 @@ var editor = {
o.nowhitespace = $("#toolbox-qrwhitespace").prop("checked") || false; o.nowhitespace = $("#toolbox-qrwhitespace").prop("checked") || false;
$("#toolbox-content-other").toggle($("#toolbox-content").val() === "other"); $("#toolbox-content-other").toggle($("#toolbox-content").val() === "other");
$("#toolbox-content-other-i18n").toggle($("#toolbox-content").val() === "other_i18n");
o.content = $("#toolbox-content").val(); o.content = $("#toolbox-content").val();
if ($("#toolbox-content").val() === "other") { if ($("#toolbox-content").val() === "other") {
o.text = $("#toolbox-content-other").val(); o.text = $("#toolbox-content-other").val();
} else if ($("#toolbox-content").val() === "other_i18n") {
o.text_i18n = {}
$("#toolbox-content-other-i18n textarea").each(function () {
o.text_i18n[$(this).attr("lang")] = $(this).val();
});
} else { } else {
o.text = editor._get_text_sample($("#toolbox-content").val()); o.text = editor._get_text_sample($("#toolbox-content").val());
} }
@@ -574,9 +590,16 @@ var editor = {
o.downward = $("#toolbox").find("button[data-action=downward]").is('.active'); o.downward = $("#toolbox").find("button[data-action=downward]").is('.active');
o.rotate(parseFloat($("#toolbox-textrotation").val())); o.rotate(parseFloat($("#toolbox-textrotation").val()));
$("#toolbox-content-other").toggle($("#toolbox-content").val() === "other"); $("#toolbox-content-other").toggle($("#toolbox-content").val() === "other");
$("#toolbox-content-other-i18n").toggle($("#toolbox-content").val() === "other_i18n");
o.content = $("#toolbox-content").val(); o.content = $("#toolbox-content").val();
if ($("#toolbox-content").val() === "other") { if ($("#toolbox-content").val() === "other") {
o.setText($("#toolbox-content-other").val()); o.setText($("#toolbox-content-other").val());
} else if ($("#toolbox-content").val() === "other_i18n") {
o.text_i18n = {}
$("#toolbox-content-other-i18n textarea").each(function () {
o.text_i18n[$(this).attr("lang")] = $(this).val();
});
o.setText($("#toolbox-content-other-i18n textarea").first().val());
} else { } else {
o.setText(editor._get_text_sample($("#toolbox-content").val())); o.setText(editor._get_text_sample($("#toolbox-content").val()));
} }