wagtail-sidebyside 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- wagtail_sidebyside/__init__.py +0 -0
- wagtail_sidebyside/action_menu.py +57 -0
- wagtail_sidebyside/apps.py +13 -0
- wagtail_sidebyside/conf.py +16 -0
- wagtail_sidebyside/fields.py +406 -0
- wagtail_sidebyside/forms.py +170 -0
- wagtail_sidebyside/locale/fr/LC_MESSAGES/django.mo +0 -0
- wagtail_sidebyside/locale/fr/LC_MESSAGES/django.po +637 -0
- wagtail_sidebyside/locale/nl/LC_MESSAGES/django.mo +0 -0
- wagtail_sidebyside/locale/nl/LC_MESSAGES/django.po +633 -0
- wagtail_sidebyside/management/__init__.py +0 -0
- wagtail_sidebyside/management/commands/__init__.py +0 -0
- wagtail_sidebyside/management/commands/import_localize_drafts.py +64 -0
- wagtail_sidebyside/managers.py +35 -0
- wagtail_sidebyside/migrations/0001_initial.py +40 -0
- wagtail_sidebyside/migrations/0002_translationsettings.py +53 -0
- wagtail_sidebyside/migrations/0003_drop_wagtail_localize.py +11 -0
- wagtail_sidebyside/migrations/0004_removed_translations.py +17 -0
- wagtail_sidebyside/migrations/0005_instructions_help_text.py +21 -0
- wagtail_sidebyside/migrations/0006_ai_usage.py +46 -0
- wagtail_sidebyside/migrations/0007_awaiting_source.py +17 -0
- wagtail_sidebyside/migrations/__init__.py +0 -0
- wagtail_sidebyside/models.py +103 -0
- wagtail_sidebyside/panels.py +24 -0
- wagtail_sidebyside/signals.py +16 -0
- wagtail_sidebyside/static/sidebyside/sidebyside.css +533 -0
- wagtail_sidebyside/static/sidebyside/translate.js +143 -0
- wagtail_sidebyside/templates/sidebyside/create.html +22 -0
- wagtail_sidebyside/templates/sidebyside/dashboard_panel.html +21 -0
- wagtail_sidebyside/templates/sidebyside/includes/object_preview.html +11 -0
- wagtail_sidebyside/templates/sidebyside/includes/translation_bar.html +27 -0
- wagtail_sidebyside/templates/sidebyside/includes/translation_bar_button.html +7 -0
- wagtail_sidebyside/templates/sidebyside/overview.html +47 -0
- wagtail_sidebyside/templates/sidebyside/panels/machine_translation_status.html +3 -0
- wagtail_sidebyside/templates/sidebyside/translate.html +236 -0
- wagtail_sidebyside/templates/sidebyside/translators/claude_status.html +64 -0
- wagtail_sidebyside/templates/sidebyside/translators/status.html +9 -0
- wagtail_sidebyside/templates/sidebyside/translators/usage.html +32 -0
- wagtail_sidebyside/templates/wagtailadmin/pages/edit.html +12 -0
- wagtail_sidebyside/templates/wagtailadmin/pages/explorable_index.html +12 -0
- wagtail_sidebyside/templatetags/__init__.py +0 -0
- wagtail_sidebyside/templatetags/sidebyside_tags.py +64 -0
- wagtail_sidebyside/translators/__init__.py +68 -0
- wagtail_sidebyside/translators/claude.py +233 -0
- wagtail_sidebyside/use_cases/__init__.py +0 -0
- wagtail_sidebyside/use_cases/machine_translate.py +86 -0
- wagtail_sidebyside/use_cases/page_status.py +99 -0
- wagtail_sidebyside/use_cases/sync_tree.py +83 -0
- wagtail_sidebyside/use_cases/translate_page.py +359 -0
- wagtail_sidebyside/views.py +323 -0
- wagtail_sidebyside/wagtail_hooks.py +161 -0
- wagtail_sidebyside-0.1.0.dist-info/METADATA +270 -0
- wagtail_sidebyside-0.1.0.dist-info/RECORD +55 -0
- wagtail_sidebyside-0.1.0.dist-info/WHEEL +4 -0
- wagtail_sidebyside-0.1.0.dist-info/licenses/LICENSE +21 -0
|
File without changes
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""Footer actions of the translation screen, on top of Wagtail's own."""
|
|
2
|
+
|
|
3
|
+
from django.utils.translation import gettext_lazy as _
|
|
4
|
+
from wagtail.admin.action_menu import (
|
|
5
|
+
ActionMenuItem,
|
|
6
|
+
CancelWorkflowMenuItem,
|
|
7
|
+
PublishMenuItem,
|
|
8
|
+
RestartWorkflowMenuItem,
|
|
9
|
+
SubmitForModerationMenuItem,
|
|
10
|
+
WorkflowMenuItem,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class PublishTogetherMenuItem(ActionMenuItem):
|
|
15
|
+
"""Publish the source's draft and the translation at once."""
|
|
16
|
+
|
|
17
|
+
name = "action-publish-together"
|
|
18
|
+
label = _("Publish source and translation")
|
|
19
|
+
icon_name = "upload"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class PublishTranslationOnlyMenuItem(PublishMenuItem):
|
|
23
|
+
label = _("Publish translation only")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def adapt(action_menu, source_page, user):
|
|
27
|
+
"""Workflow actions are left out: the translation screen does not handle
|
|
28
|
+
moderation, so they would only save a draft. When the source has
|
|
29
|
+
unpublished changes, "Publish" publishes the translation only (against
|
|
30
|
+
the published source), with "Publish source and translation" just above
|
|
31
|
+
it. A source that was never published can only be published together
|
|
32
|
+
with its translation."""
|
|
33
|
+
items = action_menu.menu_items
|
|
34
|
+
items[:] = [
|
|
35
|
+
item
|
|
36
|
+
for item in items
|
|
37
|
+
if not isinstance(
|
|
38
|
+
item,
|
|
39
|
+
(
|
|
40
|
+
SubmitForModerationMenuItem,
|
|
41
|
+
WorkflowMenuItem,
|
|
42
|
+
RestartWorkflowMenuItem,
|
|
43
|
+
CancelWorkflowMenuItem,
|
|
44
|
+
),
|
|
45
|
+
)
|
|
46
|
+
]
|
|
47
|
+
if not source_page.has_unpublished_changes:
|
|
48
|
+
return
|
|
49
|
+
for index, item in enumerate(items):
|
|
50
|
+
if isinstance(item, PublishMenuItem):
|
|
51
|
+
if source_page.live:
|
|
52
|
+
items[index] = PublishTranslationOnlyMenuItem(order=item.order)
|
|
53
|
+
else:
|
|
54
|
+
del items[index]
|
|
55
|
+
if source_page.permissions_for_user(user).can_publish():
|
|
56
|
+
items.insert(index, PublishTogetherMenuItem())
|
|
57
|
+
return
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from django.apps import AppConfig
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class SideBySideConfig(AppConfig):
|
|
5
|
+
default_auto_field = "django.db.models.BigAutoField"
|
|
6
|
+
name = "wagtail_sidebyside"
|
|
7
|
+
label = "sidebyside"
|
|
8
|
+
verbose_name = "Side-by-side translations"
|
|
9
|
+
|
|
10
|
+
def ready(self):
|
|
11
|
+
from . import signals
|
|
12
|
+
|
|
13
|
+
signals.connect()
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from django.conf import settings
|
|
2
|
+
from wagtail.models import Locale
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def config() -> dict:
|
|
6
|
+
"""The SIDEBYSIDE setting (a dict), see the README."""
|
|
7
|
+
return getattr(settings, "SIDEBYSIDE", {})
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def source_locale() -> Locale:
|
|
11
|
+
"""The main language of the site. Pages are created in this locale only,
|
|
12
|
+
every other locale is a translation of it."""
|
|
13
|
+
code = config().get("SOURCE_LOCALE")
|
|
14
|
+
if code:
|
|
15
|
+
return Locale.objects.get_for_language(code)
|
|
16
|
+
return Locale.get_default()
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
"""Which parts of a page are translatable, and how to write translations back.
|
|
2
|
+
|
|
3
|
+
A page is flattened into units: one per translatable text (page field or
|
|
4
|
+
StreamField leaf block), keyed by a stable path such as "title" or
|
|
5
|
+
"body.<block-id>.caption", plus one per image or document, which follows the
|
|
6
|
+
source unless the translation picks its own. Everything else (choices, plugin
|
|
7
|
+
config, ...) is copied from the source page when a translation is saved.
|
|
8
|
+
|
|
9
|
+
Fields listed in a page model's `translation_exclude` always come from the
|
|
10
|
+
source, e.g. translation_exclude = ["header_image"].
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import difflib
|
|
14
|
+
import functools
|
|
15
|
+
from dataclasses import dataclass
|
|
16
|
+
|
|
17
|
+
from django.db import models
|
|
18
|
+
from django.utils.text import capfirst
|
|
19
|
+
from wagtail import blocks, hooks
|
|
20
|
+
from wagtail.documents import get_document_model
|
|
21
|
+
from wagtail.documents.blocks import DocumentChooserBlock
|
|
22
|
+
from wagtail.fields import RichTextField, StreamField
|
|
23
|
+
from wagtail.images import get_image_model
|
|
24
|
+
from wagtail.images.blocks import ImageChooserBlock
|
|
25
|
+
from wagtail.models import Page
|
|
26
|
+
|
|
27
|
+
TEXT = "text"
|
|
28
|
+
TEXTAREA = "textarea"
|
|
29
|
+
RICHTEXT = "richtext"
|
|
30
|
+
HTML = "html"
|
|
31
|
+
TEXT_KINDS = {TEXT, TEXTAREA, RICHTEXT, HTML}
|
|
32
|
+
# An image or document (value: its id), the source's unless overridden.
|
|
33
|
+
IMAGE = "image"
|
|
34
|
+
DOCUMENT = "document"
|
|
35
|
+
CHOOSER_KINDS = {IMAGE, DOCUMENT}
|
|
36
|
+
# Marker for a block without translatable text, shown as a grey row.
|
|
37
|
+
SYNCED = "synced"
|
|
38
|
+
|
|
39
|
+
# Page fields that are translated; always shown, even when empty in the source
|
|
40
|
+
# (so is every field of the metadata section), as the translation may fill them.
|
|
41
|
+
PAGE_FIELDS = ["title", "slug", "seo_title", "search_description"]
|
|
42
|
+
# Page fields copied from the source. Other Page fields are tree, workflow or
|
|
43
|
+
# publishing state that belongs to the translated page itself.
|
|
44
|
+
SYNCED_PAGE_FIELDS = ["show_in_menus"]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# Sections of the translation screen: the editor's first tab, and the rest
|
|
48
|
+
# (slug, search engine fields: Wagtail's "Promote" tab).
|
|
49
|
+
CONTENT = "content"
|
|
50
|
+
METADATA = "metadata"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass
|
|
54
|
+
class Unit:
|
|
55
|
+
path: str
|
|
56
|
+
label: str
|
|
57
|
+
kind: str
|
|
58
|
+
source: str
|
|
59
|
+
features: list[str] | None = None
|
|
60
|
+
section: str = CONTENT
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def page_units(page: Page, align_to: Page | None = None) -> list[Unit]:
|
|
64
|
+
"""Units of page. With align_to, StreamField blocks get the paths of the
|
|
65
|
+
matching blocks in align_to (see _align)."""
|
|
66
|
+
units = []
|
|
67
|
+
tabs = _panel_tabs(type(page))
|
|
68
|
+
for field in _translatable_fields(page):
|
|
69
|
+
value = getattr(page, field.name)
|
|
70
|
+
label = capfirst(field.verbose_name)
|
|
71
|
+
count = len(units)
|
|
72
|
+
if isinstance(field, StreamField):
|
|
73
|
+
raw = _raw(field, value)
|
|
74
|
+
if align_to is not None:
|
|
75
|
+
raw = _align(
|
|
76
|
+
field.stream_block, _raw(field, getattr(align_to, field.name)), raw
|
|
77
|
+
)
|
|
78
|
+
_walk(field.stream_block, raw, field.name, label, units, {})
|
|
79
|
+
elif isinstance(field, models.ForeignKey):
|
|
80
|
+
object_id = getattr(page, field.attname)
|
|
81
|
+
units.append(
|
|
82
|
+
Unit(field.name, label, _field_kind(field), str(object_id or ""))
|
|
83
|
+
)
|
|
84
|
+
elif value or field.name in PAGE_FIELDS or tabs.get(field.name, 0) > 0:
|
|
85
|
+
units.append(
|
|
86
|
+
Unit(
|
|
87
|
+
field.name,
|
|
88
|
+
label,
|
|
89
|
+
_field_kind(field),
|
|
90
|
+
value or "",
|
|
91
|
+
getattr(field, "features", None),
|
|
92
|
+
)
|
|
93
|
+
)
|
|
94
|
+
if tabs.get(field.name, 0) > 0:
|
|
95
|
+
for unit in units[count:]:
|
|
96
|
+
unit.section = METADATA
|
|
97
|
+
return units
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def page_texts(page: Page, align_to: Page | None = None) -> dict[str, str]:
|
|
101
|
+
"""path -> value of every unit: texts, and image/document ids."""
|
|
102
|
+
return {
|
|
103
|
+
unit.path: unit.source
|
|
104
|
+
for unit in page_units(page, align_to)
|
|
105
|
+
if unit.kind != SYNCED
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def apply_translations(source: Page, target: Page, values: dict[str, str]) -> Page:
|
|
110
|
+
"""Make target a copy of source, with values (path -> text) replacing the
|
|
111
|
+
source text of translated units. Untranslated units keep the source text."""
|
|
112
|
+
translatable = {field.name for field in _translatable_fields(source)}
|
|
113
|
+
for field in _content_fields(source):
|
|
114
|
+
if field.name not in translatable:
|
|
115
|
+
setattr(target, field.attname, getattr(source, field.attname))
|
|
116
|
+
elif isinstance(field, StreamField):
|
|
117
|
+
raw = _walk(
|
|
118
|
+
field.stream_block,
|
|
119
|
+
_raw(field, getattr(source, field.name)),
|
|
120
|
+
field.name,
|
|
121
|
+
"",
|
|
122
|
+
[],
|
|
123
|
+
values,
|
|
124
|
+
)
|
|
125
|
+
setattr(target, field.name, field.stream_block.to_python(raw))
|
|
126
|
+
elif isinstance(field, models.ForeignKey):
|
|
127
|
+
value = values.get(field.name)
|
|
128
|
+
setattr(
|
|
129
|
+
target,
|
|
130
|
+
field.attname,
|
|
131
|
+
int(value) if value else getattr(source, field.attname),
|
|
132
|
+
)
|
|
133
|
+
elif field.name == "slug" and "slug" not in values:
|
|
134
|
+
# An untranslated slug follows the source slug, unless a sibling
|
|
135
|
+
# already has it (e.g. site roots, which share a parent).
|
|
136
|
+
siblings = target.get_parent().get_children().exclude(pk=target.pk)
|
|
137
|
+
if not siblings.filter(slug=source.slug).exists():
|
|
138
|
+
target.slug = source.slug
|
|
139
|
+
else:
|
|
140
|
+
setattr(
|
|
141
|
+
target, field.name, values.get(field.name, getattr(source, field.name))
|
|
142
|
+
)
|
|
143
|
+
return target
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def _content_fields(page: Page):
|
|
147
|
+
"""Page fields plus all fields of the specific page model."""
|
|
148
|
+
yield from (page._meta.get_field(name) for name in PAGE_FIELDS + SYNCED_PAGE_FIELDS)
|
|
149
|
+
for field in page._meta.concrete_fields:
|
|
150
|
+
if field.model is not Page and not field.primary_key:
|
|
151
|
+
yield field
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _translatable_fields(page: Page):
|
|
155
|
+
"""Translatable fields in the order of the page editor's panels (fields
|
|
156
|
+
without a panel last)."""
|
|
157
|
+
exclude = set(getattr(page, "translation_exclude", []))
|
|
158
|
+
order = _panel_order(type(page))
|
|
159
|
+
fields = [
|
|
160
|
+
field
|
|
161
|
+
for field in _content_fields(page)
|
|
162
|
+
if field.name in PAGE_FIELDS
|
|
163
|
+
or (field.name not in exclude and _is_translatable(field))
|
|
164
|
+
]
|
|
165
|
+
return sorted(fields, key=lambda field: order.get(field.name, len(order)))
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
@functools.cache
|
|
169
|
+
def _panel_order(model) -> dict[str, int]:
|
|
170
|
+
"""Field name -> position in the page editor (tabs and panels in order)."""
|
|
171
|
+
names = [name for tab in _editor_tabs(model) for name in tab]
|
|
172
|
+
return {name: index for index, name in enumerate(names)}
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
@functools.cache
|
|
176
|
+
def _panel_tabs(model) -> dict[str, int]:
|
|
177
|
+
"""Field name -> index of its tab in the page editor."""
|
|
178
|
+
return {
|
|
179
|
+
name: index for index, tab in enumerate(_editor_tabs(model)) for name in tab
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _editor_tabs(model) -> list[list[str]]:
|
|
184
|
+
"""Field names per tab of the page editor, in order."""
|
|
185
|
+
|
|
186
|
+
def walk(panel):
|
|
187
|
+
if getattr(panel, "field_name", None):
|
|
188
|
+
yield panel.field_name
|
|
189
|
+
for child in getattr(panel, "children", []):
|
|
190
|
+
yield from walk(child)
|
|
191
|
+
|
|
192
|
+
return [list(walk(tab)) for tab in model.get_edit_handler().children]
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def _is_translatable(field) -> bool:
|
|
196
|
+
if isinstance(field, (StreamField, RichTextField)):
|
|
197
|
+
return True
|
|
198
|
+
if isinstance(field, models.ForeignKey):
|
|
199
|
+
return field.related_model in (get_image_model(), get_document_model())
|
|
200
|
+
if field.choices or isinstance(field, (models.EmailField, models.URLField)):
|
|
201
|
+
return False
|
|
202
|
+
return isinstance(field, (models.CharField, models.TextField))
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def _field_kind(field) -> str:
|
|
206
|
+
if isinstance(field, models.ForeignKey):
|
|
207
|
+
return IMAGE if field.related_model is get_image_model() else DOCUMENT
|
|
208
|
+
if isinstance(field, RichTextField):
|
|
209
|
+
return RICHTEXT
|
|
210
|
+
if isinstance(field, models.TextField):
|
|
211
|
+
return TEXTAREA
|
|
212
|
+
return TEXT
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def _block_kind(block) -> str | None:
|
|
216
|
+
"""What a leaf block is for the translation screen: a text kind, IMAGE,
|
|
217
|
+
DOCUMENT, or None (copied from the source). Projects decide for their own
|
|
218
|
+
blocks with the "sidebyside_block_kind" hook: return a kind, False (not
|
|
219
|
+
translatable) or None (let the next hook or the default decide)."""
|
|
220
|
+
for fn in hooks.get_hooks("sidebyside_block_kind"):
|
|
221
|
+
kind = fn(block)
|
|
222
|
+
if kind is not None:
|
|
223
|
+
return kind or None
|
|
224
|
+
if isinstance(block, ImageChooserBlock):
|
|
225
|
+
return IMAGE
|
|
226
|
+
if isinstance(block, DocumentChooserBlock):
|
|
227
|
+
return DOCUMENT
|
|
228
|
+
if isinstance(block, blocks.RichTextBlock):
|
|
229
|
+
return RICHTEXT
|
|
230
|
+
if isinstance(block, blocks.RawHTMLBlock):
|
|
231
|
+
return HTML
|
|
232
|
+
if isinstance(block, blocks.TextBlock):
|
|
233
|
+
return TEXTAREA
|
|
234
|
+
if isinstance(block, blocks.CharBlock):
|
|
235
|
+
return TEXT
|
|
236
|
+
return None
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def _raw(field: StreamField, value) -> list:
|
|
240
|
+
return field.stream_block.get_prep_value(value)
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def _walk(block, raw, path: str, label: str, units: list, values: dict):
|
|
244
|
+
"""Walk raw (JSON) block data next to its block definition. Collects a Unit
|
|
245
|
+
per translatable leaf and returns the raw data with leaves replaced by
|
|
246
|
+
values[path] where given."""
|
|
247
|
+
if isinstance(block, blocks.StreamBlock):
|
|
248
|
+
result = []
|
|
249
|
+
for child in raw:
|
|
250
|
+
child_block = block.child_blocks.get(child["type"])
|
|
251
|
+
if child_block is None:
|
|
252
|
+
result.append(child)
|
|
253
|
+
continue
|
|
254
|
+
child_path = f"{path}.{child['id']}"
|
|
255
|
+
child_label = f"{label} > {child_block.label}"
|
|
256
|
+
count = len(units)
|
|
257
|
+
value = _walk(
|
|
258
|
+
child_block, child["value"], child_path, child_label, units, values
|
|
259
|
+
)
|
|
260
|
+
if len(units) == count:
|
|
261
|
+
units.append(Unit(child_path, child_label, SYNCED, ""))
|
|
262
|
+
result.append({**child, "value": value})
|
|
263
|
+
return result
|
|
264
|
+
|
|
265
|
+
if isinstance(block, blocks.ListBlock):
|
|
266
|
+
result = []
|
|
267
|
+
for index, item in enumerate(raw):
|
|
268
|
+
item_label = f"{label} {index + 1}"
|
|
269
|
+
if isinstance(item, dict) and item.get("type") == "item" and "id" in item:
|
|
270
|
+
value = _walk(
|
|
271
|
+
block.child_block,
|
|
272
|
+
item["value"],
|
|
273
|
+
f"{path}.{item['id']}",
|
|
274
|
+
item_label,
|
|
275
|
+
units,
|
|
276
|
+
values,
|
|
277
|
+
)
|
|
278
|
+
result.append({**item, "value": value})
|
|
279
|
+
else:
|
|
280
|
+
# Legacy list format without item ids.
|
|
281
|
+
result.append(
|
|
282
|
+
_walk(
|
|
283
|
+
block.child_block,
|
|
284
|
+
item,
|
|
285
|
+
f"{path}.{index}",
|
|
286
|
+
item_label,
|
|
287
|
+
units,
|
|
288
|
+
values,
|
|
289
|
+
)
|
|
290
|
+
)
|
|
291
|
+
return result
|
|
292
|
+
|
|
293
|
+
if isinstance(block, blocks.StructBlock):
|
|
294
|
+
return {
|
|
295
|
+
name: _walk(
|
|
296
|
+
block.child_blocks[name],
|
|
297
|
+
value,
|
|
298
|
+
f"{path}.{name}",
|
|
299
|
+
f"{label} > {block.child_blocks[name].label}",
|
|
300
|
+
units,
|
|
301
|
+
values,
|
|
302
|
+
)
|
|
303
|
+
if name in block.child_blocks
|
|
304
|
+
else value
|
|
305
|
+
for name, value in raw.items()
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
kind = _block_kind(block)
|
|
309
|
+
if kind in CHOOSER_KINDS:
|
|
310
|
+
units.append(Unit(path, label, kind, "" if raw is None else str(raw)))
|
|
311
|
+
return int(values[path]) if values.get(path) else raw
|
|
312
|
+
if kind is None or not isinstance(raw, str) or not raw.strip():
|
|
313
|
+
return raw
|
|
314
|
+
units.append(Unit(path, label, kind, raw, getattr(block, "features", None)))
|
|
315
|
+
return values.get(path, raw)
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def _align(block, source, target):
|
|
319
|
+
"""target raw data with block ids taken from source. Blocks are matched by
|
|
320
|
+
id, the rest by the longest matching sequences of block types (so moved or
|
|
321
|
+
inserted blocks do not shift everything after them). Translations normally
|
|
322
|
+
share block ids with their source (copy_for_translation keeps them), but
|
|
323
|
+
pages created per locale, e.g. by a fixture loader, have their own ids."""
|
|
324
|
+
if isinstance(block, (blocks.StreamBlock, blocks.ListBlock)):
|
|
325
|
+
if not all(isinstance(item, dict) and "id" in item for item in source + target):
|
|
326
|
+
return target # legacy list format, matched by position anyway
|
|
327
|
+
source_by_id = {item["id"]: item for item in source}
|
|
328
|
+
matches = {
|
|
329
|
+
index: source_by_id[item["id"]]
|
|
330
|
+
for index, item in enumerate(target)
|
|
331
|
+
if item["id"] in source_by_id
|
|
332
|
+
and source_by_id[item["id"]]["type"] == item["type"]
|
|
333
|
+
}
|
|
334
|
+
target_ids = {item["id"] for item in target}
|
|
335
|
+
rest_source = [item for item in source if item["id"] not in target_ids]
|
|
336
|
+
|
|
337
|
+
def is_text(item) -> bool:
|
|
338
|
+
child = _child_block(block, item)
|
|
339
|
+
return child is not None and _block_kind(child) in TEXT_KINDS
|
|
340
|
+
|
|
341
|
+
# Matching on type alone is ambiguous when blocks moved. Try all
|
|
342
|
+
# blocks in one sequence, and text blocks (heading, paragraph, ...)
|
|
343
|
+
# separately from the rest; keep whichever matches more text blocks,
|
|
344
|
+
# as the text is what must not get lost.
|
|
345
|
+
candidates = [
|
|
346
|
+
_pair(rest_source, target, matches, lambda item: True),
|
|
347
|
+
_pair(rest_source, target, matches, is_text),
|
|
348
|
+
]
|
|
349
|
+
matches = max(
|
|
350
|
+
candidates,
|
|
351
|
+
key=lambda pairs: sum(is_text(target[index]) for index in pairs),
|
|
352
|
+
)
|
|
353
|
+
|
|
354
|
+
result = []
|
|
355
|
+
for index, item in enumerate(target):
|
|
356
|
+
match = matches.get(index)
|
|
357
|
+
if match is None:
|
|
358
|
+
result.append(item)
|
|
359
|
+
continue
|
|
360
|
+
child_block = _child_block(block, item)
|
|
361
|
+
value = (
|
|
362
|
+
_align(child_block, match["value"], item["value"])
|
|
363
|
+
if child_block
|
|
364
|
+
else item["value"]
|
|
365
|
+
)
|
|
366
|
+
result.append({**item, "id": match["id"], "value": value})
|
|
367
|
+
return result
|
|
368
|
+
|
|
369
|
+
if isinstance(block, blocks.StructBlock) and isinstance(source, dict):
|
|
370
|
+
return {
|
|
371
|
+
name: _align(block.child_blocks[name], source[name], value)
|
|
372
|
+
if name in block.child_blocks and name in source
|
|
373
|
+
else value
|
|
374
|
+
for name, value in target.items()
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
return target
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def _child_block(block, item):
|
|
381
|
+
if isinstance(block, blocks.ListBlock):
|
|
382
|
+
return block.child_block
|
|
383
|
+
return block.child_blocks.get(item["type"])
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
def _pair(source: list, target: list, matches: dict, group) -> dict:
|
|
387
|
+
"""matches (target index -> source item) extended by the longest matching
|
|
388
|
+
sequences of block types, per group (items with the same group(item))."""
|
|
389
|
+
matches = dict(matches)
|
|
390
|
+
for value in {group(item) for item in target}:
|
|
391
|
+
rest_target = [
|
|
392
|
+
index
|
|
393
|
+
for index in range(len(target))
|
|
394
|
+
if index not in matches and group(target[index]) == value
|
|
395
|
+
]
|
|
396
|
+
rest_source = [item for item in source if group(item) == value]
|
|
397
|
+
matcher = difflib.SequenceMatcher(
|
|
398
|
+
None,
|
|
399
|
+
[item["type"] for item in rest_source],
|
|
400
|
+
[target[index]["type"] for index in rest_target],
|
|
401
|
+
autojunk=False,
|
|
402
|
+
)
|
|
403
|
+
for a, b, size in matcher.get_matching_blocks():
|
|
404
|
+
for offset in range(size):
|
|
405
|
+
matches[rest_target[b + offset]] = rest_source[a + offset]
|
|
406
|
+
return matches
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
from django import forms
|
|
2
|
+
from django.conf import settings
|
|
3
|
+
from django.utils.text import slugify
|
|
4
|
+
from wagtail.admin.rich_text import get_rich_text_editor_widget
|
|
5
|
+
from wagtail.documents import get_document_model
|
|
6
|
+
from wagtail.documents.widgets import AdminDocumentChooser
|
|
7
|
+
from wagtail.images import get_image_model
|
|
8
|
+
from wagtail.images.widgets import AdminImageChooser
|
|
9
|
+
|
|
10
|
+
from .fields import CHOOSER_KINDS, HTML, IMAGE, RICHTEXT, SYNCED, TEXTAREA
|
|
11
|
+
|
|
12
|
+
# Hidden inputs "machine:<path>" and "copied:<path>" carry a machine
|
|
13
|
+
# translation or a copied source text that is shown in the form but not saved
|
|
14
|
+
# yet, to tell whether the editor changed it.
|
|
15
|
+
MACHINE_PREFIX = "machine:"
|
|
16
|
+
COPIED_PREFIX = "copied:"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class TranslationForm(forms.Form):
|
|
20
|
+
"""One field per translatable unit, named by the unit path."""
|
|
21
|
+
|
|
22
|
+
def __init__(self, rows, *args, **kwargs):
|
|
23
|
+
super().__init__(*args, **kwargs)
|
|
24
|
+
self.rows = rows
|
|
25
|
+
for index, row in enumerate(rows):
|
|
26
|
+
if row.unit.kind in CHOOSER_KINDS:
|
|
27
|
+
self.fields[row.unit.path] = self._chooser_field(row)
|
|
28
|
+
elif row.unit.kind != SYNCED:
|
|
29
|
+
widget = self._widget(row.unit)
|
|
30
|
+
# Paths contain dots, which break "#id" selectors (Draftail).
|
|
31
|
+
widget.attrs["id"] = f"sbs-field-{index}"
|
|
32
|
+
self.fields[row.unit.path] = forms.CharField(
|
|
33
|
+
required=False,
|
|
34
|
+
initial=row.translation,
|
|
35
|
+
label=row.unit.label,
|
|
36
|
+
widget=widget,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
def entries(self):
|
|
40
|
+
"""(row, bound field or None, unsaved machine translation or None,
|
|
41
|
+
unsaved copied source text or None), in page order."""
|
|
42
|
+
machine = self._marked(MACHINE_PREFIX)
|
|
43
|
+
copied = self._marked(COPIED_PREFIX)
|
|
44
|
+
return [
|
|
45
|
+
(
|
|
46
|
+
row,
|
|
47
|
+
self[row.unit.path] if row.unit.path in self.fields else None,
|
|
48
|
+
machine.get(row.unit.path),
|
|
49
|
+
copied.get(row.unit.path),
|
|
50
|
+
)
|
|
51
|
+
for row in self.rows
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
def clean(self):
|
|
55
|
+
data = super().clean()
|
|
56
|
+
if not data.get("slug") and data.get("title"):
|
|
57
|
+
allow_unicode = getattr(settings, "WAGTAIL_ALLOW_UNICODE_SLUGS", True)
|
|
58
|
+
data["slug"] = slugify(data["title"], allow_unicode=allow_unicode)
|
|
59
|
+
return data
|
|
60
|
+
|
|
61
|
+
def values(self) -> dict[str, str]:
|
|
62
|
+
"""path -> text, or for an image/document the id of its own one (only
|
|
63
|
+
when "Use a different ... in this language" is ticked)."""
|
|
64
|
+
overrides = self.override_paths()
|
|
65
|
+
values = {}
|
|
66
|
+
for path, value in self.cleaned_data.items():
|
|
67
|
+
if isinstance(self.fields[path], forms.ModelChoiceField):
|
|
68
|
+
values[path] = str(value.pk) if value and path in overrides else ""
|
|
69
|
+
else:
|
|
70
|
+
values[path] = value.strip()
|
|
71
|
+
return values
|
|
72
|
+
|
|
73
|
+
def changed_paths(self) -> set[str]:
|
|
74
|
+
"""Paths whose text differs from what was shown. Rich text is compared
|
|
75
|
+
after an editor round trip, which normalises the HTML."""
|
|
76
|
+
values = self.values()
|
|
77
|
+
return {
|
|
78
|
+
path
|
|
79
|
+
for path, field in self.fields.items()
|
|
80
|
+
if not isinstance(field, forms.ModelChoiceField)
|
|
81
|
+
and values[path] != self._normalise(path, field.initial)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
def override_paths(self) -> set[str]:
|
|
85
|
+
"""Images/documents ticked "different in this language"."""
|
|
86
|
+
if self.is_bound:
|
|
87
|
+
return set(self.data.getlist("override"))
|
|
88
|
+
return {row.unit.path for row in self.rows if row.override}
|
|
89
|
+
|
|
90
|
+
def confirmed_paths(self) -> set[str]:
|
|
91
|
+
return set(self.data.getlist("confirm"))
|
|
92
|
+
|
|
93
|
+
def same_paths(self) -> set[str]:
|
|
94
|
+
"""Paths ticked "Same in both languages": as submitted, or as saved."""
|
|
95
|
+
if self.is_bound:
|
|
96
|
+
return set(self.data.getlist("same"))
|
|
97
|
+
return {row.unit.path for row in self.rows if row.same}
|
|
98
|
+
|
|
99
|
+
def machine_texts(self) -> dict[str, str]:
|
|
100
|
+
return self._marked(MACHINE_PREFIX)
|
|
101
|
+
|
|
102
|
+
def machine_paths(self) -> set[str]:
|
|
103
|
+
"""Paths still holding the unchanged machine translation."""
|
|
104
|
+
return self._unchanged(MACHINE_PREFIX)
|
|
105
|
+
|
|
106
|
+
def copied_paths(self) -> set[str]:
|
|
107
|
+
"""Paths still holding the unchanged copied source text."""
|
|
108
|
+
return self._unchanged(COPIED_PREFIX)
|
|
109
|
+
|
|
110
|
+
def with_translations(
|
|
111
|
+
self, translations: dict[str, str], prefix: str = MACHINE_PREFIX
|
|
112
|
+
) -> "TranslationForm":
|
|
113
|
+
"""A new bound form with the submitted data plus these texts (machine
|
|
114
|
+
translations, or with COPIED_PREFIX copied source texts), to show (not
|
|
115
|
+
save) them."""
|
|
116
|
+
data = self.data.copy()
|
|
117
|
+
for path, text in translations.items():
|
|
118
|
+
data[path] = self.fields[path].widget.format_value(text)
|
|
119
|
+
data[prefix + path] = text
|
|
120
|
+
return TranslationForm(self.rows, data)
|
|
121
|
+
|
|
122
|
+
def _marked(self, prefix: str) -> dict[str, str]:
|
|
123
|
+
return {
|
|
124
|
+
key[len(prefix) :]: value
|
|
125
|
+
for key, value in self.data.items()
|
|
126
|
+
if key.startswith(prefix) and key[len(prefix) :] in self.fields
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
def _unchanged(self, prefix: str) -> set[str]:
|
|
130
|
+
values = self.values()
|
|
131
|
+
return {
|
|
132
|
+
path
|
|
133
|
+
for path, text in self._marked(prefix).items()
|
|
134
|
+
if values[path] == self._normalise(path, text)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
def _normalise(self, path, value: str) -> str:
|
|
138
|
+
widget = self.fields[path].widget
|
|
139
|
+
if value and hasattr(widget, "converter"):
|
|
140
|
+
value = widget.value_from_datadict(
|
|
141
|
+
{path: widget.format_value(value)}, {}, path
|
|
142
|
+
)
|
|
143
|
+
return (value or "").strip()
|
|
144
|
+
|
|
145
|
+
@staticmethod
|
|
146
|
+
def _chooser_field(row):
|
|
147
|
+
"""Wagtail's image/document chooser, starting at the translation's own
|
|
148
|
+
object or else the source's."""
|
|
149
|
+
if row.unit.kind == IMAGE:
|
|
150
|
+
model, widget = get_image_model(), AdminImageChooser()
|
|
151
|
+
else:
|
|
152
|
+
model, widget = get_document_model(), AdminDocumentChooser()
|
|
153
|
+
initial = row.translation or row.unit.source
|
|
154
|
+
return forms.ModelChoiceField(
|
|
155
|
+
queryset=model.objects.all(),
|
|
156
|
+
required=False,
|
|
157
|
+
initial=int(initial) if initial else None,
|
|
158
|
+
label=row.unit.label,
|
|
159
|
+
widget=widget,
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
@staticmethod
|
|
163
|
+
def _widget(unit):
|
|
164
|
+
if unit.kind == RICHTEXT:
|
|
165
|
+
return get_rich_text_editor_widget(features=unit.features)
|
|
166
|
+
if unit.kind == HTML:
|
|
167
|
+
return forms.Textarea(attrs={"rows": 6, "class": "sbs-code"})
|
|
168
|
+
if unit.kind == TEXTAREA:
|
|
169
|
+
return forms.Textarea(attrs={"rows": 3})
|
|
170
|
+
return forms.TextInput()
|
|
Binary file
|