def test_create_child(self): child_page = create_test_page( title="Child page", slug="child-page", parent=self.page, test_charfield="This is some test content", ) child_source = TranslationSource.from_instance(child_page)[0] translated_parent = self.page.copy_for_translation(self.dest_locale) # Create a translation for the new context StringTranslation.objects.create( translation_of=self.string, locale=self.dest_locale, context=TranslationContext.objects.get( object_id=child_page.translation_key, path="test_charfield"), data="Ceci est du contenu de test", ) new_page, created = child_source.create_or_update_translation( self.dest_locale) self.assertTrue(created) self.assertEqual(new_page.get_parent(), translated_parent) self.assertEqual(new_page.title, "Child page") self.assertEqual(new_page.test_charfield, "Ceci est du contenu de test") self.assertEqual(new_page.translation_key, child_page.translation_key) self.assertEqual(new_page.locale, self.dest_locale) self.assertTrue( child_source.translation_logs.filter( locale=self.dest_locale).exists())
def test_creates_new_source_if_changed(self): source = TranslationSource.objects.create( object_id=self.snippet.translation_key, specific_content_type=ContentType.objects.get_for_model( TestSnippet), locale=self.snippet.locale, content_json=json.dumps({ "pk": self.snippet.pk, "field": "Some different content", # Changed "translation_key": str(self.snippet.translation_key), "locale": self.snippet.locale_id, }), created_at=timezone.now(), ) new_source, created = TranslationSource.from_instance(self.snippet) self.assertTrue(created) self.assertNotEqual(source, new_source) self.assertEqual( json.loads(source.content_json)["field"], "Some different content") self.assertEqual( json.loads(new_source.content_json)["field"], "This is some test content")
def setUp(self): self.snippet = TestSnippet.objects.create(field="Test snippet content") self.page = create_test_page( title="Test page", slug="test-page", test_charfield="This is some test content", test_snippet=self.snippet, ) self.source = TranslationSource.from_instance(self.page)[0] self.source_locale = Locale.objects.get(language_code="en") self.dest_locale = Locale.objects.create(language_code="fr") # Translate the snippet self.translated_snippet = self.snippet.copy_for_translation( self.dest_locale) self.translated_snippet.field = "Tester le contenu de l'extrait" self.translated_snippet.save() # Add translation for test_charfield self.string = String.from_value( self.source_locale, StringValue.from_plaintext("This is some test content")) self.translation = StringTranslation.objects.create( translation_of=self.string, locale=self.dest_locale, context=TranslationContext.objects.get( object_id=self.page.translation_key, path="test_charfield"), data="Ceci est du contenu de test", )
def setUp(self): self.page = create_test_page( title="Test page", slug="test-page", test_charfield="This is some test content") self.source = TranslationSource.from_instance(self.page)[0] self.source.extract_segments()
def test_update_synchronised_fields(self): translated = self.page.copy_for_translation(self.dest_locale) self.page.test_synchronized_charfield = "Test synchronised content" self.page.test_synchronized_textfield = "Test synchronised content" self.page.test_synchronized_emailfield = "*****@*****.**" self.page.test_synchronized_slugfield = "test-synchronised-content" self.page.test_synchronized_urlfield = "https://test.synchronised/content" self.page.test_synchronized_richtextfield = "<p>Test synchronised content</p>" # self.page.test_synchronized_streamfield = "" synchronized_snippet = TestSnippet.objects.create( field="Synchronised snippet") self.page.test_synchronized_snippet = synchronized_snippet self.page.test_synchronized_customfield = "Test synchronised content" # Save the page revision = self.page.save_revision() revision.publish() self.page.refresh_from_db() source_with_translated_content = TranslationSource.from_instance( self.page)[0] # Check translation hasn't been updated yet translated.refresh_from_db() self.assertEqual(translated.test_synchronized_charfield, "") # Update the original page again. This will make sure it's taking the content from the translation source and not the live version self.page.test_synchronized_charfield = ( "Test synchronised content updated again") self.page.save_revision().publish() ( new_page, created, ) = source_with_translated_content.create_or_update_translation( self.dest_locale) self.assertFalse(created) self.assertEqual(new_page, translated) self.assertEqual(new_page.test_synchronized_charfield, "Test synchronised content") self.assertEqual(new_page.test_synchronized_charfield, "Test synchronised content") self.assertEqual(new_page.test_synchronized_textfield, "Test synchronised content") self.assertEqual(new_page.test_synchronized_emailfield, "*****@*****.**") self.assertEqual(new_page.test_synchronized_slugfield, "test-synchronised-content") self.assertEqual(new_page.test_synchronized_urlfield, "https://test.synchronised/content") self.assertEqual(new_page.test_synchronized_richtextfield, "<p>Test synchronised content</p>") self.assertEqual(new_page.test_synchronized_snippet, synchronized_snippet) self.assertEqual(new_page.test_synchronized_customfield, "Test synchronised content")
def create_test_page(**kwargs): parent = kwargs.pop("parent", None) or Page.objects.get(id=1) page = parent.add_child(instance=TestPage(**kwargs)) page_revision = page.save_revision() page_revision.publish() page.refresh_from_db() source, created = TranslationSource.from_instance(page) prepare_source(source) return page
def test_import_po(self): obsolete_string = String.from_value(self.en_locale, StringValue("This is an obsolete string")) StringTranslation.objects.create( translation_of=obsolete_string, context=TranslationContext.objects.get(path="test_charfield"), locale=self.fr_locale, data="Ceci est une chaîne obsolète", ) # Create a translation source to represent a past source that had the above string past_source = TranslationSource.from_instance(self.page, force=True)[0] past_source.extract_segments() past_source.stringsegment_set.update(string=obsolete_string) po = polib.POFile(wrapwidth=200) po.metadata = { "POT-Creation-Date": str(timezone.now()), "MIME-Version": "1.0", "Content-Type": "text/plain; charset=utf-8", "X-WagtailLocalize-TranslationID": str(self.translation.uuid), } po.append( polib.POEntry( msgid="This is some test content", msgctxt="test_charfield", msgstr="Contenu de test", ) ) po.append( polib.POEntry( msgid="This is an obsolete string", msgctxt="test_charfield", msgstr="C'est encore une chaîne obsolète", obsolete=True ) ) warnings = list(self.translation.import_po(po)) self.assertEqual(warnings, []) translation = StringTranslation.objects.get(translation_of__data="This is some test content") self.assertEqual(translation.context, TranslationContext.objects.get(path="test_charfield")) self.assertEqual(translation.locale, self.fr_locale) self.assertEqual(translation.data, "Contenu de test") # Obsolete strings still get updated translation = StringTranslation.objects.get(translation_of__data="This is an obsolete string") self.assertEqual(translation.context, TranslationContext.objects.get(path="test_charfield")) self.assertEqual(translation.locale, self.fr_locale) self.assertEqual(translation.data, "C'est encore une chaîne obsolète",)
def setUp(self): self.en_locale = Locale.objects.get(language_code="en") self.fr_locale = Locale.objects.create(language_code="fr") self.page = create_test_page(title="Test page", slug="test-page", test_charfield="This is some test content") self.source = TranslationSource.from_instance(self.page)[0] self.source.extract_segments() self.translation = Translation.objects.create( object=self.source.object, target_locale=self.fr_locale, source=self.source, )
def prepare_source(source): # Extract segments from source and save them into translation memory segments = extract_segments(source.as_instance()) insert_segments(source, source.locale_id, segments) # Recurse into any related objects for segment in segments: if not isinstance(segment, RelatedObjectSegmentValue): continue related_source, created = TranslationSource.from_instance( segment.get_instance(source.locale)) prepare_source(related_source)
def test_create(self): source, created = TranslationSource.from_instance(self.snippet) self.assertTrue(created) self.assertEqual(source.object_id, self.snippet.translation_key) self.assertEqual(source.locale, self.snippet.locale) self.assertEqual( json.loads(source.content_json), { "pk": self.snippet.pk, "field": "This is some test content", "translation_key": str(self.snippet.translation_key), "locale": self.snippet.locale_id, }, ) self.assertTrue(source.created_at)
def test_update_streamfields(self): # Streamfields are special in that they contain content that needs to be synchronised as well as # translatable content. # Copy page for translation, this will have a blank streamfield translated = self.page.copy_for_translation(self.dest_locale) # Set streamfield value on original self.page.test_streamfield = StreamValue( TestPage.test_streamfield.field.stream_block, [{ "id": "id", "type": "test_charblock", "value": "This is some test content", }], is_lazy=True, ) # Save the page revision = self.page.save_revision() revision.publish() self.page.refresh_from_db() source_with_streamfield = TranslationSource.from_instance(self.page)[0] source_with_streamfield.extract_segments() # Create a translation for the new context StringTranslation.objects.create( translation_of=self.string, locale=self.dest_locale, context=TranslationContext.objects.get( object_id=self.page.translation_key, path="test_streamfield.id"), data="Ceci est du contenu de test", ) new_page, created = source_with_streamfield.create_or_update_translation( self.dest_locale) self.assertFalse(created) self.assertEqual(new_page, translated) # Check the block was copied into translation self.assertEqual(new_page.test_streamfield[0].id, "id") self.assertEqual(new_page.test_streamfield[0].value, "Ceci est du contenu de test")
def test_save_target_cant_save_snippet_as_draft(self): snippet = TestSnippet.objects.create(field="Test content") source, created = TranslationSource.from_instance(snippet) source.extract_segments() translation = Translation.objects.create( object=source.object, target_locale=self.fr_locale, source=source, ) field_context = TranslationContext.objects.get(path="field") StringTranslation.objects.create( translation_of=self.test_content_string, context=field_context, locale=self.fr_locale, data="Contenu de test", ) with self.assertRaises(CannotSaveDraftError): translation.save_target(publish=False)
def test_save_target_snippet(self): snippet = TestSnippet.objects.create(field="Test content") source, created = TranslationSource.from_instance(snippet) source.extract_segments() translation = Translation.objects.create( object=source.object, target_locale=self.fr_locale, source=source, ) field_context = TranslationContext.objects.get(path="field") StringTranslation.objects.create( translation_of=self.test_content_string, context=field_context, locale=self.fr_locale, data="Contenu de test", ) translation.save_target() translated_snippet = snippet.get_translation(self.fr_locale) self.assertEqual(translated_snippet.field, "Contenu de test")
def test_reuses_existing_source_if_not_changed(self): source = TranslationSource.objects.create( object_id=self.snippet.translation_key, specific_content_type=ContentType.objects.get_for_model( TestSnippet), locale=self.snippet.locale, content_json=json.dumps({ "pk": self.snippet.pk, "field": "This is some test content", "translation_key": str(self.snippet.translation_key), "locale": self.snippet.locale_id, }), created_at=timezone.now(), ) new_source, created = TranslationSource.from_instance(self.snippet) self.assertFalse(created) self.assertEqual(source, new_source)
def setUp(self): self.snippet = TestSnippet.objects.create( field="This is some test content") self.source = TranslationSource.from_instance(self.snippet)[0]
def setUp(self): self.page = create_test_page(title="Test page", slug="test-page") self.source = TranslationSource.from_instance(self.page)[0]