Advertisement
KosIvantsov

write_source_target.groovy

Dec 23rd, 2013
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 1.76 KB | None | 0 0
  1. /*
  2.  * #Purpose:    Write all source segments to a file
  3.  * #Files:  Writes 'source_target.txt' in the 'script_output' subfolder
  4.  *  in the current project's root
  5.  *
  6.  * @author: Kos Ivantsov
  7.  * @date:   2013-07-16
  8.  * @version:    0.2
  9.  */
  10.  
  11. /*
  12.  * Here you can specify delimiters for source and target, and segments
  13.  */
  14.  
  15. source_target_delim='\n' //one line break
  16. segment_delim="\n"*3 //three line breaks
  17.  
  18.  
  19. import static javax.swing.JOptionPane.*
  20. import static org.omegat.util.Platform.*
  21.  
  22. // abort if a project is not opened yet
  23. def prop = project.projectProperties
  24. if (!prop) {
  25.     final def title = 'Source to File'
  26.     final def msg   = 'Please try again after you open a project.'
  27.     showMessageDialog null, msg, title, INFORMATION_MESSAGE
  28.     return
  29. }
  30.  
  31. def folder = prop.projectRoot+'/script_output'
  32. def fileloc = folder+'/source_target.txt'
  33. writefile = new File(fileloc)
  34. if (! (new File(folder)).exists()) {
  35.     (new File(folder)).mkdir()
  36.     }
  37.  
  38. writefile.write("", 'UTF-8')
  39. def count = 0
  40.  
  41. files = project.projectFiles;
  42. for (i in 0 ..< files.size())
  43. {
  44.     fi = files[i];
  45.     marker = "+${'='*fi.filePath.size()}+\n"
  46.     writefile.append("$marker|$fi.filePath|\n$marker", 'UTF-8')
  47.     for (j in 0 ..< fi.entries.size())
  48.     {
  49.     ste = fi.entries[j]
  50.     source = ste.getSrcText()
  51.     target = project.getTranslationInfo(ste) ? project.getTranslationInfo(ste).translation : null;
  52.         if (target == null)
  53.         target = ''
  54.         if (target.size() == 0 )
  55.         target = "<EMPTY>"
  56.     writefile.append source +source_target_delim+target+segment_delim, 'UTF-8'
  57.     count++;
  58.     }
  59. }
  60. console.println count +" segments written to "+ writefile
  61. final def title = 'Source and Target to File'
  62. final def msg   = count +" segments"+"\n"+"written to \n"+ writefile
  63. showMessageDialog null, msg, title, INFORMATION_MESSAGE
  64. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement