def generateSinglePoT(folder, root): xmlfiles = [] abspath = os.path.join(root, folder) if os.path.isdir(abspath): os.path.walk(abspath, get_xml_list, xmlfiles) else: return if len(xmlfiles) > 0: output = os.path.join(abspath, "locale") if not os.path.exists(output): os.mkdir(output) output = os.path.join(output, folder + ".pot") try: xml2po_main = Main(default_mode, "pot", output, xml_options) xml2po_main.current_mode = myDocbookXmlMode() except IOError: print("Error: cannot open aFile %s for writing." % (output)) sys.exit(5) # print(xmlfiles) # print(">>>outout: %s ", output) xml2po_main.to_pot(xmlfiles)
def mergeSingleDocument(folder, language, root): xmlfiles = [] outputfiles = [] abspath = os.path.join(root, folder) if os.path.isdir(abspath): os.path.walk(abspath, get_xml_list, xmlfiles) else: return None if len(xmlfiles) > 0: popath = os.path.join(abspath, "locale", language + ".po") # generate MO file mofile_handler, mofile_tmppath = tempfile.mkstemp() os.close(mofile_handler) os.system("msgfmt -o %s %s" % (mofile_tmppath, popath)) for aXML in xmlfiles: # (filename, ext) = os.path.splitext(os.path.basename(aXML)) relpath = os.path.relpath(aXML, root) outputpath = os.path.join(os.path.curdir, "generated", language, relpath) try: xml2po_main = Main(default_mode, "merge", outputpath, xml_options) xml2po_main.current_mode = myDocbookXmlMode() xml2po_main.merge(mofile_tmppath, aXML) outputfiles.append(outputpath) except IOError: print("Error: cannot open aFile %s for writing.") sys.exit(5) except Exception: print("Exception happen") if mofile_tmppath: os.remove(mofile_tmppath) return outputfiles
def main(argv): if not argv: usage() sys.exit(2) name = os.path.join(os.path.dirname(__file__), '..') if not name in sys.path: sys.path.insert(0, name) from xml2po import Main # Default parameters default_mode = 'docbook' operation = 'pot' # 'pot', 'merge', 'update' output = '-' # this means to stdout options = { 'mark_untranslated': False, 'expand_entities': True, 'expand_all_entities': False, } origxml = '' mofile = None mofile_tmppath = None try: opts, remaining_args = getopt.getopt(argv, 'avhkem:t:o:p:u:r:l:', [ 'automatic-tags', 'version', 'help', 'keep-entities', 'expand-all-entities', 'mode=', 'translation=', 'output=', 'po-file=', 'update-translation=', 'reuse=', 'language=', 'mark-untranslated' ]) except getopt.GetoptError: usage(True) sys.exit(2) for opt, arg in opts: if opt in ('-m', '--mode'): default_mode = arg if opt in ('-a', '--automatic-tags'): default_mode = 'basic' elif opt in ('-k', '--keep-entities'): options['expand_entities'] = False elif opt in ('--mark-untranslated', ): options['mark_untranslated'] = True elif opt in ('-e', '--expand-all-entities'): options['expand_all_entities'] = True elif opt in ('-l', '--language'): options['translationlanguage'] = arg elif opt in ('-t', '--translation'): mofile = arg operation = 'merge' if 'translationlanguage' not in options: options['translationlanguage'] = os.path.split( os.path.splitext(mofile)[0])[1] elif opt in ('-r', '--reuse'): origxml = arg elif opt in ('-u', '--update-translation'): operation = 'update' po_to_update = arg elif opt in ('-p', '--po-file'): mofile_handle, mofile_tmppath = tempfile.mkstemp() os.close(mofile_handle) pofile = arg operation = 'merge' if 'translationlanguage' not in options: options['translationlanguage'] = os.path.split( os.path.splitext(pofile)[0])[1] os.system("msgfmt -o %s %s >%s" % (mofile_tmppath, pofile, NULL_STRING)) and sys.exit(7) mofile = mofile_tmppath elif opt in ('-o', '--output'): output = arg elif opt in ('-v', '--version'): print VERSION sys.exit(0) elif opt in ('-h', '--help'): usage(True) sys.exit(0) if operation == 'update' and output != "-": print >> sys.stderr, "Option '-o' is not yet supported when updating translations directly. Ignoring this option." # Treat remaining arguments as XML files filenames = [] while remaining_args: filenames.append(remaining_args.pop()) try: xml2po_main = Main(default_mode, operation, output, options) except IOError: print >> sys.stderr, "Error: cannot open file %s for writing." % ( output) sys.exit(5) if operation == 'merge': if len(filenames) > 1: print >> sys.stderr, "Error: You can merge translations with only one XML file at a time." sys.exit(2) if not mofile: print >> sys.stderr, "Error: You must specify MO file when merging translations." sys.exit(3) xml2po_main.merge(mofile, filenames[0]) elif operation == 'update': xml2po_main.update(filenames, po_to_update) elif origxml: xml2po_main.reuse(origxml, filenames[0]) else: # Standard POT producing xml2po_main.to_pot(filenames) if mofile_tmppath: os.remove(mofile_tmppath)