Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Purpose: Modify current target sequentially replacing [Search patterns]
- * with [Replace patterns], specified in external file
- * #Files: Requires 'segment_substitution.ini' in '.ini' subfolder
- * of the current project's root
- * #File format: Plain text, where *each* line is:
- * [Search Pattern] [Tab] [Replace Pattern];
- * only the last line *must* be empty.
- * #Details: http://wp.me/p3fHEs-5e
- *
- * @author Kos Ivantsov (with major contributions by Yu Tang)
- * @date 2013-07-12
- * @version 0.3
- */
- import static javax.swing.JOptionPane.*
- import static org.omegat.util.Platform.*
- def prop = project.projectProperties
- if (!prop) {
- final def title = 'Replace using substitution file'
- final def msg = 'Please try again after you open a project.'
- showMessageDialog null, msg, title, INFORMATION_MESSAGE
- return
- }
- def folder = prop.projectRoot
- def fileloc = folder+'.ini/segment_substitution.ini'
- subst_file = new File(fileloc)
- if (! subst_file.exists()) {
- final def title = 'No file'
- final def msg = 'Substitution file ' + subst_file + ' doesn\'t exist.'
- showMessageDialog null, msg, title, INFORMATION_MESSAGE
- return
- }
- length = subst_file.readLines().size()
- search_array = []
- replace_array = []
- def count = 0
- while ( count < length ) {
- ln = subst_file.readLines().get(count).tokenize('\t')
- sr = ln[0]
- rp = ln[1]
- search_array.add(sr)
- replace_array.add(rp)
- count++
- }
- def range = 0..<search_array.size()
- if (editor.selectedText){
- target = editor.selectedText
- }else
- /*
- * The script can either use source text, replacing the target after all
- * substitutions, or it can use the text that is already placed as translation,
- * replacing it with itself after substituting
- * To enable desired behavior, use one of the two following lines (be
- * sure to have the other commented out)
- */
- //target = editor.currentEntry.getSrcText();
- target = editor.getCurrentTranslation()
- for ( i in range) {
- ser = search_array[i]
- rep = (replace_array[i] =~ /\$(\d+)/ ).replaceAll( '\\${(it[$1] as String) }' )
- shell = new GroovyShell()
- eval = {statement, arg -> shell.setVariable 'it', arg; shell.evaluate '"' + statement + '"' }
- target = target.replaceAll(/null/, 'repl0')
- target = target.replaceAll(ser) { eval rep, it }
- target = target.replaceAll(/null/, '')
- target = target.replaceAll(/repl0/, 'null')
- }
- if (editor.selectedText){
- editor.insertText(target)
- }else
- editor.replaceEditText(target)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement