Exemplo n.º 1
0
def translate_task(instance_key, system, src_lang, src_field, dst_lang,
                   dst_field):
    """Celery task to perform a single translation

    .. Note::

       This is rate-limited at 60/m.

       We really want the translate call to be rate-limited based on
       the translation system, but given we're only supporting Gengo
       right now, I'm going to do the rate limiting here across all
       translation systems rather than figure out how to do it just
       for Gengo.

    :arg instance_key: The key for the instance we're translating
    :arg system: The name of the translation system to use
    :arg src_lang: The source language
    :arg src_field: The field in the instance holding the text to
         translate
    :arg dst_lang: The destination language
    :arg dst_field: The field in the instance to shove the translated
        text into

    """
    instance = key_to_instance(instance_key)
    translate(instance, system, src_lang, src_field, dst_lang, dst_field)
Exemplo n.º 2
0
    def test_translate_dennis(self):
        obj = SuperModel(locale='fr', desc=u'This is a test string')
        obj.save()

        assert obj.trans_desc == u''
        translate(obj, 'dennis', 'br', 'desc', 'en', 'trans_desc')
        assert obj.trans_desc == u'\xabTHIS IS A TEST STRING\xbb'
Exemplo n.º 3
0
def translate_task(instance_key, system, src_lang, src_field,
                   dst_lang, dst_field):
    """Celery task to perform a single translation

    .. Note::

       This is rate-limited at 60/m.

       We really want the translate call to be rate-limited based on
       the translation system, but given we're only supporting Gengo
       right now, I'm going to do the rate limiting here across all
       translation systems rather than figure out how to do it just
       for Gengo.

    :arg instance_key: The key for the instance we're translating
    :arg system: The name of the translation system to use
    :arg src_lang: The source language
    :arg src_field: The field in the instance holding the text to
         translate
    :arg dst_lang: The destination language
    :arg dst_field: The field in the instance to shove the translated
        text into

    """
    instance = key_to_instance(instance_key)
    translate(instance, system, src_lang, src_field, dst_lang, dst_field)
Exemplo n.º 4
0
    def test_translate_dennis(self):
        obj = SuperModel(locale='fr', desc=u'This is a test string')
        obj.save()

        assert obj.trans_desc == u''
        translate(obj, 'dennis', 'br', 'desc', 'en', 'trans_desc')
        assert obj.trans_desc == u'\xabTHIS IS A TEST STRING\xbb'
Exemplo n.º 5
0
    def test_translate_fake(self):
        obj = SuperModel(locale='br', desc=u'This is a test string')
        obj.save()

        assert obj.trans_desc == u''
        translate(obj, 'fake', 'br', 'desc', 'en', 'trans_desc')
        assert obj.trans_desc == u'THIS IS A TEST STRING'
Exemplo n.º 6
0
    def test_translate_gengo_machine_unsupported_language(self):
        """Translation should handle unsupported languages without erroring"""
        gengo_utils.GENGO_LANGUAGE_CACHE = ({
            u'opstat':
            u'ok',
            u'response': [{
                u'unit_type': u'word',
                u'localized_name': u'Deutsch',
                u'lc': u'de',
                u'language': u'German'
            }]
        }, (u'de', ))

        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            gengo_mock_instance = GengoMock.return_value

            obj = SuperModel(locale='es', desc=u'Muy lento')
            obj.save()

            eq_(obj.trans_desc, u'')
            translate(obj, 'gengo_machine', 'es', 'desc', 'en', 'trans_desc')
            eq_(obj.trans_desc, u'')

            # Make sure we don't call postTranslationJobs().
            eq_(gengo_mock_instance.postTranslationJobs.call_count, 0)
Exemplo n.º 7
0
    def test_translate_fake(self):
        obj = SuperModel(locale='br', desc=u'This is a test string')
        obj.save()

        assert obj.trans_desc == u''
        translate(obj, 'fake', 'br', 'desc', 'en', 'trans_desc')
        assert obj.trans_desc == u'THIS IS A TEST STRING'
Exemplo n.º 8
0
    def test_translate_gengo_human_unsupported_pair(self):
        obj = SuperModel(locale='el', desc=u'This is really greek.')
        obj.save()

        assert obj.trans_desc == u''
        translate(obj, 'gengo_human', 'el', 'desc', 'en', 'trans_desc')
        # el -> en is not a supported pair, so it shouldn't get translated.
        assert obj.trans_desc == u''
Exemplo n.º 9
0
    def test_translate_gengo_human_unsupported_pair(self):
        obj = SuperModel(locale='el', desc=u'This is really greek.')
        obj.save()

        assert obj.trans_desc == u''
        translate(obj, 'gengo_human', 'el', 'desc', 'en', 'trans_desc')
        # el -> en is not a supported pair, so it shouldn't get translated.
        assert obj.trans_desc == u''
Exemplo n.º 10
0
    def test_translate_gengo_human_english_copy_over(self):
        obj = SuperModel(locale='es', desc=u'This is English.')
        obj.save()

        assert obj.trans_desc == u''
        translate(obj, 'gengo_human', 'es', 'desc', 'en', 'trans_desc')
        # If the guesser guesses English, then we just copy it over.
        assert obj.trans_desc == u'This is English.'
Exemplo n.º 11
0
    def test_translate_gengo_human_english_copy_over(self):
        obj = SuperModel(locale='es', desc=u'This is English.')
        obj.save()

        assert obj.trans_desc == u''
        translate(obj, 'gengo_human', 'es', 'desc', 'en', 'trans_desc')
        # If the guesser guesses English, then we just copy it over.
        assert obj.trans_desc == u'This is English.'
Exemplo n.º 12
0
    def test_translate_gengo_human(self):
        # Note: This just sets up the GengoJob--it doesn't create any
        # Gengo human translation jobs.
        obj = SuperModel(
            locale='es', desc=u'Facebook no se puede enlazar con peru')
        obj.save()

        assert obj.trans_desc == u''
        translate(obj, 'gengo_human', 'es', 'desc', 'en', 'trans_desc')
        # Nothing should be translated
        assert obj.trans_desc == u''

        assert len(GengoJob.objects.all()) == 1
Exemplo n.º 13
0
    def test_translate_gengo_human(self):
        # Note: This just sets up the GengoJob--it doesn't create any
        # Gengo human translation jobs.
        obj = SuperModel(locale='es',
                         desc=u'Facebook no se puede enlazar con peru')
        obj.save()

        assert obj.trans_desc == u''
        translate(obj, 'gengo_human', 'es', 'desc', 'en', 'trans_desc')
        # Nothing should be translated
        assert obj.trans_desc == u''

        assert len(GengoJob.objects.all()) == 1
Exemplo n.º 14
0
    def test_translate_gengo_machine_english_copy_over(self):
        """If the guesser guesses english, we copy it over"""
        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            gengo_mock_instance = GengoMock.return_value

            obj = SuperModel(locale='es', desc=u'This is English.')
            obj.save()

            eq_(obj.trans_desc, u'')
            translate(obj, 'gengo_machine', 'es', 'desc', 'en', 'trans_desc')
            eq_(obj.trans_desc, u'This is English.')

            # Make sure we don't call postTranslationJobs().
            eq_(gengo_mock_instance.postTranslationJobs.call_count, 0)
Exemplo n.º 15
0
    def test_translate_gengo_machine_unknown_language(self):
        """Translation should handle unknown languages without erroring"""
        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            gengo_mock_instance = GengoMock.return_value

            obj = SuperModel(locale='es', desc=u'Muy lento')
            obj.save()

            eq_(obj.trans_desc, u'')
            translate(obj, 'gengo_machine', 'es', 'desc', 'en', 'trans_desc')
            eq_(obj.trans_desc, u'')

            # Make sure we don't call postTranslationJobs().
            eq_(gengo_mock_instance.postTranslationJobs.call_count, 0)
Exemplo n.º 16
0
    def test_translate_gengo_machine_unknown_language(self):
        """Translation should handle unknown languages without erroring"""
        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            gengo_mock_instance = GengoMock.return_value

            obj = SuperModel(locale='es', desc=u'Muy lento')
            obj.save()

            eq_(obj.trans_desc, u'')
            translate(obj, 'gengo_machine', 'es', 'desc', 'en', 'trans_desc')
            eq_(obj.trans_desc, u'')

            # Make sure we don't call postTranslationJobs().
            eq_(gengo_mock_instance.postTranslationJobs.call_count, 0)
