Advertisement
KosIvantsov

write_sel_files2TMX.groovy

Aug 9th, 2013
952
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 4.57 KB | None | 0 0
  1. /*
  2.  * Purpose: Export source and translation segments of user selected
  3.  *  files into TMX-file
  4.  * #Files:  Writes 'selected_files_<date_time>.tmx' in the current project's root
  5.  * #File format:    TMX v.1.4
  6.  * #Details:    http://wp.me/p3fHEs-6g
  7.  *
  8.  * @author  Kos Ivantsov
  9.  * @date    2013-08-12
  10.  * @version 0.3
  11.  */
  12.  
  13. import javax.swing.JFileChooser
  14. import org.omegat.util.StaticUtils
  15. import org.omegat.util.TMXReader
  16. import static javax.swing.JOptionPane.*
  17. import static org.omegat.util.Platform.*
  18.  
  19. def prop = project.projectProperties
  20. if (!prop) {
  21.     final def title = 'Export TMX from selected files'
  22.     final def msg   = 'Please try again after you open a project.'
  23.     showMessageDialog null, msg, title, INFORMATION_MESSAGE
  24.     return
  25. }
  26.  
  27. def curtime = new Date().format("MMM-dd-yyyy_HH.mm")
  28. srcroot = new File(prop.getSourceRoot())
  29. def fileloc = prop.projectRoot+'selected_files_'+curtime+'.tmx'
  30. exportfile = new File(fileloc)
  31. def sourceroot = prop.getSourceRoot().toString() as String
  32.  
  33. JFileChooser fc = new JFileChooser(
  34.     currentDirectory: srcroot,
  35.     dialogTitle: "Choose files to export",
  36.     fileSelectionMode: JFileChooser.FILES_ONLY,
  37.     //the file filter must show also directories, in order to be able to look into them
  38.     multiSelectionEnabled: true)
  39.  
  40. if(fc.showOpenDialog() != JFileChooser.APPROVE_OPTION) {
  41. console.println "Canceled"
  42. return
  43. }
  44.  
  45. if (!(fc.selectedFiles =~ sourceroot.replaceAll(/\\+/, '\\\\\\\\'))) {
  46.         console.println "Selection outside of ${prop.getSourceRoot()} folder"
  47.         final def title = 'Wrong file(s) selected'
  48.         final def msg   = "Files must be in ${prop.getSourceRoot()} folder."
  49.         showMessageDialog null, msg, title, INFORMATION_MESSAGE
  50.         return
  51.     }
  52.  
  53. if (prop.isSentenceSegmentingEnabled())
  54.     segmenting = TMXReader.SEG_SENTENCE
  55.     else
  56.     segmenting = TMXReader.SEG_PARAGRAPH
  57.  
  58. def sourceLocale = prop.getSourceLanguage().toString()
  59. def targetLocale = prop.getTargetLanguage().toString()
  60.  
  61. exportfile.write("", 'UTF-8')
  62. exportfile.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", 'UTF-8')
  63. exportfile.append("<!DOCTYPE tmx SYSTEM \"tmx11.dtd\">\n", 'UTF-8')
  64. exportfile.append("<tmx version=\"1.4\">\n", 'UTF-8')
  65. exportfile.append(" <header\n", 'UTF-8')
  66. exportfile.append("  creationtool=\"OmegaTScripting\"\n", 'UTF-8')
  67. exportfile.append("  segtype=\"" + segmenting + "\"\n", 'UTF-8')
  68. exportfile.append("  o-tmf=\"OmegaT TMX\"\n", 'UTF-8')
  69. exportfile.append("  adminlang=\"EN-US\"\n", 'UTF-8')
  70. exportfile.append("  srclang=\"" + sourceLocale + "\"\n", 'UTF-8')
  71. exportfile.append("  datatype=\"plaintext\"\n", 'UTF-8')
  72. exportfile.append(" >\n", 'UTF-8')
  73. fc.selectedFiles.each{
  74.     fl = "${it.toString()}" - "$sourceroot"
  75.     exportfile.append("  <prop type=\"Filename\">" + fl + "</prop>\n", 'UTF-8')
  76. }
  77. exportfile.append(" </header>\n", 'UTF-8')
  78. exportfile.append("  <body>\n", 'UTF-8')
  79.  
  80. def count = 0
  81. fc.selectedFiles.each{
  82.     fl = "${it.toString()}" - "$sourceroot"
  83.     files = project.projectFiles
  84.     files.each{
  85.         if ( "${it.filePath}" != "$fl" ) {
  86.         println "Skipping to the next file"
  87.         }else{
  88.     it.entries.each {
  89.     def info = project.getTranslationInfo(it)
  90.     def changeId = info.changer
  91.     def changeDate = info.changeDate
  92.     def creationId = info.creator
  93.     def creationDate = info.creationDate
  94.     def alt = 'unknown'
  95.     if (info.isTranslated()) {
  96.         source = StaticUtils.makeValidXML(it.srcText)
  97.         target = StaticUtils.makeValidXML(info.translation)
  98.         exportfile.append("    <tu>\n", 'UTF-8')
  99.         exportfile.append("      <tuv xml:lang=\"" + sourceLocale + "\">\n", 'UTF-8')
  100.         exportfile.append("        <seg>" + "$source" + "</seg>\n", 'UTF-8')
  101.         exportfile.append("      </tuv>\n", 'UTF-8')
  102.         exportfile.append("      <tuv xml:lang=\"" + targetLocale + "\"", 'UTF-8')
  103.         exportfile.append(" changeid=\"${changeId ?: alt }\"", 'UTF-8')
  104.         exportfile.append(" changedate=\"${ changeDate > 0 ? new Date(changeDate).format("yyyyMMdd'T'HHmmss'Z'") : alt }\"", 'UTF-8')
  105.         exportfile.append(" creationid=\"${creationId ?: alt }\"", 'UTF-8')
  106.         exportfile.append(" creationdate=\"${ creationDate > 0 ? new Date(creationDate).format("yyyyMMdd'T'HHmmss'Z'") : alt }\"", 'UTF-8')
  107.         exportfile.append(">\n", 'UTF-8')
  108.         exportfile.append("        <seg>" + "$target" + "</seg>\n", 'UTF-8')
  109.         exportfile.append("      </tuv>\n", 'UTF-8')
  110.         exportfile.append("    </tu>\n", 'UTF-8')
  111.         count++;
  112.                 }
  113.             }
  114.         }
  115.     }
  116. }
  117. exportfile.append("  </body>\n", 'UTF-8')
  118. exportfile.append("</tmx>", 'UTF-8')
  119.  
  120. final def title = 'TMX file written'
  121. final def msg   = "$count TU's written to " + exportfile.toString()
  122. console.println msg
  123. showMessageDialog null, msg, title, INFORMATION_MESSAGE
  124. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement