def test_blockquoteblock(self): block_id = uuid.uuid4() page = make_test_page_with_streamfield_block(str(block_id), "test_blockquoteblock", "Test content") translated_page = page.copy_for_translation(self.locale) ingest_segments( page, translated_page, self.src_locale, self.locale, [ SegmentValue(f"test_streamfield.{block_id}", "Tester le contenu") ], ) translated_page.save() translated_page.refresh_from_db() self.assertEqual( translated_page.test_streamfield.stream_data, [{ "id": str(block_id), "type": "test_blockquoteblock", "value": "Tester le contenu", }], )
def test_listblock(self): block_id = uuid.uuid4() page = make_test_page_with_streamfield_block( str(block_id), "test_listblock", ["Test content", "Some more test content"]) translated_page = page.copy_for_translation(self.locale) ingest_segments( page, translated_page, self.src_locale, self.locale, [ SegmentValue(f"test_streamfield.{block_id}", "Tester le contenu", order=0), SegmentValue(f"test_streamfield.{block_id}", "Encore du contenu de test", order=1), ], ) translated_page.save() translated_page.refresh_from_db() self.assertEqual( translated_page.test_streamfield.stream_data, [{ "id": str(block_id), "type": "test_listblock", "value": ["Tester le contenu", "Encore du contenu de test"], }], )
def test_childobjects(self): page = make_test_page() page.test_childobjects.add(TestChildObject(field="Test content")) page.save() child_translation_key = TestChildObject.objects.get().translation_key translated_page = page.copy_for_translation(self.locale) ingest_segments( page, translated_page, self.src_locale, self.locale, [ SegmentValue( f"test_childobjects.{child_translation_key}.field", "Tester le contenu", ) ], ) old_child_object = page.test_childobjects.get() new_child_object = translated_page.test_childobjects.get() self.assertNotEqual(old_child_object.id, new_child_object.id) self.assertEqual(old_child_object.locale, self.src_locale) self.assertEqual(new_child_object.locale, self.locale) self.assertEqual(old_child_object.translation_key, new_child_object.translation_key) self.assertEqual(new_child_object.field, "Tester le contenu")
def test_rawhtmlblock(self): block_id = uuid.uuid4() page = make_test_page_with_streamfield_block(str(block_id), "test_rawhtmlblock", RICH_TEXT_TEST_INPUT) translated_page = page.copy_for_translation(self.locale) ingest_segments( page, translated_page, self.src_locale, self.locale, [ segment.wrap(f"test_streamfield.{block_id}") for segment in RICH_TEXT_TEST_FRENCH_SEGMENTS ], ) translated_page.save() translated_page.refresh_from_db() self.assertEqual( translated_page.test_streamfield.stream_data, [{ "id": str(block_id), "type": "test_rawhtmlblock", "value": RICH_TEXT_TEST_OUTPUT, }], )
def ingest_messages(self, instance): if instance.translation_key in self.seen_objects: return self.seen_objects.add(instance.translation_key) segments = extract_segments(instance) # Ingest segments for dependencies first for segment in segments: if isinstance(segment, RelatedObjectValue): self.ingest_messages(segment.get_instance(self.source_locale)) text_segments = [ segment for segment in segments if isinstance(segment, SegmentValue) ] # Initialise translated segments by copying templates and related objects translated_segments = [ segment for segment in segments if isinstance(segment, (TemplateValue, RelatedObjectValue)) ] missing_segments = 0 for segment in text_segments: if segment.text in self.translations: translated_segments.append( SegmentValue( segment.path, self.translations[segment.text], order=segment.order, )) else: missing_segments += 1 if missing_segments: raise MissingSegmentsException(instance, missing_segments) translated_segments.sort(key=lambda segment: segment.order) try: translation = instance.get_translation(self.target_locale) except instance.__class__.DoesNotExist: translation = instance.copy_for_translation(self.target_locale) ingest_segments( instance, translation, self.source_locale, self.target_locale, translated_segments, ) if isinstance(translation, Page): translation.slug = slugify(translation.slug) revision = translation.save_revision() else: translation.save() return translation
def test_urlblock(self): block_id = uuid.uuid4() page = make_test_page_with_streamfield_block( str(block_id), "test_urlblock", "http://test-content.com/foo") translated_page = page.copy_for_translation(self.locale) ingest_segments( page, translated_page, self.src_locale, self.locale, [ StringSegmentValue(f"test_streamfield.{block_id}", "http://test-content.fr/foo") ], ) translated_page.save() translated_page.refresh_from_db() self.assertEqual( translated_page.test_streamfield.stream_data, [{ "id": str(block_id), "type": "test_urlblock", "value": "http://test-content.fr/foo", }], )
def test_emailfield(self): page = make_test_page(test_emailfield="*****@*****.**") translated_page = page.copy_for_translation(self.locale) ingest_segments( page, translated_page, self.src_locale, self.locale, [SegmentValue("test_emailfield", "*****@*****.**")], ) self.assertEqual(translated_page.test_emailfield, "*****@*****.**")
def test_textfield(self): page = make_test_page(test_textfield="Test content") translated_page = page.copy_for_translation(self.locale) ingest_segments( page, translated_page, self.src_locale, self.locale, [SegmentValue("test_textfield", "Tester le contenu")], ) self.assertEqual(translated_page.test_textfield, "Tester le contenu")
def test_urlfield(self): page = make_test_page(test_urlfield="http://test-content.com/foo") translated_page = page.copy_for_translation(self.locale) ingest_segments( page, translated_page, self.src_locale, self.locale, [SegmentValue("test_urlfield", "http://test-content.fr/foo")], ) self.assertEqual(translated_page.test_urlfield, "http://test-content.fr/foo")
def test_richtextfield(self): page = make_test_page(test_richtextfield=RICH_TEXT_TEST_INPUT) translated_page = page.copy_for_translation(self.locale) ingest_segments( page, translated_page, self.src_locale, self.locale, [ segment.wrap("test_richtextfield") for segment in RICH_TEXT_TEST_FRENCH_SEGMENTS ], ) self.assertEqual(translated_page.test_richtextfield, RICH_TEXT_TEST_OUTPUT)
def test_snippet(self): test_snippet = TestSnippet.objects.create(field="Test content") translated_snippet = test_snippet.copy_for_translation(self.locale) translated_snippet.save() # Ingest segments into the snippet ingest_segments( test_snippet, translated_snippet, self.src_locale, self.locale, [StringSegmentValue("field", "Tester le contenu")], ) translated_snippet.save() self.assertEqual(translated_snippet.field, "Tester le contenu") # Now ingest a RelatedObjectSegmentValue into the page page = make_test_page(test_snippet=test_snippet) translated_page = page.copy_for_translation(self.locale) ingest_segments( page, translated_page, self.src_locale, self.locale, [ RelatedObjectSegmentValue.from_instance( "test_snippet", test_snippet) ], ) # Check the translated snippet was linked to the translated page self.assertNotEqual(page.test_snippet_id, translated_page.test_snippet_id) self.assertEqual(page.test_snippet.locale, self.src_locale) self.assertEqual(translated_page.test_snippet.locale, self.locale) self.assertEqual( page.test_snippet.translation_key, translated_page.test_snippet.translation_key, ) self.assertEqual(translated_page.test_snippet.field, "Tester le contenu")
def test_customstructblock(self): block_id = uuid.uuid4() page = make_test_page_with_streamfield_block( str(block_id), "test_customstructblock", { "field_a": "Test content", "field_b": "Some more test content" }, ) translated_page = page.copy_for_translation(self.locale) ingest_segments( page, translated_page, self.src_locale, self.locale, [ SegmentValue( f"test_streamfield.{block_id}.foo", "Tester le contenu / Encore du contenu de test", ) ], ) translated_page.save() translated_page.refresh_from_db() self.assertEqual( translated_page.test_streamfield.stream_data, [{ "id": str(block_id), "type": "test_customstructblock", "value": { "field_a": "Tester le contenu", "field_b": "Encore du contenu de test", }, }], )