Exemple #1
0
 def test_get_machine_translation_for_cached_translation_returns_from_cache(
         self):
     translation_models.MachineTranslationModel.create(
         'en', 'es', 'hello world', 'hola mundo')
     translation = translation_fetchers.get_machine_translation(
         'en', 'es', 'hello world')
     self.assertEqual(translation.translated_text, 'hola mundo')
 def test_get_machine_translation_with_same_source_and_target_language_code(
         self):
     translated_text = (
         translation_services.get_and_cache_machine_translation(
             'en', 'en', 'text to translate'))
     self.assertEqual(translated_text, 'text to translate')
     translation = translation_fetchers.get_machine_translation(
         'en', 'en', 'text to translate')
     self.assertIsNone(translation)
 def test_get_machine_translation_with_new_translation_saves_translation(
         self):
     translated_text = (
         translation_services.get_and_cache_machine_translation(
             'en', 'fr', 'hello world'))
     self.assertEqual(translated_text, 'Bonjour le monde')
     translation = translation_fetchers.get_machine_translation(
         'en', 'fr', 'hello world')
     self.assertIsNotNone(translation)
     self.assertEqual(translation.translated_text, 'Bonjour le monde')
 def test_get_machine_translation_for_cached_translation_returns_from_cache(
     self
 ) -> None:
     translation_models.MachineTranslationModel.create(
         'en', 'es', 'hello world', 'hola mundo')
     translation = translation_fetchers.get_machine_translation(
         'en', 'es', 'hello world'
     )
     # Ruling out the possibility of None for mypy type checking.
     assert translation is not None
     self.assertEqual(translation.translated_text, 'hola mundo')
Exemple #5
0
 def test_get_machine_translation_with_new_translation_saves_translation(
     self
 ) -> None:
     translated_text = (
         translation_services.get_and_cache_machine_translation(
             'en', 'fr', 'hello world')
     )
     self.assertEqual(translated_text, 'Bonjour le monde')
     translation = translation_fetchers.get_machine_translation(
         'en', 'fr', 'hello world')
     self.assertIsNotNone(translation)
     # Ruling out the possibility of None for mypy type checking.
     assert translation is not None
     self.assertEqual(translation.translated_text, 'Bonjour le monde')
def get_and_cache_machine_translation(
        source_language_code, target_language_code, source_text):
    """Gets a machine translation of the source text for the given source and
    target languages. If no translation exists in the datastore for the given
    input, generates a machine translation using cloud_translate_services and
    saves the translation to the datastore.

    Args:
        source_language_code: str. The language code for the source text
            language. Must be different from target_language_code.
        target_language_code: str. The language code for the target
            translation language. Must be different from source_language_code.
        source_text: str. The untranslated source text.

    Returns:
        str|None. The translated text or None if no translation is found.
    """
    translation = translation_fetchers.get_machine_translation(
        source_language_code,
        target_language_code,
        source_text.strip()
    )
    if translation is not None:
        return translation.translated_text

    translated_text = None
    try:
        translated_text = translate_services.translate_text(
            source_text, source_language_code, target_language_code)
    # An error here indicates a valid, but not allowlisted language code, or an
    # error raised by the Google Cloud Translate API. The error is logged
    # instead of raised to provide an uninterrupted end user experience while
    # still providing error context on the back-end.
    except ValueError as e:
        logging.error(e)

    if translated_text is not None:
        translation_models.MachineTranslationModel.create(
            source_language_code,
            target_language_code,
            source_text,
            translated_text
        )

    return translated_text
 def test_get_machine_translation_with_no_translation_returns_none(
     self
 ) -> None:
     translation = translation_fetchers.get_machine_translation(
         'en', 'es', 'untranslated_text')
     self.assertIsNone(translation)