Advertisement
hcartagena

write_sel_files2TMX_hc.groovy

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