def main(): args = parser.parse_args() if not args.docx: args.docx = path.splitext(args.input)[0] + ".docx" if not args.output: args.output = args.input print("Opening ts file: " + args.input) orig_ts = tsfile.tsfile() orig_ts.load_file(args.input) print("Opening docx file: " + args.docx) doc = Document(args.docx) new_table = doc.tables[0] rows = [row.cells for row in new_table.rows] new_data = {} print("Reading translation from docx") for row in rows: context = get_text_from_cell(row[0] or "") orig = get_text_from_cell(row[1] or "") transl = get_text_from_cell(row[2] or "") new_data[(my_strip(context), my_strip(orig))] = transl print("Found " + str(len(new_data)) + " translations") print("Updating ts data") for context in orig_ts.context_list: for message in context.message_list: try: message.translation.text = new_data[(my_strip(context.name),my_strip(message.source))] message.translation.finished = message.translation.text != "" except KeyError: pass print("Saving new tsdata in: " + args.output) orig_ts.save_file(args.output)
def main(): args = parser.parse_args() if not args.output: args.output = path.splitext(args.input)[0] + ".docx" print("Opening ts file: " + args.input) f = tsfile.tsfile() try: f.load_file(args.input) except Exception as err: print("Unable to open " + args.input) exit(-1) print("Preparing translations") num_translations = 0 num_conflicts = 0 translations = {} keys = [] for context in f.context_list: for message in context.message_list: if args.light and message.translation.status in ["finished", "obsolete"]: continue result = "" if message.translation: result = message.translation.text or "" key = my_strip(context.name) + my_strip(message.source) translations[key] = result if not message.source in keys: keys.append((context.name, message.source)) message.translation.text = "" print("Identified " + str(len(keys)) + " translations") print("Generating docx") doc = Document("template.docx") table = doc.tables[0] for context, source in keys: row = table.add_row().cells append_text_to_cell(row[0], context or "") append_text_to_cell(row[1], source or "") key = my_strip(context) + my_strip(source) append_text_to_cell(row[2], translations[key] or "") print("Saving output: " + args.output) doc.save(args.output)