--- /home/umedoblock/git/cpython/Tools/i18n/msgfmt.py 2017-08-30 23:15:31.516718074 +0900 +++ /home/umedoblock/KiCad/kicad-doc/src/translate-set/msgfmt.py 2018-04-03 13:37:03.522923756 +0900 @@ -15,6 +15,18 @@ Specify the output file to write to. If omitted, output will go to a file named filename.mo (based off the input file name). + -d + --dump + Show all msg_id_NO, msgid and msgstr in filename.po. + + -p + --progress + Show translate progress. + + -r + --remaining + Show no translated msgid with msg_id_NO. + -h --help Print this message and exit. @@ -35,6 +47,9 @@ __version__ = "1.1" MESSAGES = {} +MSG_ID_NO = 0 # count up msg_id. +COUNT_FUZZY = 0 # count up fuzzy translate +COUNT_NO_TRANSLATED = 0 @@ -46,12 +61,31 @@ -def add(id, str, fuzzy): +def add(id, str, fuzzy, dump=False, remaining=False): "Add a non-fuzzy translation to the dictionary." - global MESSAGES + global MESSAGES, MSG_ID_NO, COUNT_FUZZY, COUNT_NO_TRANSLATED if not fuzzy and str: MESSAGES[id] = str + if not fuzzy and not str: + COUNT_NO_TRANSLATED += 1 + if fuzzy: + COUNT_FUZZY += 1 + if dump and MSG_ID_NO: + # The normal pot-file header is ignored, so doesn't print msgid if MSG_ID_NO == 0. + # + print("msg_id_NO={}".format(MSG_ID_NO)) + print(id.decode()) + print(str.decode()) + print() + elif not MESSAGES.get(id) and remaining: + # print just no translated msgid + print("msg_id_NO={}".format(MSG_ID_NO)) + print(id.decode()) + print() + + MSG_ID_NO += 1 + def generate(): @@ -96,16 +130,19 @@ -def make(filename, outfile): +def make(filename, outfile, dump=False, progress=False, remaining=False): ID = 1 STR = 2 + global MSG_ID_NO + MSG_ID_NO = 0 # clear MSG_ID_NO. It doesn't relate ID and STR. + # Compute .mo name from .po name and arguments if filename.endswith('.po'): infile = filename else: infile = filename + '.po' - if outfile is None: + if outfile is None and not any([dump, progress, remaining]): outfile = os.path.splitext(infile)[0] + '.mo' try: @@ -128,7 +165,7 @@ lno += 1 # If we get a comment line after a msgstr, this is a new entry if l[0] == '#' and section == STR: - add(msgid, msgstr, fuzzy) + add(msgid, msgstr, fuzzy, dump, remaining) section = None fuzzy = 0 # Record a fuzzy mark @@ -140,7 +177,7 @@ # Now we are in a msgid section, output previous section if l.startswith('msgid') and not l.startswith('msgid_plural'): if section == STR: - add(msgid, msgstr, fuzzy) + add(msgid, msgstr, fuzzy, dump, remaining) if not msgid: # See whether there is an encoding declaration p = HeaderParser() @@ -193,11 +230,27 @@ sys.exit(1) # Add last entry if section == STR: - add(msgid, msgstr, fuzzy) + add(msgid, msgstr, fuzzy, dump, remaining) # Compute output output = generate() + # Show translate progress + if progress: + MSG_ID_NO -= 1 + remained = COUNT_FUZZY + COUNT_NO_TRANSLATED + translated = MSG_ID_NO - remained + translated_rate = int(translated / MSG_ID_NO * 100) + + print("translated: {} of {} ({}%), Remaining: {}".format(translated, MSG_ID_NO, translated_rate, remained)) + # print("MSG_ID_NO =", MSG_ID_NO) + # print("COUNT_FUZZY =", COUNT_FUZZY) + # print("COUNT_NO_TRANSLATED =", COUNT_NO_TRANSLATED) + # print("remained =", remained) + + if not outfile: + return + try: open(outfile,"wb").write(output) except IOError as msg: @@ -207,11 +260,15 @@ def main(): try: - opts, args = getopt.getopt(sys.argv[1:], 'hVo:', - ['help', 'version', 'output-file=']) + opts, args = getopt.getopt(sys.argv[1:], 'hVodpr:', + ['help', 'version', 'output-file=', \ + 'dump', 'progress', 'remaining']) except getopt.error as msg: usage(1, msg) + dump = False + progress = False + remaining = False outfile = None # parse options for opt, arg in opts: @@ -222,6 +279,12 @@ sys.exit(0) elif opt in ('-o', '--output-file'): outfile = arg + elif opt in ('-d', '--dump'): + dump = True + elif opt in ('-p', '--progress'): + progress = True + elif opt in ('-r', '--remaining'): + remaining = True # do it if not args: print('No input file given', file=sys.stderr) @@ -229,7 +292,7 @@ return for filename in args: - make(filename, outfile) + make(filename, outfile, dump, progress, remaining) if __name__ == '__main__':