Advertisement
KosIvantsov

write_new_trans2TMX.groovy

Aug 10th, 2013
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 7.32 KB | None | 0 0
  1. /*
  2.  * Purpose:  Export new translations completed after the specified
  3.  *   date (line 21) either for the entire project or for the
  4.  *   selected files ("select_files" must be set to 'yes' — line 27)
  5.  *   to TMX file
  6.  * #Files:   Writes 'translated_after_<date_time>.tmx'
  7.  *   in the current project's root
  8.  * #File format:     TMX v.1.4
  9.  * #Details:    http://wp.me/p3fHEs-6z
  10.  *
  11.  * @author  Kos Ivantsov
  12.  * @date    2013-08-12
  13.  * @version 0.3
  14.  */
  15.  
  16. /*
  17.  * The date should be specified as "year-month-day HOURS:minutes"
  18.  * If not specified or specified wrongly, the script will look for
  19.  * translations that are newer than one day.
  20.  */
  21. def newdate = ''
  22. /*
  23.  * Set "select_files" to 'yes' if you want to use file selector
  24.  * to specify files for export. If anything else is specified, the script
  25.  * will work with the complete project.
  26.  */
  27. select_files = ''
  28.  
  29. import javax.swing.JFileChooser
  30. import org.omegat.util.StaticUtils
  31. import org.omegat.util.TMXReader
  32. import static javax.swing.JOptionPane.*
  33. import static org.omegat.util.Platform.*
  34. def prop = project.projectProperties
  35.  
  36. if (!prop) {
  37.     final def title = 'Export new translation'
  38.     final def msg   = 'Please try again after you open a project.'
  39.     showMessageDialog null, msg, title, INFORMATION_MESSAGE
  40.     return
  41. }
  42.  
  43. try {
  44.     newdate = new Date().parse("yyyy-MM-dd HH:mm", newdate)
  45.     }
  46.     catch (java.text.ParseException e) {
  47.         newdate = new Date().minus(1)
  48.         final def title = 'Wrong date format'
  49.         final def msg   = """\
  50. The date has been specified in a wrong format.
  51. The script will work with entries exactly one day old,
  52. i.e. changed after $newdate\
  53. """
  54.         console.println msg
  55.         showMessageDialog null, msg, title, INFORMATION_MESSAGE
  56.         }
  57.  
  58. namedate = new Date().parse("E MMM dd H:m:s z yyyy", newdate.toString()).format("MMM-dd-yyyy_HH.mm")
  59.  
  60. def fileloc = prop.projectRoot+'translated_after_'+namedate+"${ (select_files == 'yes') ? "_select" : ''}"+'.tmx'
  61. exportfile = new File(fileloc)
  62.  
  63. if (prop.isSentenceSegmentingEnabled())
  64.     segmenting = TMXReader.SEG_SENTENCE
  65.     else
  66.     segmenting = TMXReader.SEG_PARAGRAPH
  67.  
  68. def sourceLocale = prop.getSourceLanguage().toString()
  69. def targetLocale = prop.getTargetLanguage().toString()
  70.  
  71. exportfile.write("", 'UTF-8')
  72. exportfile.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", 'UTF-8')
  73. exportfile.append("<!DOCTYPE tmx SYSTEM \"tmx11.dtd\">\n", 'UTF-8')
  74. exportfile.append("<tmx version=\"1.4\">\n", 'UTF-8')
  75. exportfile.append(" <header\n", 'UTF-8')
  76. exportfile.append("  creationtool=\"OmegaTScripting\"\n", 'UTF-8')
  77. exportfile.append("  segtype=\"" + segmenting + "\"\n", 'UTF-8')
  78. exportfile.append("  o-tmf=\"OmegaT TMX\"\n", 'UTF-8')
  79. exportfile.append("  adminlang=\"EN-US\"\n", 'UTF-8')
  80. exportfile.append("  srclang=\"" + sourceLocale + "\"\n", 'UTF-8')
  81. exportfile.append("  datatype=\"plaintext\"\n", 'UTF-8')
  82. exportfile.append(" >\n", 'UTF-8')
  83.  
  84. def hitcount = 0
  85.  
  86. if ((select_files == 'yes')) {
  87.     srcroot = new File(prop.getSourceRoot())
  88.     sourceroot = prop.getSourceRoot().toString() as String
  89.     JFileChooser fc = new JFileChooser(
  90.     currentDirectory: srcroot,
  91.     dialogTitle: "Choose files to export",
  92.     fileSelectionMode: JFileChooser.FILES_ONLY,
  93.     //the file filter must show also directories, in order to be able to look into them
  94.     multiSelectionEnabled: true)
  95.  
  96.     if(fc.showOpenDialog() != JFileChooser.APPROVE_OPTION) {
  97.     console.println "Canceled"
  98.     return
  99.     }
  100.  
  101.     if (!(fc.selectedFiles =~ sourceroot.replaceAll(/\\+/, '\\\\\\\\'))) {
  102.         console.println "Selection outside of ${prop.getSourceRoot()} folder"
  103.         final def title = 'Wrong file(s) selected'
  104.         final def msg   = "Files must be in ${prop.getSourceRoot()} folder."
  105.         showMessageDialog null, msg, title, INFORMATION_MESSAGE
  106.         return
  107.     }
  108.  
  109.     fc.selectedFiles.each {
  110.         fl = "${it.toString()}" - "$sourceroot"
  111.         exportfile.append("  <prop type=\"Filename\">" + fl + "</prop>\n", 'UTF-8')
  112.     }
  113.     exportfile.append(" </header>\n", 'UTF-8')
  114.     exportfile.append("  <body>\n", 'UTF-8')
  115.  
  116.     fc.selectedFiles.each{
  117.         fl = "${it.toString()}" - "$sourceroot"
  118.         files = project.projectFiles
  119.         files.each{
  120.             if ( "${it.filePath}" != "$fl" ) {
  121.             println "Skipping to the next file"
  122.             }else{
  123.         it.entries.each {
  124.         def info = project.getTranslationInfo(it)
  125.         def changeId = info.changer
  126.         def changeDate = info.changeDate
  127.         def creationId = info.creator
  128.         def creationDate = info.creationDate
  129.         def alt = 'unknown'
  130.         if (info.isTranslated()) {
  131.             if (newdate.before(new Date(changeDate))){
  132.                 hitcount++
  133.                 source = StaticUtils.makeValidXML(it.srcText)
  134.                 target = StaticUtils.makeValidXML(info.translation)
  135.                 exportfile.append("    <tu>\n", 'UTF-8')
  136.                 exportfile.append("      <tuv xml:lang=\"" + sourceLocale + "\">\n", 'UTF-8')
  137.                 exportfile.append("        <seg>" + "$source" + "</seg>\n", 'UTF-8')
  138.                 exportfile.append("      </tuv>\n", 'UTF-8')
  139.                 exportfile.append("      <tuv xml:lang=\"" + targetLocale + "\"", 'UTF-8')
  140.                 exportfile.append(" changeid=\"${changeId ?: alt }\"", 'UTF-8')
  141.                 exportfile.append(" changedate=\"${ changeDate > 0 ? new Date(changeDate).format("yyyyMMdd'T'HHmmss'Z'") : alt }\"", 'UTF-8')
  142.                 exportfile.append(" creationid=\"${creationId ?: alt }\"", 'UTF-8')
  143.                 exportfile.append(" creationdate=\"${ creationDate > 0 ? new Date(creationDate).format("yyyyMMdd'T'HHmmss'Z'") : alt }\"", 'UTF-8')
  144.                 exportfile.append(">\n", 'UTF-8')
  145.                 exportfile.append("        <seg>" + "$target" + "</seg>\n", 'UTF-8')
  146.                 exportfile.append("      </tuv>\n", 'UTF-8')
  147.                 exportfile.append("    </tu>\n", 'UTF-8')
  148.                         }
  149.                     }
  150.                 }
  151.             }
  152.         }
  153.     }
  154. } else {
  155.     exportfile.append(" </header>\n", 'UTF-8')
  156.     exportfile.append("  <body>\n", 'UTF-8')
  157.     files = project.projectFiles
  158.         files.each {
  159.             it.entries.each {
  160.             def info = project.getTranslationInfo(it)
  161.             def changeId = info.changer
  162.             def changeDate = info.changeDate
  163.             def creationId = info.creator
  164.             def creationDate = info.creationDate
  165.             def alt = 'unknown'
  166.             if (info.isTranslated()) {
  167.                 if (newdate.before(new Date(changeDate))){
  168.                 hitcount++
  169.                 source = StaticUtils.makeValidXML(it.srcText)
  170.                 target = StaticUtils.makeValidXML(info.translation)
  171.                 exportfile.append("    <tu>\n", 'UTF-8')
  172.                 exportfile.append("      <tuv xml:lang=\"" + sourceLocale + "\">\n", 'UTF-8')
  173.                 exportfile.append("        <seg>" + "$source" + "</seg>\n", 'UTF-8')
  174.                 exportfile.append("      </tuv>\n", 'UTF-8')
  175.                 exportfile.append("      <tuv xml:lang=\"" + targetLocale + "\"", 'UTF-8')
  176.                 exportfile.append(" changeid=\"${changeId ?: alt }\"", 'UTF-8')
  177.                 exportfile.append(" changedate=\"${ changeDate > 0 ? new Date(changeDate).format("yyyyMMdd'T'HHmmss'Z'") : alt }\"", 'UTF-8')
  178.                 exportfile.append(" creationid=\"${creationId ?: alt }\"", 'UTF-8')
  179.                 exportfile.append(" creationdate=\"${ creationDate > 0 ? new Date(creationDate).format("yyyyMMdd'T'HHmmss'Z'") : alt }\"", 'UTF-8')
  180.                 exportfile.append(">\n", 'UTF-8')
  181.                 exportfile.append("        <seg>" + "$target" + "</seg>\n", 'UTF-8')
  182.                 exportfile.append("      </tuv>\n", 'UTF-8')
  183.                 exportfile.append("    </tu>\n", 'UTF-8')
  184.                     }
  185.                 }
  186.             }
  187.         }
  188. }
  189.  
  190. exportfile.append("  </body>\n", 'UTF-8')
  191. exportfile.append("</tmx>", 'UTF-8')
  192.  
  193. final def title = 'TMX file written'
  194. final def msg   = "$hitcount TU's written to " + exportfile.toString()
  195. console.println msg
  196. showMessageDialog null, msg, title, INFORMATION_MESSAGE
  197. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement