def convertfile(storefile): store = xlifffile() store.setfilename(store.getfilenode("NoName"), "odf") contents = open_odf(storefile) for data in contents.values(): parse_state = ParseState(no_translate_content_elements, inline_elements) build_store(BytesIO(data), store, parse_state) return store
def translate_dom_trees(unit_trees, dom_trees): """Return a dict with the translated files for the IDML package. The keys are the filenames for the translatable files inside the template IDML package, and the values are etree ElementTree instances for each of those files. """ def get_po_doms(unit): """Return a tuple with unit source and target DOM objects. This method is method is meant to provide a way to retrieve the DOM objects for the unit source and target for PO stores. Since POunit doesn't have any source_dom nor target_dom attributes, it is necessary to craft those objects. """ def add_node_content(string, node): """Append the translatable content to the node. The string is going to have XLIFF placeables, so we have to parse it as XML in order to get the right nodes to append to the node. """ # Add a wrapper "whatever" tag to avoid problems when parsing # several sibling tags at the root level. fake_string = "<whatever>" + string + "</whatever>" # Copy the children to the XLIFF unit's source or target node. fake_node = etree.fromstring(fake_string) node.extend(fake_node.getchildren()) return node source_dom = etree.Element("source") source_dom = add_node_content(unit.source, source_dom) target_dom = etree.Element("target") if unit.target: target_dom = add_node_content(unit.target, target_dom) else: target_dom = add_node_content(unit.source, target_dom) return (source_dom, target_dom) make_parse_state = lambda: ParseState(NO_TRANSLATE_ELEMENTS, INLINE_ELEMENTS) for filename, dom_tree in dom_trees.items(): file_unit_tree = unit_trees[filename] apply_translations( dom_tree.getroot(), file_unit_tree, replace_dom_text( make_parse_state, dom_retriever=get_po_doms, process_translatable=process_idml_translatable, ), ) return dom_trees
def translate_dom_trees(unit_trees, dom_trees): """Return a dict with the translated files for the ODF package. The keys are the filenames for the translatable files inside the template ODF package, and the values are etree ElementTree instances for each of those files. """ make_parse_state = lambda: ParseState(no_translate_content_elements, inline_elements) for filename, dom_tree in six.iteritems(dom_trees): file_unit_tree = unit_trees[filename] apply_translations(dom_tree.getroot(), file_unit_tree, replace_dom_text(make_parse_state)) return dom_trees
def convertodf(inputfile, outputfile, templates): """Convert an ODF package to XLIFF.""" store = factory.getobject(outputfile) try: store.setfilename(store.getfilenode('NoName'), inputfile.name) except Exception: print("couldn't set origin filename") contents = open_odf(inputfile) for data in contents.values(): parse_state = ParseState(no_translate_content_elements, inline_elements) build_store(BytesIO(data), store, parse_state) store.save() return True
def convert_idml(inputfile, outputfile, template): """Convert an IDML package to PO.""" store = factory.getobject(outputfile) contents = open_idml(inputfile) id_maker = IdMaker() # Create it here to avoid having repeated ids. for filename, translatable_file in six.iteritems(contents): parse_state = ParseState(NO_TRANSLATE_ELEMENTS, INLINE_ELEMENTS) po_store_adder = make_postore_adder(store, id_maker, filename) build_idml_store(BytesIO(translatable_file), store, parse_state, store_adder=po_store_adder) store.save() return True
def convertfile(storefile): store = pofile() contents = open_idml(storefile) # Create it here to avoid having repeated ids. id_maker = IdMaker() for filename, translatable_file in contents.items(): parse_state = ParseState(NO_TRANSLATE_ELEMENTS, INLINE_ELEMENTS) po_store_adder = make_postore_adder(store, id_maker, filename) build_idml_store( BytesIO(translatable_file), store, parse_state, store_adder=po_store_adder, ) return store
def convertodf(inputfile, outputfile, templates): """Convert an ODF package to XLIFF.""" # Since the convertoptionsparser will give us an open file, we risk that # it could have been opened in non-binary mode on Windows, and then we'll # have problems, so let's make sure we have what we want. inputfile.close() inputfile = file(inputfile.name, mode='rb') store = factory.getobject(outputfile) try: store.setfilename(store.getfilenode('NoName'), inputfile.name) except: print("couldn't set origin filename") contents = open_odf(inputfile) for data in contents.values(): parse_state = ParseState(no_translate_content_elements, inline_elements) build_store(BytesIO(data), store, parse_state) store.save() return True
def convert_idml(inputfile, outputfile, template): """Convert an IDML package to PO.""" # Since the convertoptionsparser will give us an open file, we risk that # it could have been opened in non-binary mode on Windows, and then we'll # have problems, so let's make sure we have what we want. inputfile.close() inputfile = file(inputfile.name, mode='rb') store = factory.getobject(outputfile) contents = open_idml(inputfile) id_maker = IdMaker() # Create it here to avoid having repeated ids. for filename, translatable_file in six.iteritems(contents): parse_state = ParseState(NO_TRANSLATE_ELEMENTS, INLINE_ELEMENTS) po_store_adder = make_postore_adder(store, id_maker, filename) build_idml_store(BytesIO(translatable_file), store, parse_state, store_adder=po_store_adder) store.save() return True