Exemplo n.º 17
0
    def test_translate_gengo_machine_english_copy_over(self):
        """If the guesser guesses english, we copy it over"""
        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            gengo_mock_instance = GengoMock.return_value

            obj = SuperModel(locale='es', desc=u'This is English.')
            obj.save()

            eq_(obj.trans_desc, u'')
            translate(obj, 'gengo_machine', 'es', 'desc', 'en', 'trans_desc')
            eq_(obj.trans_desc, u'This is English.')

            # Make sure we don't call postTranslationJobs().
            eq_(gengo_mock_instance.postTranslationJobs.call_count, 0)
Exemplo n.º 18
0
    def test_gengo_machine_translation(self):
        # Note: This doesn't work in the sandbox, so we skip it if
        # we're in sandbox mode. That is some happy horseshit, but so
        # it goes.

        # Note: This test might be brittle since it's calling out to
        # Gengo to do a machine translation and it's entirely possible
        # that they might return a different translation some day.
        obj = SuperModel(locale='es',
                         desc=u'Facebook no se puede enlazar con peru')
        obj.save()

        eq_(obj.trans_desc, u'')
        translate(obj, 'gengo_machine', 'es', 'desc', 'en', 'trans_desc')
        eq_(obj.trans_desc, u'Facebook can bind with peru')
Exemplo n.º 19
0
    def test_no_translate_if_disabled(self):
        """No GengoAPI calls if gengosystem switch is disabled"""
        with patch('fjord.translations.models.waffle') as waffle_mock:
            waffle_mock.switch_is_active.return_value = False

            with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
                obj = SuperModel(locale='es', desc=u'Muy lento')
                obj.save()

                eq_(obj.trans_desc, u'')
                translate(obj, 'gengo_machine', 'es', 'desc', 'en',
                          'trans_desc')
                eq_(obj.trans_desc, u'')

                # We should not have used the API at all.
                eq_(GengoMock.called, False)
Exemplo n.º 20
0
    def test_no_translate_if_disabled(self):
        """No GengoAPI calls if gengosystem switch is disabled"""
        with patch('fjord.translations.models.waffle') as waffle_mock:
            waffle_mock.switch_is_active.return_value = False

            with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
                obj = SuperModel(locale='es', desc=u'Muy lento')
                obj.save()

                eq_(obj.trans_desc, u'')
                translate(obj, 'gengo_machine', 'es', 'desc', 'en',
                          'trans_desc')
                eq_(obj.trans_desc, u'')

                # We should not have used the API at all.
                eq_(GengoMock.called, False)
Exemplo n.º 21
0
    def test_gengo_machine_translation(self):
        # Note: This doesn't work in the sandbox, so we skip it if
        # we're in sandbox mode. That is some happy horseshit, but so
        # it goes.

        # Note: This test might be brittle since it's calling out to
        # Gengo to do a machine translation and it's entirely possible
        # that they might return a different translation some day.
        obj = SuperModel(
            locale='es',
            desc=u'Facebook no se puede enlazar con peru'
        )
        obj.save()

        eq_(obj.trans_desc, u'')
        translate(obj, 'gengo_machine', 'es', 'desc', 'en', 'trans_desc')
        eq_(obj.trans_desc, u'Facebook can bind with peru')
Exemplo n.º 22
0
    def test_no_translate_if_disabled(self):
        """No GengoAPI calls if gengosystem switch is disabled"""
        with patch('fjord.translations.models.waffle') as waffle_mock:
            waffle_mock.switch_is_active.return_value = False

            with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
                # Note: This just sets up the GengoJob--it doesn't
                # create any Gengo human translation jobs.
                obj = SuperModel(
                    locale='es', desc=u'Facebook no se puede enlazar con peru')
                obj.save()

                assert obj.trans_desc == u''
                translate(obj, 'gengo_human', 'es', 'desc', 'en', 'trans_desc')
                assert obj.trans_desc == u''

                # Verify no jobs were created
                assert len(GengoJob.objects.all()) == 0

                # Verify we didn't call the API at all.
                assert GengoMock.called == False
