def xliff_import(self, howmuch, file, REQUEST=None): """XLIFF is the XML Localization Interchange File Format designed by a group of software providers. It is specified by www.oasis-open.org """ try: data = file.read() xliff = XLIFF(string=data) except: return MessageDialog( title="Parse error", message=_("impossible to parse the file"), action="manage_Import_form" ) num_notes = 0 num_trans = 0 (file_ids, sources, targets) = xliff.get_languages() if howmuch == "clear": # Clear the message catalogue prior to import self._messages = {} self._languages = () self._default_language = sources[0] # update languages if len(sources) > 1 or sources[0] != self._default_language: return MessageDialog( title="Language error", message=_("incompatible language sources"), action="manage_Import_form" ) for lang in targets: if lang != self._default_language and lang not in self._languages: self._languages += (lang,) # get messages for file in xliff.state.files: cur_target = file.attributes.get("target-language", "") for msg in file.body.keys(): if not self._messages.has_key(msg) and howmuch == "existing": pass else: if not self._messages.has_key(msg): self._messages[msg] = {} if cur_target and file.body[msg].target: self._messages[msg][cur_target] = file.body[msg].target num_trans += 1 if file.body[msg].notes: ns = [n.text for n in file.body[msg].notes] comment = " ".join(ns) self._messages[msg]["note"] = comment num_notes += 1 if REQUEST is not None: return MessageDialog( title=_(u"Messages imported"), message=(_(u"Imported %d messages and %d notes to %s") % (num_trans, num_notes, " ".join(targets))), action="manage_messages", )
def xliff_export(self, targetlang, export_all=1, REQUEST=None, RESPONSE=None): """ Exports the content of the message catalog to an XLIFF file """ orglang = self._default_language export_all = int(export_all) from DateTime import DateTime # Generate the XLIFF file header RESPONSE.setHeader('Content-Type', 'text/xml; charset=UTF-8') RESPONSE.setHeader('Content-Disposition', 'attachment; filename="%s_%s_%s.xlf"' % ( self.id, orglang, targetlang )) # 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'xliff', u'http://www.oasis-open.org/committees/xliff/documents/xliff.dtd') version = u'1.0' # build the data-stucture for the File tag attributes = {} attributes['original'] = u'/%s' % self.absolute_url(1) attributes['product-name'] = u'Localizer' attributes['product-version'] = u'1.1.x' attributes['data-type'] = u'plaintext' attributes['source-language'] = orglang attributes['target-language'] = targetlang attributes['date'] = DateTime().HTML4() # Get the messages, and perhaps its translations. d = {} for prop in self._local_properties.keys(): target, fuzzy = self.get_localproperty(prop, targetlang) msgkey, fuzzy = self.get_localproperty(prop, self._default_language) # if export_all=1 export all messages otherwise export # only untranslated messages if export_all or not target: id = md5text(msgkey) if target: t = Translation(msgkey, target, {'id':id}) else: t = Translation(msgkey, msgkey, {'id':id}) d[msgkey] = t files = [xliff_File(d, attributes)] xliff = XLIFF() xliff.build(xml_header, version, files) return xliff.to_str()
def xliff_export(self, x, export_all=1, REQUEST=None, RESPONSE=None): """Exports the content of the message catalog to an XLIFF file """ orglang = self._default_language export_all = int(export_all) from DateTime import DateTime # Generate the XLIFF file header RESPONSE.setHeader("Content-Type", "text/xml; charset=UTF-8") RESPONSE.setHeader("Content-Disposition", 'attachment; filename="%s_%s_%s.xlf"' % (self.id, orglang, x)) # 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"xliff", u"http://www.oasis-open.org/committees/xliff/documents/xliff.dtd") version = u"1.0" # build the data-stucture for the File tag attributes = {} attributes["original"] = u"/%s" % self.absolute_url(1) attributes["product-name"] = u"Localizer" attributes["product-version"] = u"1.1.x" attributes["data-type"] = u"plaintext" attributes["source-language"] = orglang attributes["target-language"] = x attributes["date"] = DateTime().HTML4() # Get the messages, and perhaps its translations. d = {} for msgkey, transunit in self._messages.items(): target = transunit.get(x, "") # if export_all=1 export all messages otherwise export # only untranslated messages if export_all or not target: id = md5text(msgkey) notes = [] if transunit.has_key("note") and transunit["note"]: notes = [xliff_Note(transunit["note"])] if target: t = Translation(msgkey, target, {"id": id}, notes) else: t = Translation(msgkey, msgkey, {"id": id}, notes) d[msgkey] = t files = [xliff_File(d, attributes)] xliff = XLIFF() xliff.build(xml_header, version, files) return xliff.to_str()
def xliff_import(self, file, REQUEST=None): """ XLIFF is the XML Localization Interchange File Format designed by a group of software providers. It is specified by www.oasis-open.org """ try: data = file.read() xliff = XLIFF(string=data) except: return MessageDialog(title = 'Parse error', message = _('impossible to parse the file') , action = 'manage_import',) num_trans = 0 (file_ids, sources, targets) = xliff.get_languages() # update languages if len(sources) > 1 or sources[0] != self._default_language: return MessageDialog(title = 'Language error', message = _('incompatible language sources') , action = 'manage_import',) for lang in targets: if lang != self._default_language and lang not in self._languages: self._languages += (lang,) # get messages for file in xliff.state.files: cur_target = file.attributes.get('target-language', '') for msg in file.body.keys(): for (prop, val) in self._local_properties.items(): if val[self._default_language][0] == msg: if cur_target and file.body[msg].target: texte = file.body[msg].target self.set_localpropvalue(prop, cur_target, texte) num_trans += 1 if REQUEST is not None: return MessageDialog( title = _(u'Messages imported'), message = (_(u'Imported %d messages to %s') % (num_trans, ' '.join(targets))), action = 'manage_localPropertiesForm')