diff --git a/wagtail_localize/fields.py b/wagtail_localize/fields.py index 4d3e166e..d8dd00cc 100644 --- a/wagtail_localize/fields.py +++ b/wagtail_localize/fields.py @@ -61,16 +61,24 @@ class TranslatableField(BaseTranslatableField): def is_translated(self, obj): return True - def is_synchronized(self, obj): - field = self.get_field(obj.__class__) + def is_synchronized(self, source): + field = self.get_field(source.__class__) # Child relations should all be synchronised before translation - if isinstance(field, (models.ManyToOneRel)) and isinstance( + if isinstance(field, models.ManyToOneRel) and isinstance( field.remote_field, ParentalKey ): return True - # Streamfields need to be re-synchronised before translation so the structure and non-translatable content is copied over + # We have a text field that has been cleared so we should mark it as synchronised + if ( + isinstance(field, (RichTextField, models.TextField, models.CharField)) + and getattr(source, field.attname) == "" + ): + return True + + # Streamfields need to be re-synchronised before translation so + # the structure and non-translatable content is copied over return isinstance(field, StreamField) def __repr__(self): @@ -245,7 +253,7 @@ def copy_synchronised_fields(source, target): if translatable_field.is_synchronized(source): field = translatable_field.get_field(target.__class__) - if isinstance(field, (models.ManyToOneRel)) and isinstance( + if isinstance(field, models.ManyToOneRel) and isinstance( field.remote_field, ParentalKey ): # Use modelcluster's copy_child_relation for child relations diff --git a/wagtail_localize/tests/test_update_translations.py b/wagtail_localize/tests/test_update_translations.py index 33f01714..739db8b6 100644 --- a/wagtail_localize/tests/test_update_translations.py +++ b/wagtail_localize/tests/test_update_translations.py @@ -206,6 +206,7 @@ def setUp(self): title="Blog post", slug="blog-post", test_charfield="Test content", + test_richtextfield="

rich text

", ) self.en_snippet = TestSnippet.objects.create(field="Test snippet") @@ -351,6 +352,30 @@ def test_post_update_page_translation_with_publish_translations(self): self.fr_blog_post.refresh_from_db() self.assertEqual(self.fr_blog_post.test_charfield, "Edited blog post") + def test_post_update_page_translation_with_publish_translations_and_cleared_text_field( + self, + ): + self.en_blog_post.test_charfield = "" + self.en_blog_post.test_richtextfield = "" + self.en_blog_post.save_revision().publish() + + response = self.client.post( + reverse( + "wagtail_localize:update_translations", + args=[self.page_source.id], + ), + {"publish_translations": "on"}, + ) + + self.assertRedirects( + response, reverse("wagtailadmin_explore", args=[self.en_blog_index.id]) + ) + + # The FR version should be updated + self.fr_blog_post.refresh_from_db() + self.assertEqual(self.fr_blog_post.test_charfield, "") + self.assertEqual(self.fr_blog_post.test_richtextfield, "") + def test_post_update_snippet_translation(self): self.en_snippet.field = "Edited snippet" self.en_snippet.save()