diff --git a/wagtail_localize/models.py b/wagtail_localize/models.py index fa3fb40f..b7e5dd33 100644 --- a/wagtail_localize/models.py +++ b/wagtail_localize/models.py @@ -18,9 +18,11 @@ Subquery, Exists, OuterRef, - Q + Q, + F ) -from django.db.models.signals import post_delete +from django.db.models.signals import post_delete, post_save +from django.dispatch import receiver from django.utils import timezone from django.utils.encoding import force_text from django.utils.text import capfirst, slugify @@ -1050,6 +1052,28 @@ def get_comment(self): return _("Machine translated on {date}").format(date=self.updated_at.strftime(DATE_FORMAT)) +@receiver(post_save, sender=StringTranslation) +def post_save_string_translation(instance, **kwargs): + # If the StringTranslation is for a page title, update that page's draft_title field + if instance.context.path == 'title': + # Note: if this StringTranslation isn't for a page, this should do nothing + Page.objects.filter( + translation_key=instance.context.object_id, + locale_id=instance.locale_id + ).update(draft_title=instance.data) + + +@receiver(post_delete, sender=StringTranslation) +def post_delete_string_translation(instance, **kwargs): + # If the StringTranslation is for a page title, reset that page's draft title to the main title + if instance.context.path == 'title': + # Note: if this StringTranslation isn't for a page, this should do nothing + Page.objects.filter( + translation_key=instance.context.object_id, + locale_id=instance.locale_id + ).update(draft_title=F('title')) + + class Template(models.Model): BASE_UUID_NAMESPACE = uuid.UUID("4599eabc-3f8e-41a9-be61-95417d26a8cd") diff --git a/wagtail_localize/static_src/editor/components/TranslationEditor/header.tsx b/wagtail_localize/static_src/editor/components/TranslationEditor/header.tsx index 7c7069ec..180c39f7 100644 --- a/wagtail_localize/static_src/editor/components/TranslationEditor/header.tsx +++ b/wagtail_localize/static_src/editor/components/TranslationEditor/header.tsx @@ -111,7 +111,8 @@ const EditorHeader: FunctionComponent = ({ breadcrumb, sourceLocale, locale, - translations + translations, + stringTranslations }) => { // Build actions let actions = []; @@ -162,9 +163,22 @@ const EditorHeader: FunctionComponent = ({ /> ]; + // Title + // Allow the title to be overridden by the segment that represents the "title" field on Pages. + let title = object.title; + if (object.titleSegmentId) { + Array.from(stringTranslations.entries()).forEach( + ([segmentId, stringTranslation]) => { + if (segmentId == object.titleSegmentId) { + title = stringTranslation.value; + } + } + ); + } + return (