Advertisement
KosIvantsov

replace_with_template.groovy

Jun 19th, 2013
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 1.60 KB | None | 0 0
  1. /*
  2.  *  Substitute with template
  3.  *
  4.  * @author  Kos Ivantsov
  5.  * @date    2013-06-20
  6.  * @version 0.1
  7.  */
  8.  
  9. import static javax.swing.JOptionPane.*
  10. import static org.omegat.util.Platform.*
  11.  
  12. def prop = project.projectProperties
  13. if (!prop) {
  14.   final def title = 'Replace using substitution file'
  15.   final def msg   = 'Please try again after you open a project.'
  16.   showMessageDialog null, msg, title, INFORMATION_MESSAGE
  17.   return
  18. }
  19.  
  20. def folder = prop.projectRoot
  21. def fileloc = folder+'/subst_template.txt'
  22. subst_file = new File(fileloc)
  23.  
  24. if (! subst_file.exists()) {
  25.     final def title = 'No file' ;
  26.     final def msg   = 'Substitution file ' + subst_file + ' doesn\'t exist.' ;
  27.     showMessageDialog null, msg, title, INFORMATION_MESSAGE ;
  28.     return
  29.     }
  30.  
  31. length = subst_file.readLines().size() ;
  32. search_array = []
  33. replace_array = []
  34. def count = 0 ;
  35.  
  36. while ( count < length ) {
  37.     ln = subst_file.readLines().get(count).tokenize('\t')
  38.     sr = ln[0]
  39.     rp = ln[1]
  40.     search_array.add(sr)
  41.     replace_array.add(rp)
  42.     count++ ;
  43.     }
  44.  
  45. def range = 0..(search_array.size() - 1)
  46.  
  47. /*
  48.  * The script can either use source text, replacing the target after all  
  49.  * substitutions, or it can use the text that is already placed as translation,
  50.  * replacing it with itself after substituting
  51.  * To enable desired behaviour, use one of the two following lines (be
  52.  * sure to have the other commented out)
  53.  */
  54.  
  55. //target = editor.currentEntry.getSrcText();
  56. target = editor.getCurrentTranslation()
  57.  
  58. for ( i in range) {
  59.     target = (target =~ search_array[i] ).replaceAll( replace_array[i] )
  60.     }
  61.  
  62. editor.replaceEditText(target);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement