Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* :name = Merge 2 TMX with different source :description=
- * Purpose: To merge two TMX files in the /tm folder, taking the source text of the first one and the target text of the second one, and combining them in one third TMX file.
- * #Files: Expectes first file to be called Translator1.tmx and the second one to be called Translator2.tmx
- Renames Translator2.tmx as Translator2.tmx.fra-bak and outputs new Translator2.tmx
- * #Format: TMX v.1.4
- *
- * @author Manuel Souto Pico, Kos Ivantsov
- * @date 2019-04-12
- * @version 0.2
- */
- import groovy.swing.SwingBuilder
- import groovy.util.XmlSlurper
- import groovy.util.GroovyCollections
- // import groovy.xml.XmlUtil
- import java.awt.FlowLayout
- import java.nio.file.Files
- import java.nio.file.Paths
- import java.util.zip.ZipFile
- import javax.swing.JOptionPane
- import javax.swing.WindowConstants as WC
- import org.apache.commons.io.FileUtils
- import org.omegat.util.Preferences
- import org.omegat.util.StaticUtils
- import org.omegat.util.StringUtil
- import static javax.swing.JOptionPane.*
- import static org.omegat.util.Platform.*
- utils = (StringUtil.getMethods().toString().findAll("makeValidXML")) ? StringUtil : StaticUtils
- def prop = project.projectProperties;
- def targetLang = prop.getTargetLanguage();
- def tmDir = prop.getTMRoot();
- // console.println(targetLang);
- tmxOne = new File(tmDir.toString() + File.separator + "Translator1.tmx");
- tmxTwo = new File(tmDir.toString() + File.separator + "Translator2.tmx");
- console.println('Merging source text from\n' + tmxOne + '\nwith target text from\n' + tmxTwo + '...\n')
- parser=new XmlSlurper()
- parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false)
- parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
- if (tmxOne.exists()) {
- // def t1EngTmx = new XmlSlurper().parse(tmxOne)
- tmxOneEntries = parser.parse(tmxOne)
- }
- if (tmxTwo.exists()) {
- // def t1EngTmx = new XmlSlurper().parse(tmxOne)
- tmxTwoEntries = parser.parse(tmxTwo)
- }
- //console.println(tmxOneEntries) // outputs long string
- // console.println tmxOneEntries.getClass()
- finalTMX_contents = new StringWriter()
- preamble = '''<?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE tmx SYSTEM "tmx11.dtd">
- <tmx version="1.1">
- <header creationtool="OmegaT" o-tmf="OmegaT TMX" adminlang="EN-US" datatype="plaintext" creationtoolversion="4.1.5_3_10485" segtype="sentence" srclang="en-ZZ"/>
- <body>'''
- end = ''' </body>
- </tmx>'''
- def count = 0
- while (count < tmxOneEntries.body.tu.size()) {
- //console.println(tmxOneEntries.body.tu[count].tuv[0].seg)
- //console.println(tmxTwoEntries.body.tu[count].tuv[1].seg)
- // Converting node to string, which also escapes <, > and & (but not ' and ")
- def src_node = tmxOneEntries.body.tu[count].tuv[0].seg
- //console.println src.getClass() // gives class groovy.util.slurpersupport.NodeChildren
- def src_str = new groovy.xml.StreamingMarkupBuilder().bindNode(src_node) as String
- // console.println src_str.getClass() // gives class java.lang.String
- // console.println(src_str)
- def tgt_node = tmxOneEntries.body.tu[count].tuv[0].seg
- def tgt_str = new groovy.xml.StreamingMarkupBuilder().bindNode(tgt_node) as String
- // def tgt = tmxTwoEntries.body.tu[count].tuv[1].seg
- finalTMX_contents << """ <tu>
- <tuv lang="en-ZZ">
- ${src_str}
- </tuv>
- <tuv lang="${targetLang}">
- ${tgt_str}
- </tuv>
- </tu>
- """
- count++
- }
- //console.println(finalTMX_contents)
- String origPath = tmxTwo
- tmxTwo.renameTo origPath.concat(".fra-bak")
- finalTMX = new File(tmDir + File.separator + "Translator2.tmx")
- finalTMX.write(preamble + finalTMX_contents + end, "UTF-8")
- console.println("Translator2.tmx has been re-built now with English as the source language.")
- //return console.println(tmxOneEntries.body.tu[0].tuv[0].seg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement