Example #1
0
    def spreadsheet_export(self, lang, dialect, encoding='utf-8'):
        """ Exports the content of the message catalog to a spreadsheet format """
        from Products.NaayaCore.managers.utils import spreadsheet_file
        if dialect == 'excel':
            ct, ext = 'text/comma-separated-values', 'csv'
        else:
            ct, ext = 'text/tab-separated-values', 'txt'

        translations = {}
        orglang = self._catalog._default_language
        for msgkey, transunit in self._catalog.messages():
            if isinstance(msgkey, unicode):
                msgkey = msgkey.encode(encoding)
            try:
                translations[msgkey] = transunit[lang]
            except KeyError:
                translations[msgkey] = ""

        #sort translations
        tkeys = translations.keys()
        tkeys.sort()

        #build headers
        output = [('source', 'target')]
        output_app = output.append  #optimisations

        #build content
        for msgkey in tkeys:
            translation = translations[msgkey]
            if isinstance(msgkey, unicode):
                msgkey = msgkey.encode(encoding)
            else:
                msgkey = unicode(msgkey, 'utf-8').encode(encoding)
            if isinstance(translation, unicode):
                translation = translation.encode(encoding)
            else:
                translation = unicode(translation, 'utf-8').encode(encoding)

            output_app((msgkey, translation))

        #generate a temporary file on the filesystem that will be used to return the actual output
        tmp_name = spreadsheet_file(output, dialect)

        #return spreadsheet file
        content = open(str(tmp_name)).read()
        headers = [
            ('Content-Type', '%s;charset=%s' % (ct, encoding)),
            ('Content-Disposition',
             'attachment; filename=%s' % '%s-%s.%s' % (orglang, lang, ext))
        ]
        return (headers, content)
Example #2
0
    def spreadsheet_export(self,
                           target_lang,
                           dialect,
                           encoding='utf-8',
                           REQUEST=None,
                           RESPONSE=None):
        """ Exports the content of the message catalog to a spreadsheet format """

        site = self.getSite()
        default_lang = site.gl_get_default_language()
        if dialect == 'excel':
            ct, ext = 'text/comma-separated-values', 'csv'
        else:
            ct, ext = 'text/tab-separated-values', 'txt'

        translations = {}
        for msgkey, transunit in self._messages.items():
            try:
                translations[msgkey] = transunit[target_lang]
            except KeyError:
                translations[msgkey] = ""

        #sort translations
        tkeys = translations.keys()
        tkeys.sort()

        #build headers
        output = [('source', 'target')]
        output_app = output.append  #optimisations

        #build content
        for msgkey in tkeys:
            try:
                output_app((unicode(msgkey, 'utf-8').encode(encoding),
                            unicode(translations[msgkey],
                                    'utf-8').encode(encoding)))
            except TypeError:
                output_app((msgkey.encode(encoding),
                            translations[msgkey].encode(encoding)))

        #generate a temporary file on the filesystem that will be used to return the actual output
        tmp_name = spreadsheet_file(output, dialect)

        #return spreadsheet file
        content = open(str(tmp_name)).read()
        RESPONSE.setHeader('Content-Type', '%s;charset=%s' % (ct, encoding))
        RESPONSE.setHeader(
            'Content-Disposition', 'attachment; filename=%s' % '%s-%s.%s' %
            (default_lang, target_lang, ext))
        return content
Example #3
0
    def spreadsheet_export(self, lang, dialect, encoding='utf-8'):
        """ Exports the content of the message catalog to a spreadsheet format """
        from Products.NaayaCore.managers.utils import spreadsheet_file
        if dialect == 'excel':
            ct, ext = 'text/comma-separated-values', 'csv'
        else:
            ct, ext = 'text/tab-separated-values', 'txt'

        translations = {}
        orglang = self._catalog._default_language
        for msgkey, transunit in self._catalog.messages():
            if isinstance(msgkey, unicode):
                msgkey = msgkey.encode(encoding)
            try:
                translations[msgkey] = transunit[lang]
            except KeyError:
                translations[msgkey] = ""

        #sort translations
        tkeys = translations.keys()
        tkeys.sort()

        #build headers
        output = [('source', 'target')]
        output_app = output.append  #optimisations

        #build content
        for msgkey in tkeys:
            translation = translations[msgkey]
            if isinstance(msgkey, unicode):
                msgkey = msgkey.encode(encoding)
            else:
                msgkey = unicode(msgkey, 'utf-8').encode(encoding)
            if isinstance(translation, unicode):
                translation = translation.encode(encoding)
            else:
                translation = unicode(translation, 'utf-8').encode(encoding)

            output_app((msgkey, translation))

        #generate a temporary file on the filesystem that will be used to return the actual output
        tmp_name = spreadsheet_file(output, dialect)

        #return spreadsheet file
        content = open(str(tmp_name)).read()
        headers = [('Content-Type', '%s;charset=%s' % (ct, encoding)),
                   ('Content-Disposition', 'attachment; filename=%s' % '%s-%s.%s' % (orglang, lang, ext))]
        return (headers, content)
Example #4
0
 def spreadsheet_export(self, target_lang, dialect, encoding='utf-8', REQUEST=None, RESPONSE=None):
     """ Exports the content of the message catalog to a spreadsheet format """
     
     site = self.getSite()
     default_lang = site.gl_get_default_language()
     if dialect == 'excel':
         ct, ext = 'text/comma-separated-values', 'csv'
     else:
         ct, ext = 'text/tab-separated-values', 'txt'
         
     translations = {}
     for msgkey, transunit in self._messages.items():
         try:
             translations[msgkey] = transunit[target_lang]
         except KeyError:
             translations[msgkey] = ""
             
     #sort translations
     tkeys = translations.keys()
     tkeys.sort()
     
     #build headers
     output = [('source', 'target')]
     output_app = output.append  #optimisations
     
     #build content
     for msgkey in tkeys:
         try:
             output_app((unicode(msgkey, 'utf-8').encode(encoding), unicode(translations[msgkey], 'utf-8').encode(encoding)))
         except TypeError:
             output_app((msgkey.encode(encoding), translations[msgkey].encode(encoding)))
         
     #generate a temporary file on the filesystem that will be used to return the actual output
     tmp_name = spreadsheet_file(output, dialect)
     
     #return spreadsheet file
     content = open(str(tmp_name)).read()
     RESPONSE.setHeader('Content-Type', '%s;charset=%s' % (ct, encoding))
     RESPONSE.setHeader('Content-Disposition', 'attachment; filename=%s' % '%s-%s.%s' % (default_lang, target_lang, ext))
     return content