Exemple #1
0
def _compile_translation_template(resource=None, language=None, pseudo_type=None):
    """
    Given a resource and a language we create the translation file
    """
    parser = get_i18n_handler_from_type(resource.i18n_type)
    handler = parser(resource=resource, language=language)
    if language is not None:
        if pseudo_type:
            handler.bind_pseudo_type(pseudo_type)
        handler.compile()
    else:
        handler.compile_pot()

    return handler.compiled_template
Exemple #2
0
    def test_pseudo_file(self):
        """Test Pseudo translation generation based on FORMATS var dict."""
        for i18n_type, v in FORMATS.items():

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

            # Set a file, resource and language for the resource
            parser = get_i18n_handler_from_type(i18n_type)
            handler = parser(v['file'], resource=self.resource,
                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():

                # 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 type(file_content) != unicode:
                    file_content = file_content.decode('utf-8')

                #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
                self.assertTrue(
                    v['pseudo_messages'][pseudo_type] in file_content)
Exemple #3
0
    def update_source_file(self, fake=False):
        """
        Fetch source file from remote url and import it, updating existing
        entries.
        """
        try:
            file = urllib2.urlopen(self.source_file_url)
        except:
            logger.error("Could not pull source file for resource %s (%s)" %
                (self.resource.full_name, self.source_file_url))
            raise

        filename = ''
        if file.info().has_key('Content-Disposition'):
                # If the response has Content-Disposition, we try to take
                # filename from it
                content = file.info()['Content-Disposition']
                if 'filename' in content:
                    filename = content.split('filename')[1]
                    filename = filename.replace('"', '').replace("'", ""
                        ).replace("=", "").replace('/', '-').strip()

        if filename == '':
            parts = urlparse.urlsplit(self.source_file_url)
            #FIXME: This still might end empty
            filename = parts.path.split('/')[-1]

        sf = StorageFile()
        sf.uuid = str(uuid4())
        sf.name = filename
        fh = open(sf.get_storage_path(), 'wb')
        fh.write(file.read())
        fh.flush()
        fh.close()

        sf.size = os.path.getsize(sf.get_storage_path())
        sf.language = self.resource.source_language

        sf.update_props()
        sf.save()

        try:
            if self.resource.i18n_type:
                parser = get_i18n_handler_from_type(self.resource.i18n_type)
            else:
                parser = sf.find_parser()
                assert parser, "Could not find a suitable handler for this file."
                i18n_type = get_i18n_type_from_file(sf.get_storage_path())
                self.resource.i18n_type = i18n_type
                self.resource.save()
            language = sf.language
            fhandler = parser(filename=sf.get_storage_path())
            fhandler.set_language(language)
            fhandler.bind_resource(self.resource)
            fhandler.contents_check(fhandler.filename)
            fhandler.parse_file(is_source=True)
            strings_added, strings_updated = 0, 0
            if not fake:
                strings_added, strings_updated = fhandler.save2db(is_source=True)
        except Exception,e:
            logger.error("Error importing source file for resource %s.%s (%s): %s" %
                ( self.resource.project.slug, self.resource.slug,
                    self.source_file_url, str(e)))
            raise