def translate(self, source_locale, target_locale, strings): translator = googletrans.Translator() google_translations = translator.translate( [string.render_text() for string in strings], src=language_code(source_locale.language_code), dest=language_code(target_locale.language_code), ) return { StringValue.from_plaintext(translation.origin): StringValue.from_plaintext(translation.text) for translation in google_translations }
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 test_string_from_plaintext(self): string = StringValue.from_plaintext("This is a test <Foo> bar 'baz'", ) # Django 2.x HTML escape function escapes quotes differently if DJANGO_VERSION >= (3, 0): self.assertEqual( string.data, "This is a test <Foo> bar 'baz'", ) else: self.assertEqual( string.data, "This is a test <Foo> bar 'baz'", )
def test_newlines_converted_to_br_tags(self): string = StringValue.from_plaintext("foo\nbar\nbaz") self.assertEqual(string.data, "foo<br>bar<br>baz") string = StringValue.from_plaintext("\nfoo\nbar\n") self.assertEqual(string.data, "<br>foo<br>bar<br>")
def test_special_chars_escaped(self): string = StringValue.from_plaintext("foo & bar") self.assertEqual(string.data, "foo & bar")