예제 #1
0
def _compile_translation_template(resource=None, language=None, skip=False):
    """
    Given a resource and a language we create the translation file
    """
    handler = registry.handler_for(resource.i18n_method)
    handler.bind_resource(resource)
    handler.set_language(language)

    if not skip:
        # Monkey-patch handler to combine results with the source locale
        def combine_strings(source_entities, language):
            source_entities = list(source_entities)
            result = old_get_strings(source_entities, language)
            for entity in source_entities:
                if not result.get(entity, None):
                    trans = handler._get_translation(entity, source_language, 5)
                    if trans:
                        logger.debug(trans.string)
                        result[entity] = trans.string
            return result
        source_language = resource.source_language
        old_get_strings = handler._get_translation_strings
        handler._get_translation_strings = combine_strings

    handler.compile()

    if not skip:
        handler._get_translation_strings = old_get_strings

    return handler.compiled_template
예제 #2
0
def _compile_translation_template(resource=None, language=None, skip=False):
    """
    Given a resource and a language we create the translation file
    """
    handler = registry.handler_for(resource.i18n_method)
    handler.bind_resource(resource)
    handler.set_language(language)

    if not skip:
        # Monkey-patch handler to combine results with the source locale
        def combine_strings(source_entities, language):
            source_entities = list(source_entities)
            result = old_get_strings(source_entities, language)
            for entity in source_entities:
                if not result.get(entity, None):
                    trans = handler._get_translation(entity, source_language,
                                                     5)
                    if trans:
                        logger.debug(trans.string)
                        result[entity] = trans.string
            return result

        source_language = resource.source_language
        old_get_strings = handler._get_translation_strings
        handler._get_translation_strings = combine_strings

    handler.compile()

    if not skip:
        handler._get_translation_strings = old_get_strings

    return handler.compiled_template
예제 #3
0
 def test_normal_types(self):
     for method in settings.I18N_METHODS:
         if method not in ('PO', 'POT', ):
             resource = Mock()
             resource.__dict__['i18n_type'] = method
             handler = self.appropriate_handler(resource, None)
             self.assertIsInstance(
                 handler, type(registry.handler_for(method))
             )
예제 #4
0
    def test_pseudo_file(self):
        """Test Pseudo translation generation based on FORMATS var dict."""
        for i18n_type, v in FORMATS.items():

            #if i18n_type != "INI": continue

            # Set i18n_type for resource
            self.resource.i18n_type = i18n_type
            self.resource.save()

            # Set a file, resource and language for the resource
            handler = registry.handler_for(i18n_type)
            handler.bind_file(v['file'])
            handler.bind_resource(self.resource)
            handler.set_language(self.language)
            handler.parse_file(is_source=True)
            handler.save2db(is_source=True)

            # For each pseudo type that exists, try to generate files in the
            # supported i18n formats supported.
            for pseudo_type in settings.PSEUDO_TYPES.keys():

                #if pseudo_type != "MIXED": continue

                # Get Pseudo type class
                pseudo_class = import_to_python(
                    settings.PSEUDO_TYPE_CLASSES[pseudo_type])

                # Create a PseudoType instance and set it into the handler
                handler.bind_pseudo_type(pseudo_class(self.resource.i18n_type))

                # Compile file and check encoding
                handler.compile()
                file_content = handler.compiled_template
                if not isinstance(file_content, unicode):
                    try:
                        file_content = file_content.decode('utf-8')
                    except UnicodeDecodeError:
                        file_content = file_content.decode('iso-8859-1')


                #FIXME: We have a bug related to spaces being escaped in
                # .properties files. This can be dropped after fixing it.
                if i18n_type == 'PROPERTIES' and \
                    pseudo_type in ['PLANGUAGE', 'UNICODE']:
                    file_content = file_content.replace('\\ ', ' ')

                # Assert expected value in the generated file
                for message in v['pseudo_messages'][pseudo_type]:
                    logger.debug(file_content)
                    logger.debug("-----------------")
                    logger.debug(message)
                    logger.debug(i18n_type)
                    logger.debug(pseudo_type)
                    logger.debug("-----------------")
                    self.assertIn(message, file_content)
예제 #5
0
    def test_put(self, _mock, mock):
        """Test PUT method for the API.

        Test filenames used.
        """
        # JSON APi
        mock.return_value = {
            "strings_added": 0,
            "strings_updated": 0,
            "redirect": reverse("resource_detail", args=[self.resource.project.slug, self.resource.slug]),
        }
        self.client["maintainer"].put(
            self.translation_url, data=simplejson.dumps({"content": ""}), content_type="application/json"
        )
        self.assertTrue(mock.called)
        used_handler = mock.call_args[0][0]
        self.assertIsInstance(used_handler, type(registry.handler_for("PO")))

        res = self.client["maintainer"].put(
            self.source_url, data=simplejson.dumps({"content": ""}), content_type="application/json"
        )
        self.assertTrue(mock.called)
        self.assertIsInstance(used_handler, type(registry.handler_for("PO")))

        # filename API
        pofile_path = os.path.join(settings.TX_ROOT, "resources/tests/lib/pofile")
        po_filename = os.path.join(pofile_path, "pt_BR.po")
        pot_filename = os.path.join(pofile_path, "general/test.pot")
        po_class = type(registry.handler_for("PO"))
        pot_class = type(registry.handler_for("POT"))

        for url in (self.source_url, self.translation_url):
            with open(po_filename) as f:
                self.client["maintainer"].put(url, data={"name": "name.po", "attachment": f})
                self.assertTrue(mock.called)
                used_handler = mock.call_args[0][0]
                self.assertIsInstance(used_handler, po_class)
            with open(pot_filename) as f:
                self.client["maintainer"].put(url, data={"name": "name.po", "attachment": f})
                self.assertTrue(mock.called)
                used_handler = mock.call_args[0][0]
                self.assertIsInstance(used_handler, pot_class)
예제 #6
0
    def test_pseudo_file(self):
        """Test Pseudo translation generation based on FORMATS var dict."""
        for i18n_type, v in FORMATS.items():

            #if i18n_type != "INI": continue

            # Set i18n_type for resource
            self.resource.i18n_type = i18n_type
            self.resource.save()

            # Set a file, resource and language for the resource
            handler = registry.handler_for(i18n_type)
            handler.bind_file(v['file'])
            handler.bind_resource(self.resource)
            handler.set_language(self.language)
            handler.parse_file(is_source=True)
            handler.save2db(is_source=True)

            # For each pseudo type that exists, try to generate files in the
            # supported i18n formats supported.
            for pseudo_type in settings.PSEUDO_TYPES.keys():

                #if pseudo_type != "MIXED": continue

                # Get Pseudo type class
                pseudo_class = import_to_python(
                    settings.PSEUDO_TYPE_CLASSES[pseudo_type])

                # Compile file and check encoding
                file_content = handler.compile(
                    pseudo=pseudo_class(self.resource.i18n_type))
                if not isinstance(file_content, unicode):
                    try:
                        file_content = file_content.decode('utf-8')
                    except UnicodeDecodeError:
                        file_content = file_content.decode('iso-8859-1')

                #FIXME: We have a bug related to spaces being escaped in
                # .properties files. This can be dropped after fixing it.
                if i18n_type == 'PROPERTIES' and \
                    pseudo_type in ['PLANGUAGE', 'UNICODE']:
                    file_content = file_content.replace('\\ ', ' ')

                # Assert expected value in the generated file
                for message in v['pseudo_messages'][pseudo_type]:
                    logger.debug(file_content)
                    logger.debug("-----------------")
                    logger.debug(message)
                    logger.debug(i18n_type)
                    logger.debug(pseudo_type)
                    logger.debug("-----------------")
                    self.assertIn(message, file_content)
예제 #7
0
 def find_parser(self):
     from transifex.resources.formats.registry import registry
     i18n_type = registry.guess_method(self.name, self.mime_type)
     if i18n_type is None:
         msg = "Unsupported resource"
         if self.name is not None:
             msg = "Unsupported extension of file: %s" % self.name
         elif self.mimetype is not None:
             msg = "Unsupported mimetype %s" % self.mimetype
         raise FileCheckError(msg)
     if self.name.endswith('pot'):
         i18n_type = 'POT'
     return registry.handler_for(i18n_type)
예제 #8
0
    def test_put(self, import_mock):
        """Check file upload through views.

        Specifically,
        - resource_edit view
        - upload_create_resource_form
        - create_translation_form
        - update_translation_form
        """
        pofile_path = os.path.join(settings.TX_ROOT, "resources/tests/lib/pofile")
        po_filename = os.path.join(pofile_path, "pt_BR.po")
        pot_filename = os.path.join(pofile_path, "general/test.pot")
        po_class = type(registry.handler_for("PO"))
        pot_class = type(registry.handler_for("POT"))

        resource_edit_url = reverse("resource_edit", kwargs={"project_slug": self.pslug, "resource_slug": self.rslug})
        filenames, klasses = [po_filename, pot_filename], [po_class, pot_class]
        for (filename, klass) in zip(filenames, klasses):
            with open(filename) as f:
                self.mclient.post(
                    resource_edit_url, data={"sourcefile": f, "slug": self.resource.slug, "name": self.resource.name}
                )
                self.assertTrue(import_mock.called)
                used_handler = import_mock.call_args[0][0]
                self.assertIsInstance(used_handler, klass)

        # Test template tags
        factory = RequestFactory()

        # Create request for template tags
        url = reverse("project_detail", kwargs={"project_slug": self.pslug})
        for (filename, klass) in zip(filenames, klasses):
            with open(filename) as f:
                request = factory.post(
                    url,
                    data={
                        "create_resource": True,
                        "create_form-name": "Name",
                        "create_form-i18n_method": "PO",
                        "create_form-source_file": f,
                    },
                )
                # we need to patch the request.user object
                with patch.object(request, "user", create=True) as mock:
                    mock.return_value = self.user["maintainer"]
                    upload_create_resource_form(request, self.project)
                    self.assertTrue(import_mock.called)
                    used_handler = import_mock.call_args[0][0]
                    self.assertIsInstance(used_handler, klass)

        with open(po_filename) as f:
            lang_code = "en_US"
            url = reverse(
                "update_translation",
                kwargs={"project_slug": self.pslug, "resource_slug": self.rslug, "lang_code": lang_code},
            )
            request = factory.post(url, data={"name": po_filename, "attachment": f})
            # we need to patch the request.user object for the request
            with patch.object(request, "user", create=True) as mock:
                mock.return_value = self.user["maintainer"]
                update_translation(request, project_slug=self.pslug, resource_slug=self.rslug, lang_code=lang_code)
                self.assertTrue(import_mock.called)
                used_handler = import_mock.call_args[0][0]
                self.assertIsInstance(used_handler, po_class)

            request = factory.post(url, data={"name": po_filename, "attachment": f, "language_code": "en_US"})
            # we need to patch the request.user object for the request
            with patch.object(request, "user", create=True) as mock:
                mock.return_value = self.user["maintainer"]
                update_translation(request, project_slug=self.pslug, resource_slug=self.rslug)
                self.assertTrue(import_mock.called)
                used_handler = import_mock.call_args[0][0]
                self.assertIsInstance(used_handler, po_class)
예제 #9
0
    def test_put(self, _mock, mock):
        """Test PUT method for the API.

        Test filenames used.
        """
        # JSON APi
        mock.return_value = {
            'strings_added': 0,
            'strings_updated': 0,
            'redirect': reverse(
                'resource_detail',
                args=[self.resource.project.slug, self.resource.slug]
            )
        }
        self.client['maintainer'].put(
            self.translation_url,
            data=simplejson.dumps({'content': '', }),
            content_type='application/json'
        )
        self.assertTrue(mock.called)
        used_handler = mock.call_args[0][0]
        self.assertIsInstance(used_handler, type(registry.handler_for('PO')))

        res = self.client['maintainer'].put(
            self.source_url,
            data=simplejson.dumps({'content': '', }),
            content_type='application/json'
        )
        self.assertTrue(mock.called)
        self.assertIsInstance(used_handler, type(registry.handler_for('PO')))

        # filename API
        pofile_path = os.path.join(
            settings.TX_ROOT, 'resources/tests/lib/pofile'
        )
        po_filename = os.path.join(pofile_path, "pt_BR.po")
        pot_filename = os.path.join(pofile_path, "general/test.pot")
        po_class = type(registry.handler_for('PO'))
        pot_class = type(registry.handler_for('POT'))

        for url in (self.source_url, self.translation_url):
            with open(po_filename) as f:
                self.client['maintainer'].put(
                    url, data={
                        'name': 'name.po',
                        'attachment': f
                    },
                )
                self.assertTrue(mock.called)
                used_handler = mock.call_args[0][0]
                self.assertIsInstance(used_handler, po_class)
            with open(pot_filename) as f:
                self.client['maintainer'].put(
                    url, data={
                        'name': 'name.po',
                        'attachment': f
                    },
                )
                self.assertTrue(mock.called)
                used_handler = mock.call_args[0][0]
                self.assertIsInstance(used_handler, pot_class)
예제 #10
0
    def test_put(self, import_mock):
        """Check file upload through views.

        Specifically,
        - resource_edit view
        - upload_create_resource_form
        - create_translation_form
        - update_translation_form
        """
        pofile_path = os.path.join(
            settings.TX_ROOT, 'resources/tests/lib/pofile'
        )
        po_filename = os.path.join(pofile_path, "pt_BR.po")
        pot_filename = os.path.join(pofile_path, "general/test.pot")
        po_class = type(registry.handler_for('PO'))
        pot_class = type(registry.handler_for('POT'))

        resource_edit_url = reverse(
            'resource_edit', kwargs={
                'project_slug': self.pslug,
                'resource_slug': self.rslug,
            }
        )
        filenames, klasses = [po_filename, pot_filename], [po_class, pot_class]
        for (filename, klass) in zip(filenames, klasses):
            with open(filename) as f:
                self.mclient.post(
                    resource_edit_url, data={
                        'sourcefile': f,
                        'slug': self.resource.slug,
                        'name': self.resource.name,
                    }
                )
                self.assertTrue(import_mock.called)
                used_handler = import_mock.call_args[0][0]
                self.assertIsInstance(used_handler, klass)

        # Test template tags
        factory = RequestFactory()

        # Create request for template tags
        url = reverse('project_detail', kwargs={'project_slug': self.pslug})
        for (filename, klass) in zip(filenames, klasses):
            with open(filename) as f:
                request = factory.post(
                    url, data={
                        'create_resource': True,
                        'create_form-name': 'Name',
                        'create_form-i18n_method': 'PO',
                        'create_form-source_file': f
                    }
                )
                # we need to patch the request.user object
                with patch.object(request, 'user', create=True) as mock:
                    mock.return_value = self.user['maintainer']
                    upload_create_resource_form(request, self.project)
                    self.assertTrue(import_mock.called)
                    used_handler = import_mock.call_args[0][0]
                    self.assertIsInstance(used_handler, klass)

        with open(po_filename) as f:
            lang_code = 'en_US'
            url = reverse(
                'update_translation',
                kwargs={
                    'project_slug': self.pslug,
                    'resource_slug': self.rslug,
                    'lang_code': lang_code,
                }
            )
            request = factory.post(
                url, data={
                    'name': po_filename,
                    'attachment': f
                }
            )
            # we need to patch the request.user object for the request
            with patch.object(request, 'user', create=True) as mock:
                mock.return_value = self.user['maintainer']
                update_translation(
                    request, project_slug=self.pslug,
                    resource_slug=self.rslug, lang_code=lang_code
                )
                self.assertTrue(import_mock.called)
                used_handler = import_mock.call_args[0][0]
                self.assertIsInstance(used_handler, po_class)

            request = factory.post(
                url, data={
                    'name': po_filename,
                    'attachment': f,
                    'language_code': 'en_US'
                }
            )
            # we need to patch the request.user object for the request
            with patch.object(request, 'user', create=True) as mock:
                mock.return_value = self.user['maintainer']
                update_translation(
                    request, project_slug=self.pslug, resource_slug=self.rslug
                )
                self.assertTrue(import_mock.called)
                used_handler = import_mock.call_args[0][0]
                self.assertIsInstance(used_handler, po_class)