Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * #Purpose: Pretranslate certain untranslated segments
- * #Files: Requires 'pretranslate.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.
- * #Note: Only those segment where a [Search Pattern] is found,
- * are pretranslated and prefixed with {PRETRAN}
- * #Details: http://wp.me/p3fHEs-5e
- *
- * @author Kos Ivantsov
- * @author Yu Tang
- * @based on Didier Briel's "search and replace script"
- * @date 2013-12-28
- * @version 0.2
- */
- import static javax.swing.JOptionPane.*
- import static org.omegat.util.Platform.*
- import groovy.swing.SwingBuilder
- import java.awt.Component
- import javax.swing.JButton
- import javax.swing.JTable
- import javax.swing.table.*
- import javax.swing.event.*
- import java.awt.event.*
- import java.awt.BorderLayout as BL
- /*
- * set 'GUI' to anything but 'true' (with quotes) in the next line
- * if you don't need a GUI listing of all changed segments
- */
- def GUI = 'true'
- /*
- * set true to process current document only,
- * otherwise process whole project.
- */
- def CURRENT_FILE_ONLY = false
- def prop = project.projectProperties
- if (!prop) {
- final def title = 'Pretranslate'
- final def msg = 'Please try again after you open a project.'
- showMessageDialog null, msg, title, INFORMATION_MESSAGE
- return
- }
- subst_file = new File(prop.projectRoot, '.ini/pretranslate.ini')
- if (! subst_file.exists()) {
- final def title = 'No file'
- final def msg = 'Template file for pretranslation'+'\n' + subst_file +'\n'+'doesn\'t exist.'
- showMessageDialog null, msg, title, INFORMATION_MESSAGE
- return
- }
- length = subst_file.readLines().size()
- search_array = []
- replace_array = []
- data = []
- 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() - 1)
- def segment_count = 0
- console.println "Do a search and replace.\n${'-'*10}\n"
- // target entries
- def targetEntries = CURRENT_FILE_ONLY ? project.projectFiles.get(editor.displayedFileIndex).entries
- : project.allEntries
- targetEntries.each { ste ->
- // Skip translated segments
- if (project.getTranslationInfo(ste).translation) {
- return
- }
- initial_target = target = source = ste.srcText
- 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 (initial_target != target) {
- data.add([ seg: ste.entryNum(), source: source, in_target: initial_target, target: target ])
- segment_count++
- editor.gotoEntry(ste.entryNum())
- console.println(ste.entryNum() + "\t" + ste.srcText + "\t" + target )
- target = target.replaceAll(/(^)/, '\\{PRETRAN\\}$1')
- editor.replaceEditText(target)
- }
- }
- swing = new SwingBuilder()
- frame = swing.frame(title:'Changed segments', preferredSize: [1024, 500]) {
- scrollPane {
- table() {
- tableModel(list:data) {
- propertyColumn(editable: true, header:'Segment', propertyName:'seg', minWidth: 80, maxWidth: 80, preferredWidth: 80,
- cellEditor: new TableCellEditor()
- {
- public void cancelCellEditing() {}
- public boolean stopCellEditing() { return false; }
- public Object getCellEditorValue() { return value; }
- public boolean isCellEditable(EventObject anEvent) { return true; }
- public boolean shouldSelectCell(EventObject anEvent) {return true; }
- public void addCellEditorListener(CellEditorListener l) {}
- public void removeCellEditorListener(CellEditorListener l) {}
- public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
- {
- println("value: " + value);
- org.omegat.core.Core.editor.gotoEntry value
- }
- },
- cellRenderer: new TableCellRenderer()
- {
- public Component getTableCellRendererComponent(JTable table,
- Object value,
- boolean isSelected,
- boolean hasFocus,
- int row,
- int column)
- {
- def btn = new JButton()
- btn.setText(value.toString())
- return btn
- }
- }
- )
- propertyColumn(editable: false, header:'Source',propertyName:'source', minWidth: 150, preferredWidth: 350)
- propertyColumn(editable: false, header:'Initial Target',propertyName:'in_target', minWidth: 150, preferredWidth: 350)
- propertyColumn(editable: false, header:'Target',propertyName:'target', minWidth: 150, preferredWidth: 350)
- }
- }
- }
- panel(constraints: BL.SOUTH){
- button('Quit', actionPerformed:{
- frame.dispose()
- })
- }
- }
- console.println "\n${'-'*10}"+"\n"+"Segments modified: " + segment_count
- if (segment_count == 0){
- final def title = 'Pretranslate'
- final def msg = 'No segments can be pretranslated.'
- showMessageDialog null, msg, title, INFORMATION_MESSAGE
- return
- }
- if (GUI == 'true' && segment_count) {
- frame.pack()
- frame.show()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement