SHARE
TWEET

search_replace_pretranslate.groovy

KosIvantsov Jul 12th, 2013 150 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * #Purpose:    Pretranslate certain untranslated segments
  3.  * #Files:      Requires 'pretranslate.ini' in .ini subfolder
  4.  *              of the current project's root
  5.  * #File format:    Plain text, where *each* line is:
  6.  *    [Search Pattern] [Tab] [Replace Pattern];
  7.  *    only the last line *must* be empty.
  8.  * #Note:    Only those segment where a [Search Pattern] is found,
  9.  *    are pretranslated and prefixed with {PRETRAN}
  10.  * #Details:    http://wp.me/p3fHEs-5e
  11.  *
  12.  * @author    Kos Ivantsov
  13.  * @author    Yu Tang
  14.  * @based on Didier Briel's "search and replace script"
  15.  * @date    2013-12-28
  16.  * @version 0.2
  17.  */
  18.  
  19. import static javax.swing.JOptionPane.*
  20. import static org.omegat.util.Platform.*
  21. import groovy.swing.SwingBuilder
  22. import java.awt.Component
  23. import javax.swing.JButton
  24. import javax.swing.JTable
  25. import javax.swing.table.*
  26. import javax.swing.event.*
  27. import java.awt.event.*
  28. import java.awt.BorderLayout as BL
  29.  
  30. /*
  31.  * set 'GUI' to anything but 'true' (with quotes) in the next line
  32.  * if you don't need a GUI listing of all changed segments
  33.  */
  34. def GUI = 'true'
  35.  
  36. /*
  37.  * set true to process current document only,
  38.  * otherwise process whole project.
  39.  */
  40. def CURRENT_FILE_ONLY = false
  41.  
  42. def prop = project.projectProperties
  43. if (!prop) {
  44.     final def title = 'Pretranslate'
  45.     final def msg   = 'Please try again after you open a project.'
  46.     showMessageDialog null, msg, title, INFORMATION_MESSAGE
  47.     return
  48. }
  49.  
  50. subst_file = new File(prop.projectRoot, '.ini/pretranslate.ini')
  51.  
  52. if (! subst_file.exists()) {
  53.     final def title = 'No file'
  54.     final def msg   = 'Template file for pretranslation'+'\n' + subst_file +'\n'+'doesn\'t exist.'
  55.     showMessageDialog null, msg, title, INFORMATION_MESSAGE
  56.     return
  57. }
  58.  
  59. length = subst_file.readLines().size()
  60. search_array = []
  61. replace_array = []
  62. data = []
  63. def count = 0
  64.  
  65. while ( count < length ) {
  66.     ln = subst_file.readLines().get(count).tokenize('\t')
  67.     sr = ln[0]
  68.     rp = ln[1]
  69.     search_array.add(sr)
  70.     replace_array.add(rp)
  71.     count++
  72. }
  73.  
  74. def range = 0..(search_array.size() - 1)
  75.  
  76. def segment_count = 0
  77.  
  78. console.println "Do a search and replace.\n${'-'*10}\n"
  79.  
  80. // target entries
  81. def targetEntries = CURRENT_FILE_ONLY ? project.projectFiles.get(editor.displayedFileIndex).entries
  82.                                       : project.allEntries
  83.  
  84. targetEntries.each { ste ->
  85.     // Skip translated segments
  86.     if (project.getTranslationInfo(ste).translation) {
  87.         return
  88.     }
  89.  
  90.     initial_target = target = source = ste.srcText
  91.  
  92.     for ( i in range) {
  93.         ser = search_array[i]
  94.         rep = (replace_array[i] =~ /\$(\d+)/ ).replaceAll( '\\${(it[$1] as String) }' )
  95.         shell = new GroovyShell()
  96.         eval = {statement, arg -> shell.setVariable 'it', arg; shell.evaluate '"' + statement + '"' }
  97.         target = target.replaceAll(/null/, 'repl0')
  98.         target = target.replaceAll(ser) { eval rep, it }
  99.         target = target.replaceAll(/null/, '')
  100.         target = target.replaceAll(/repl0/, 'null')
  101.     }
  102.  
  103.  
  104.     if (initial_target != target) {
  105.         data.add([ seg: ste.entryNum(), source: source, in_target: initial_target, target: target ])
  106.         segment_count++
  107.         editor.gotoEntry(ste.entryNum())
  108.         console.println(ste.entryNum() + "\t" + ste.srcText + "\t" + target )
  109.         target = target.replaceAll(/(^)/, '\\{PRETRAN\\}$1')
  110.         editor.replaceEditText(target)
  111.     }
  112. }
  113.  
  114. swing = new SwingBuilder()
  115. frame = swing.frame(title:'Changed segments', preferredSize: [1024, 500]) {
  116.     scrollPane {
  117.         table() {
  118.             tableModel(list:data) {
  119.                 propertyColumn(editable: true, header:'Segment', propertyName:'seg', minWidth: 80, maxWidth: 80, preferredWidth: 80,
  120.                         cellEditor: new TableCellEditor()
  121.                         {
  122.                             public void cancelCellEditing()    {}
  123.                             public boolean stopCellEditing()    {   return false;   }
  124.                             public Object getCellEditorValue()    {   return value;   }
  125.                             public boolean isCellEditable(EventObject anEvent)    {   return true;    }
  126.                             public boolean shouldSelectCell(EventObject anEvent)    {return true; }
  127.                             public void addCellEditorListener(CellEditorListener l)    {}
  128.                             public void removeCellEditorListener(CellEditorListener l)    {}
  129.                             public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
  130.                             {
  131.                                 println("value: " + value);
  132.                                 org.omegat.core.Core.editor.gotoEntry value
  133.                             }
  134.                            
  135.                         },
  136.                         cellRenderer: new TableCellRenderer()
  137.                         {
  138.                             public Component getTableCellRendererComponent(JTable table,
  139.                             Object value,
  140.                             boolean isSelected,
  141.                             boolean hasFocus,
  142.                             int row,
  143.                             int column)
  144.                             {
  145.                                 def btn = new JButton()
  146.                                 btn.setText(value.toString())
  147.                                 return btn
  148.                             }
  149.                         }
  150.                         )
  151.                 propertyColumn(editable: false, header:'Source',propertyName:'source', minWidth: 150, preferredWidth: 350)
  152.                 propertyColumn(editable: false, header:'Initial Target',propertyName:'in_target', minWidth: 150, preferredWidth: 350)
  153.                 propertyColumn(editable: false, header:'Target',propertyName:'target', minWidth: 150, preferredWidth: 350)
  154.             }
  155.         }
  156.        
  157.     }
  158.     panel(constraints: BL.SOUTH){
  159.         button('Quit', actionPerformed:{
  160.             frame.dispose()
  161.         })
  162.     }
  163. }
  164.  
  165. console.println "\n${'-'*10}"+"\n"+"Segments modified: " + segment_count
  166.  
  167. if (segment_count == 0){
  168.     final def title = 'Pretranslate'
  169.     final def msg   = 'No segments can be pretranslated.'
  170.     showMessageDialog null, msg, title, INFORMATION_MESSAGE
  171.     return
  172. }
  173.  
  174. if (GUI == 'true' && segment_count) {
  175.     frame.pack()
  176.     frame.show()
  177. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top