Exemplo n.º 23
0
    def test_no_translate_if_disabled(self):
        """No GengoAPI calls if gengosystem switch is disabled"""
        with patch('fjord.translations.models.waffle') as waffle_mock:
            waffle_mock.switch_is_active.return_value = False

            with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
                # Note: This just sets up the GengoJob--it doesn't
                # create any Gengo human translation jobs.
                obj = SuperModel(locale='es',
                                 desc=u'Facebook no se puede enlazar con peru')
                obj.save()

                assert obj.trans_desc == u''
                translate(obj, 'gengo_human', 'es', 'desc', 'en', 'trans_desc')
                assert obj.trans_desc == u''

                # Verify no jobs were created
                assert len(GengoJob.objects.all()) == 0

                # Verify we didn't call the API at all.
                assert GengoMock.called is False
Exemplo n.º 24
0
    def test_translate_gengo_machine_unsupported_language(self):
        """Translation should handle unsupported languages without erroring"""
        gengo_utils.GENGO_LANGUAGE_CACHE = (
            {u'opstat': u'ok',
             u'response': [
                 {u'unit_type': u'word', u'localized_name': u'Deutsch',
                  u'lc': u'de', u'language': u'German'}
             ]},
            (u'de',)
        )

        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            gengo_mock_instance = GengoMock.return_value

            obj = SuperModel(locale='es', desc=u'Muy lento')
            obj.save()

            eq_(obj.trans_desc, u'')
            translate(obj, 'gengo_machine', 'es', 'desc', 'en', 'trans_desc')
            eq_(obj.trans_desc, u'')

            # Make sure we don't call postTranslationJobs().
            eq_(gengo_mock_instance.postTranslationJobs.call_count, 0)
Exemplo n.º 25
0
    def test_translate_gengo_machine(self):
        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            # Note: We're mocking with "Muy lento" because it's
            # short, but the Gengo language guesser actually can't
            # figure out what language that is.
            instance = GengoMock.return_value
            instance.postTranslationJobs.return_value = {
                u'opstat': u'ok',
                u'response': {
                    u'jobs': {
                        u'job_1': {
                            u'status': u'approved',
                            u'job_id': u'NULL',
                            u'credits': 0,
                            u'unit_count': 7,
                            u'body_src': u'Muy lento',
                            u'mt': 1,
                            u'eta': -1,
                            u'custom_data': u'10101',
                            u'tier': u'machine',
                            u'lc_tgt': u'en',
                            u'lc_src': u'es',
                            u'body_tgt': u'Very slow',
                            u'slug': u'Input machine translation',
                            u'ctime': u'2014-05-21 15:09:50.361847'
                        }
                    }
                }
            }

            obj = SuperModel(locale='es', desc=u'Muy lento')
            obj.save()

            eq_(obj.trans_desc, u'')
            translate(obj, 'gengo_machine', 'es', 'desc', 'en', 'trans_desc')
            eq_(obj.trans_desc, u'Very slow')
Exemplo n.º 26
0
    def test_translate_gengo_machine(self):
        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            # Note: We're mocking with "Muy lento" because it's
            # short, but the Gengo language guesser actually can't
            # figure out what language that is.
            instance = GengoMock.return_value
            instance.postTranslationJobs.return_value = {
                u'opstat': u'ok',
                u'response': {
                    u'jobs': {
                        u'job_1': {
                            u'status': u'approved',
                            u'job_id': u'NULL',
                            u'credits': 0,
                            u'unit_count': 7,
                            u'body_src': u'Muy lento',
                            u'mt': 1,
                            u'eta': -1,
                            u'custom_data': u'10101',
                            u'tier': u'machine',
                            u'lc_tgt': u'en',
                            u'lc_src': u'es',
                            u'body_tgt': u'Very slow',
                            u'slug': u'Input machine translation',
                            u'ctime': u'2014-05-21 15:09:50.361847'
                        }
                    }
                }
            }

            obj = SuperModel(locale='es', desc=u'Muy lento')
            obj.save()

            eq_(obj.trans_desc, u'')
            translate(obj, 'gengo_machine', 'es', 'desc', 'en', 'trans_desc')
            eq_(obj.trans_desc, u'Very slow')