def convertmessage( self, contextname, messagenum, source, target, disambiguation, msgcomments, transtype, ): """makes a pounit from the given message""" thepo = po.pounit(encoding="UTF-8") thepo.addlocation("%s#%d" % (contextname, messagenum)) thepo.source = source if not self.pot: thepo.target = target if len(disambiguation) > 0: thepo.setcontext(disambiguation) if len(msgcomments) > 0: thepo.addnote(msgcomments) if transtype == "unfinished" and thepo.istranslated(): thepo.markfuzzy() if transtype == "obsolete": # This should use the Gettext obsolete method but it would require quite a bit of work thepo.addnote("(obsolete)", origin="developer") # using the fact that -- quote -- "(this is nonsense)" return thepo
def convertmixedunit(self, labeldtd, accesskeydtd): labelpo = self.convertunit(labeldtd) accesskeypo = self.convertunit(accesskeydtd) if labelpo is None: return accesskeypo if accesskeypo is None: return labelpo thepo = po.pounit(encoding="UTF-8") thepo.addlocations(labelpo.getlocations()) thepo.addlocations(accesskeypo.getlocations()) thepo.msgidcomment = thepo._extract_msgidcomments() + labelpo._extract_msgidcomments() thepo.msgidcomment = thepo._extract_msgidcomments() + accesskeypo._extract_msgidcomments() thepo.addnote(labelpo.getnotes("developer"), "developer") thepo.addnote(accesskeypo.getnotes("developer"), "developer") thepo.addnote(labelpo.getnotes("translator"), "translator") thepo.addnote(accesskeypo.getnotes("translator"), "translator") # redo the strings from original dtd... label = dtd.unquotefromdtd(labeldtd.definition).decode('UTF-8') accesskey = dtd.unquotefromdtd(accesskeydtd.definition).decode('UTF-8') label = accesskeyfn.combine(label, accesskey) if label is None: return None thepo.source = label thepo.target = "" return thepo
def converttransunit(self, transunit): """makes a pounit from the given transunit""" thepo = po.pounit() # Header if transunit.getrestype() == "x-gettext-domain-header": thepo.source = "" else: thepo.source = transunit.source thepo.target = transunit.target # Location comments locations = transunit.getlocations() if locations: thepo.addlocations(locations) # NOTE: Supporting both <context> and <note> tags in xliff files # for comments # Translator comments trancomments = transunit.getnotes("translator") if trancomments: thepo.addnote(trancomments, origin="translator") # Automatic and Developer comments autocomments = transunit.getnotes("developer") if autocomments: thepo.addnote(autocomments, origin="developer") # See 5.6.1 of the spec. We should not check fuzzyness, but approved # attribute if transunit.isfuzzy(): thepo.markfuzzy(True) return thepo
def converttransunit(self, transunit): """makes a pounit from the given transunit""" thepo = po.pounit() #Header if transunit.getrestype() == "x-gettext-domain-header": thepo.source = "" else: thepo.source = transunit.source thepo.target = transunit.target #Location comments locations = transunit.getlocations() if locations: thepo.addlocations(locations) #NOTE: Supporting both <context> and <note> tags in xliff files for comments #Translator comments trancomments = transunit.getnotes("translator") if trancomments: thepo.addnote(trancomments, origin="translator") #Automatic and Developer comments autocomments = transunit.getnotes("developer") if autocomments: thepo.addnote(autocomments, origin="developer") #See 5.6.1 of the spec. We should not check fuzzyness, but approved attribute if transunit.isfuzzy(): thepo.markfuzzy(True) return thepo
def convertunit(self, source_str, target_str): pounit = po.pounit(encoding="UTF-8") pounit.settypecomment('python-format') pounit.source = source_str if target_str: pounit.target = target_str return pounit
def create_termunit(term, unit, targets, locations, sourcenotes, transnotes, filecounts): termunit = po.pounit(term) if unit is not None: termunit.merge(unit, overwrite=False, comments=False) if len(targets.keys()) > 1: txt = '; '.join([ "%s {%s}" % (target, ', '.join(files)) for target, files in six.iteritems(targets) ]) if termunit.target.find('};') < 0: termunit.target = txt termunit.markfuzzy() else: # if annotated multiple terms already present, keep as-is termunit.addnote(txt, "translator") for location in locations: termunit.addlocation(location) for sourcenote in sourcenotes: termunit.addnote(sourcenote, "developer") for transnote in transnotes: termunit.addnote(transnote, "translator") for filename, count in six.iteritems(filecounts): termunit.addnote("(poterminology) %s (%d)\n" % (filename, count), 'translator') return termunit
def convertunit(self, csvunit): """converts csv unit to po unit""" pounit = po.pounit(encoding="UTF-8") if csvunit.location: pounit.addlocation(csvunit.location) pounit.source = csvunit.source pounit.target = csvunit.target return pounit
def convert_l20nunit(self, unit): po_unit = po.pounit(encoding="UTF-8") po_unit.setid(unit.getid()) po_unit.addlocation(unit.getid()) po_unit.source = unit.value po_unit.addnote(unit.comment, "developer") return po_unit
def convertunit(self, inputunit, origin): """Converts a .php unit to a .po unit""" outputunit = po.pounit(encoding="UTF-8") outputunit.addnote(inputunit.getnotes(origin), origin) outputunit.addlocation("".join(inputunit.getlocations())) outputunit.source = inputunit.source outputunit.target = "" return outputunit
def xaml2po(node, store): for n in node.childNodes: if n.nodeType == Node.ELEMENT_NODE: if (n.hasAttribute('Text')): unit = po.pounit(n.getAttribute('Text')) unit.addlocation(f'{n.nodeName}:{n.getAttribute("Name")}') store.addunit(unit) xaml2po(n, store)
def convertmixedunit(self, labelprop, accesskeyprop, commenttype): label_unit = self.convertunit(labelprop, commenttype) accesskey_unit = self.convertunit(accesskeyprop, commenttype) if label_unit is None: return accesskey_unit if accesskey_unit is None: return label_unit target_unit = po.pounit(encoding="UTF-8") return self.mixer.mix_units(label_unit, accesskey_unit, target_unit)
def convertmixedunit(self, labeldtd, accesskeydtd): label_unit = self.convertunit(labeldtd) accesskey_unit = self.convertunit(accesskeydtd) if label_unit is None: return accesskey_unit if accesskey_unit is None: return label_unit target_unit = po.pounit(encoding="UTF-8") return self.mixer.mix_units(label_unit, accesskey_unit, target_unit)
def count(self, source, expectedsource, target=None, expectedtarget=None): """simple helper to check the respective word counts""" poelement = po.pounit(source) if target is not None: poelement.target = target wordssource, wordstarget = statsdb.wordsinunit(poelement) print('Source (expected=%d; actual=%d): "%s"' % (expectedsource, wordssource, source)) assert wordssource == expectedsource if target is not None: print('Target (expected=%d; actual=%d): "%s"' % (expectedtarget, wordstarget, target)) assert wordstarget == expectedtarget
def convert_unit(self, input_unit, commenttype): """Converts a .rc unit to a .po unit. Returns None if empty or not for translation.""" if input_unit is None: return None # escape unicode output_unit = po.pounit(encoding="UTF-8") output_unit.addlocation("".join(input_unit.getlocations())) output_unit.source = input_unit.source output_unit.target = "" return output_unit
def create_pounit(filename, lineno, message, comments): unit = po.pounit(encoding='UTF-8') context, msgid = split_context(message) unit.setsource(msgid) if context: unit.msgctxt = ['"%s"' % context] for comment in comments: unit.addnote(comment, 'developer') unit.addlocation('%s:%s' % (filename, lineno)) return unit
def test_escaping_newline_tabs(self): """test that we handle all kinds of newline permutations""" dtdsource = '<!ENTITY noupdatesfound.intro "A hard coded newline.\\nAnd tab\\t and a \\r carriage return.">\n' converter = dtd2po.dtd2po() thedtd = dtd.dtdunit() thedtd.parse(dtdsource) thepo = po.pounit() converter.convertstrings(thedtd, thepo) print(thedtd) print(thepo.source) # \n in a dtd should also appear as \n in the PO file assert thepo.source == r"A hard coded newline.\nAnd tab\t and a \r carriage return."
def convert_unit(self, input_unit, commenttype): """Converts a JSON unit to a PO unit :return: None if empty or not for translation """ if input_unit is None: return None # escape unicode output_unit = po.pounit(encoding="UTF-8") output_unit.addlocation(input_unit.getid()) output_unit.source = input_unit.source output_unit.target = "" return output_unit
def convert_unit(self, input_unit, commenttype): """Converts a RESX unit to a PO unit @return: None if empty or not for translation """ if input_unit is None: return None output_unit = po.pounit(encoding="UTF-8") output_unit.addlocation(input_unit.getid()) output_unit.source = input_unit.source output_unit.addnote(input_unit.getnotes("developer"), "developer") output_unit.target = "" return output_unit
def convertfile(self, tbxfile): """Converts a tbxfile to a tbxfile, and returns it. uses templatepo if given at construction """ self.pofile = po.pofile() for tbxunit in tbxfile.units: term = po.pounit() term.source = tbxunit.source term.target = tbxunit.target term.setcontext(tbxunit.getnotes('definition')) term.addnote("Part of speech: %s" % tbxunit.getnotes('pos'), 'developer') self.pofile.addunit(term) self.pofile.removeduplicates() return self.pofile
def maketargetunit(self, part1, part2, translators_comment, key, subkey): """makes a base unit (.po or XLIFF) out of a subkey of two parts""" #TODO: Do better text1 = getattr(part1, subkey) if text1 == "": return None text2 = getattr(part2, subkey) unit = po.pounit(text1, encoding="UTF-8") unit.target = text2 unit.addlocation(key + "." + subkey) if getattr(translators_comment, subkey).strip() != "": unit.addnote(getattr(translators_comment, subkey), origin="developer") return unit
def convertmessage(self, contextname, messagenum, source, target, msgcomments, transtype): """makes a pounit from the given message""" thepo = po.pounit(encoding="UTF-8") thepo.addlocation("%s#%d" % (contextname, messagenum)) thepo.source = source thepo.target = target if len(msgcomments) > 0: thepo.addnote(msgcomments) if transtype == "unfinished" and thepo.istranslated(): thepo.markfuzzy() if transtype == "obsolete": # This should use the Gettext obsolete method but it would require quite a bit of work thepo.addnote("(obsolete)", origin="developer") # using the fact that -- quote -- "(this is nonsense)" return thepo
def convertunit(self, dtd_unit): """Converts a simple (non-mixed) dtd unit into a po unit. Returns None if empty or not for translation. """ if dtd_unit is None: return None po_unit = po.pounit(encoding="UTF-8") # remove unwanted stuff for commentnum in range(len(dtd_unit.comments)): commenttype, locnote = dtd_unit.comments[commentnum] # if this is a localization note if commenttype == 'locnote': # parse the locnote into the entity and the actual note typeend = quote.findend(locnote, 'LOCALIZATION NOTE') # parse the id idstart = locnote.find('(', typeend) if idstart == -1: continue idend = locnote.find(')', (idstart + 1)) entity = locnote[idstart+1:idend].strip() # parse the actual note actualnotestart = locnote.find(':', (idend + 1)) actualnoteend = locnote.find('-->', idend) actualnote = locnote[actualnotestart+1:actualnoteend].strip() # if it's for this entity, process it if dtd_unit.getid() == entity: # if it says don't translate (and nothing more), if actualnote.startswith("DONT_TRANSLATE"): # take out the entity,definition and the # DONT_TRANSLATE comment dtd_unit.setid("") dtd_unit.source = "" del dtd_unit.comments[commentnum] # finished this for loop break else: # convert it into an automatic comment, to be # processed by convertcomments dtd_unit.comments[commentnum] = ("automaticcomment", actualnote) # do a standard translation self.convertcomments(dtd_unit, po_unit) self.convertstrings(dtd_unit, po_unit) if po_unit.isblank() and not po_unit.getlocations(): return None else: return po_unit
def convertunit(self, dtd_unit): """Converts a simple (non-mixed) dtd unit into a po unit. Returns None if empty or not for translation. """ if dtd_unit is None: return None po_unit = po.pounit(encoding="UTF-8") # remove unwanted stuff for commentnum in range(len(dtd_unit.comments)): commenttype, locnote = dtd_unit.comments[commentnum] # if this is a localization note if commenttype == 'locnote': # parse the locnote into the entity and the actual note typeend = quote.findend(locnote, 'LOCALIZATION NOTE') # parse the id idstart = locnote.find('(', typeend) if idstart == -1: continue idend = locnote.find(')', (idstart + 1)) entity = locnote[idstart + 1:idend].strip() # parse the actual note actualnotestart = locnote.find(':', (idend + 1)) actualnoteend = locnote.find('-->', idend) actualnote = locnote[actualnotestart + 1:actualnoteend].strip() # if it's for this entity, process it if dtd_unit.getid() == entity: # if it says don't translate (and nothing more), if actualnote.startswith("DONT_TRANSLATE"): # take out the entity,definition and the # DONT_TRANSLATE comment dtd_unit.setid("") dtd_unit.source = "" del dtd_unit.comments[commentnum] # finished this for loop break else: # convert it into an automatic comment, to be # processed by convertcomments dtd_unit.comments[commentnum] = ("automaticcomment", actualnote) # do a standard translation self.convertcomments(dtd_unit, po_unit) self.convertstrings(dtd_unit, po_unit) if po_unit.isblank() and not po_unit.getlocations(): return None else: return po_unit
def convertunit(self, thedtd): """converts a dtd unit to a po unit, returns None if empty or not for translation""" if thedtd is None: return None if getattr(thedtd, "entityparameter", None) == "SYSTEM": return None thepo = po.pounit(encoding="UTF-8") # remove unwanted stuff for commentnum in range(len(thedtd.comments)): commenttype, locnote = thedtd.comments[commentnum] # if this is a localization note if commenttype == 'locnote': # parse the locnote into the entity and the actual note typeend = quote.findend(locnote, 'LOCALIZATION NOTE') # parse the id idstart = locnote.find('(', typeend) if idstart == -1: continue idend = locnote.find(')', (idstart + 1)) entity = locnote[idstart+1:idend].strip() # parse the actual note actualnotestart = locnote.find(':', (idend + 1)) actualnoteend = locnote.find('-->', idend) actualnote = locnote[actualnotestart+1:actualnoteend].strip() # if it's for this entity, process it if thedtd.entity == entity: # if it says don't translate (and nothing more), if actualnote.startswith("DONT_TRANSLATE"): # take out the entity,definition and the # DONT_TRANSLATE comment thedtd.entity = "" thedtd.definition = "" del thedtd.comments[commentnum] # finished this for loop break else: # convert it into an automatic comment, to be # processed by convertcomments thedtd.comments[commentnum] = ("automaticcomment", actualnote) # do a standard translation self.convertcomments(thedtd, thepo) self.convertstrings(thedtd, thepo) if thepo.isblank() and not thepo.getlocations(): return None else: return thepo
def create_pounit(filename, lineno, msgid, comments, context): # FIXME: Test context handling unit = po.pounit(encoding="UTF-8") unit.setsource(msgid) if context: unit.msgctxt = ['"%s"' % context] for comment in comments: unit.addnote(comment, "developer") unit.addlocation("%s:%s" % (filename, lineno)) # FIXME: Add variable format flags # if python-format: # unit.settypecomment('python-format', present=True) # if python-brace-format: # unit.settypecomment('python-brace-format', present=True) return unit
def buildunits(self, candidates): """Builds a list of units conforming to base API, with the score in the comment""" units = [] for score, candidate in candidates: if hasattr(candidate, "orig_source"): candidate.source = candidate.orig_source candidate.target = candidate.orig_target newunit = po.pounit(candidate.source) newunit.target = candidate.target newunit.markfuzzy(candidate.fuzzy) candidatenotes = candidate.getnotes().strip() if candidatenotes: newunit.addnote(candidatenotes) if self.addpercentage: newunit.addnote("%d%%" % score) units.append(newunit) return units
def convertunit(self, thedtd): """converts a dtd unit to a po unit, returns None if empty or not for translation""" if thedtd is None: return None if getattr(thedtd, "entityparameter", None) == "SYSTEM": return None thepo = po.pounit(encoding="UTF-8") # remove unwanted stuff for commentnum in range(len(thedtd.comments)): commenttype, locnote = thedtd.comments[commentnum] # if this is a localization note if commenttype == 'locnote': # parse the locnote into the entity and the actual note typeend = quote.findend(locnote, 'LOCALIZATION NOTE') # parse the id idstart = locnote.find('(', typeend) if idstart == -1: continue idend = locnote.find(')', idstart + 1) entity = locnote[idstart + 1:idend].strip() # parse the actual note actualnotestart = locnote.find(':', idend + 1) actualnoteend = locnote.find('-->', idend) actualnote = locnote[actualnotestart + 1:actualnoteend].strip() # if it's for this entity, process it if thedtd.entity == entity: # if it says don't translate (and nothing more), if actualnote.startswith("DONT_TRANSLATE"): # take out the entity,definition and the DONT_TRANSLATE comment thedtd.entity = "" thedtd.definition = "" del thedtd.comments[commentnum] # finished this for loop break else: # convert it into an automatic comment, to be processed by convertcomments thedtd.comments[commentnum] = ("automaticcomment", actualnote) # do a standard translation self.convertcomments(thedtd, thepo) self.convertstrings(thedtd, thepo) if thepo.isblank() and not thepo.getlocations(): return None else: return thepo
def _update_store(plugin, pootle_path): from django.contrib.auth import get_user_model from pootle_store.models import Store, Revision store = Store.objects.get(pootle_path=pootle_path) revision = Revision.incr() uid = uuid.uuid4().hex unitid = "New unit %s" % uid unit = pounit(unitid) unit.target = "Bar %s" % uid user = plugin.pootle_user if user is None: User = get_user_model() user = User.objects.get_system_user() unit.store = store unit = store.addunit(unit, user=user, update_revision=revision) store.save()
def convertstore(self, thetikifile): """Converts a given (parsed) tiki file to a po file. :param thetikifile: a tikifile pre-loaded with input data """ thetargetfile = po.pofile() # For each lang unit, make the new po unit accordingly for unit in thetikifile.units: if not self.includeunused and "unused" in unit.getlocations(): continue newunit = po.pounit() newunit.source = unit.source newunit.target = unit.target locations = unit.getlocations() if locations: newunit.addlocations(locations) thetargetfile.addunit(newunit) return thetargetfile
def convertunit(self, propunit, commenttype): """Converts a .properties unit to a .po unit. Returns None if empty or not for translation.""" if propunit is None: return None # escape unicode pounit = po.pounit(encoding="UTF-8") if hasattr(propunit, "comments"): for comment in propunit.comments: if "DONT_TRANSLATE" in comment: return "discard" pounit.addnote(u"".join(propunit.getnotes()).rstrip(), commenttype) # TODO: handle multiline msgid if propunit.isblank(): return None pounit.addlocation(propunit.name) pounit.source = propunit.source pounit.target = u"" return pounit
def create_pounit(filename, lineno, message, comments): unit = po.pounit(encoding="UTF-8") if isinstance(message, tuple): _, s = split_context(message[0]) c, p = split_context(message[1]) unit.setsource([s, p]) # Workaround for http://bugs.locamotion.org/show_bug.cgi?id=1385 unit.target = [u"", u""] else: c, m = split_context(message) unit.setsource(m) if c: unit.msgctxt = ['"%s"' % c] if comments: for comment in comments: unit.addnote(comment, "developer") unit.addlocation("%s:%s" % (filename, lineno)) return unit
def create_termunit(term, unit, targets, locations, sourcenotes, transnotes, filecounts): termunit = po.pounit(term) if unit is not None: termunit.merge(unit, overwrite=False, comments=False) if len(targets.keys()) > 1: txt = "; ".join(["%s {%s}" % (target, ", ".join(files)) for target, files in targets.iteritems()]) if termunit.target.find("};") < 0: termunit.target = txt termunit.markfuzzy() else: # if annotated multiple terms already present, keep as-is termunit.addnote(txt, "translator") for location in locations: termunit.addlocation(location) for sourcenote in sourcenotes: termunit.addnote(sourcenote, "developer") for transnote in transnotes: termunit.addnote(transnote, "translator") for filename, count in filecounts.iteritems(): termunit.addnote("(poterminology) %s (%d)\n" % (filename, count), "translator") return termunit
def xlf2po(inputfile, outputfile, pot): tree = etree.parse(inputfile) nodes = tree.xpath("//xlf:trans-unit", namespaces=namespaces) po = pofile() for tu in nodes: source = tu.xpath('xlf:source', namespaces=namespaces)[0] unit = pounit(source.text) unit.msgctxt.append('"{0}"'.format(tu.get('id'))) if pot: unit.markfuzzy() else: target = tu.xpath('xlf:target', namespaces=namespaces) if len(target) == 1: target = target[0] unit.settarget(target.text) po.units.append(unit) po.removeduplicates() po.savefile(outputfile)
def convertunit(self, source_str, target_str): pounit = po.pounit(encoding="UTF-8") pounit.source = source_str if target_str: pounit.target = target_str return pounit
def convertunit(self, source_str, target_str): pounit = po.pounit(encoding="UTF-8") pounit.setsource(source_str) if target_str: pounit.settarget(target_str) return pounit
def translate(name, msg): u = po.pounit(encoding="UTF-8") u.addlocation(name) u.source = msg u.target = "" pofile.addunit(u)