def tmx_import(self, howmuch, file, REQUEST=None, RESPONSE=None): """Imports a TMX level 1 file. """ try: data = file.read() tmx = TMX(string=data) except: return MessageDialog( title="Parse error", message=_("impossible to parse the file"), action="manage_Import_form" ) num_notes = 0 num_trans = 0 if howmuch == "clear": # Clear the message catalogue prior to import self._messages = {} self._languages = () self._default_language = tmx.get_srclang() for (id, msg) in tmx.state.messages.items(): if not self._messages.has_key(id) and howmuch == "existing": pass else: msg.msgstr.pop(self._default_language) if not self._messages.has_key(id): self._messages[id] = {} for lang in msg.msgstr.keys(): # normalize the languageTag and extract the core (core, local) = LanguageTag.decode(lang) lang = LanguageTag.encode((core, local)) if lang not in self._languages: self._languages += (lang,) if msg.msgstr[lang].text: self._messages[id][lang] = msg.msgstr[lang].text if core != lang and core != self._default_language: if core not in self._languages: self._languages += (core,) if not msg.msgstr.has_key(core): self._messages[id][core] = msg.msgstr[lang].text if msg.notes: ns = [m.text for m in msg.notes] self._messages[id]["note"] = u" ".join(ns) num_notes += 1 num_trans += 1 if REQUEST is not None: return MessageDialog( title=_(u"Messages imported"), message=_(u"Imported %d messages and %d notes") % (num_trans, num_notes), action="manage_messages", )
def tmx_export(self, REQUEST, RESPONSE=None): """Exports the content of the message catalog to a TMX file """ orglang = self._default_language # Get the header info header = self.get_po_header(orglang) charset = header["charset"] # build data structure for the xml header xml_header = {} xml_header["standalone"] = -1 xml_header["xml_version"] = u"1.0" xml_header["document_type"] = (u"tmx", u"http://www.lisa.org/tmx/tmx14.dtd") # build data structure for the tmx header version = u"1.4" tmx_header = {} tmx_header["creationtool"] = u"Localizer" tmx_header["creationtoolversion"] = u"1.x" tmx_header["datatype"] = u"plaintext" tmx_header["segtype"] = u"paragraph" tmx_header["adminlang"] = u"%s" % orglang tmx_header["srclang"] = u"%s" % orglang tmx_header["o-encoding"] = u"%s" % charset.lower() # handle messages d = {} filename = "%s.tmx" % self.id for msgkey, transunit in self._messages.items(): sentences = {} for lang in transunit.keys(): if lang != "note": s = Sentence(transunit[lang], {"lang": "%s" % lang}) sentences[lang] = s if orglang not in transunit.keys(): s = Sentence(msgkey, {"lang": "%s" % orglang}) sentences[orglang] = s if transunit.has_key("note"): d[msgkey] = Message(sentences, {}, [Note(transunit.get("note"))]) else: d[msgkey] = Message(sentences) tmx = TMX() tmx.build(xml_header, version, tmx_header, d) if RESPONSE is not None: RESPONSE.setHeader("Content-type", "application/data") RESPONSE.setHeader("Content-Disposition", 'attachment; filename="%s"' % filename) return tmx.to_str()
def tmx_export(self, REQUEST, RESPONSE=None): """ Exports the content of the message catalog to a TMX file """ langorg = self._default_language # build data structure for the xml header xml_header = {} xml_header['standalone'] = -1 xml_header['xml_version'] = u'1.0' xml_header['document_type'] = (u'tmx', u'http://www.lisa.org/tmx/tmx14.dtd') # build data structure for the tmx header version = u'1.4' tmx_header = {} tmx_header['creationtool'] = u'Localizer' tmx_header['creationtoolversion'] = u'1.x' tmx_header['datatype'] = u'plaintext' tmx_header['segtype'] = u'paragraph' tmx_header['adminlang'] = u'%s' % langorg tmx_header['srclang'] = u'%s' % langorg tmx_header['o-encoding'] = u'utf-8' # Get the messages, and perhaps its translations. d = {} filename = '%s.tmx' % self.id for k in self._local_properties.keys(): sentences = {} for lang in self._languages: trans, fuzzy = self.get_localproperty(k, lang) sentences[lang] = Sentence(trans, {'lang':lang}) d[self.get_localproperty(k, langorg)[0]] = Message(sentences) tmx = TMX() tmx.build(xml_header, version, tmx_header, d) if RESPONSE is not None: RESPONSE.setHeader('Content-type','application/data') RESPONSE.setHeader('Content-Disposition', 'attachment; filename="%s"' % filename) return tmx.to_str()