Exemplo n.º 1
0
 def test_machine_translation_with_non_allowlisted_language_returns_none(
     self
 ) -> None:
     translated_text = (
         translation_services.get_and_cache_machine_translation(
             'en', 'hi', 'text to translate')
     )
     self.assertIsNone(translated_text)
     translated_text = (
         translation_services.get_and_cache_machine_translation(
             'hi', 'en', 'text to translate')
     )
     self.assertIsNone(translated_text)
     # Ensure that no translation is cached when returning none (no
     # translation found).
     self.assertIsNone(
         translation_models.MachineTranslationModel.get_machine_translation(
             'en', 'hi', 'text to translated'
         )
     )
     self.assertIsNone(
         translation_models.MachineTranslationModel.get_machine_translation(
             'hi', 'en', 'text to translated'
         )
     )
 def test_get_machine_translation_checks_datastore_first(self):
     with self.swap_to_always_raise(translate_services.CLIENT,
                                    'translate',
                                    error=AssertionError):
         self.assertEqual(
             translation_services.get_and_cache_machine_translation(
                 'en', 'es', 'text to translate'), 'texto para traducir')
 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')
Exemplo n.º 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')
Exemplo n.º 6
0
    def get(self):
        """Handles GET requests. Responds with a mapping from content id to
        translation of form:

            dict('translated_texts', dict(str, str|None))

        If no translation is found for a given content id, that id is mapped to
        None.

        Params:
            exp_id: str. The ID of the exploration being translated.
            state_name: str. The name of the exploration state being translated.
            content_ids: str[]. The content IDs of the texts to be translated.
            target_language_code: str. The language code of the target
                translation language.

        Data Response:

            dict('translated_texts': dict(str, str|None))

            A dictionary containing the translated texts stored as a mapping
                from content ID to the translated text. If an error occured
                during retrieval of some content translations, but not others,
                failed translations are mapped to None.

        Raises:
            400 (Bad Request): InvalidInputException. At least one input is
                missing or improperly formatted.
            404 (Not Found): PageNotFoundException. At least one identifier does
                not correspond to an entry in the datastore.
        """
        exp_id = self.normalized_request.get('exp_id')

        state_name = self.normalized_request.get('state_name')

        content_ids_string = self.normalized_request.get('content_ids')
        content_ids = []
        try:
            content_ids = json.loads(content_ids_string)
        except Exception as e:
            raise self.InvalidInputException(
                'Improperly formatted content_ids: %s' % content_ids_string
            ) from e

        target_language_code = self.normalized_request.get(
            'target_language_code')

        exp = exp_fetchers.get_exploration_by_id(exp_id, strict=False)
        if exp is None:
            raise self.PageNotFoundException()
        state_names_to_content_id_mapping = exp.get_translatable_text(
            target_language_code)
        if state_name not in state_names_to_content_id_mapping:
            raise self.PageNotFoundException()
        content_id_to_translatable_item_mapping = (
            state_names_to_content_id_mapping[state_name])
        translated_texts = {}
        for content_id in content_ids:
            if content_id not in content_id_to_translatable_item_mapping:
                translated_texts[content_id] = None
                continue

            source_text = content_id_to_translatable_item_mapping[
                content_id].content
            translated_texts[content_id] = (
                translation_services.get_and_cache_machine_translation(
                    exp.language_code, target_language_code, source_text)
            )

        self.values = {
            'translated_texts': translated_texts
        }
        self.render_json(self.values)