File: search_replace.groovy

package info (click to toggle)
omegat 3.6.0.10%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 63,728 kB
  • sloc: xml: 114,044; java: 74,758; sh: 174; javascript: 108; makefile: 22
file content (126 lines) | stat: -rw-r--r-- 4,912 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 *  Simple search and replace script
 *
 * @author  Didier Briel
 * @author  Briac Pilpre
 * @date    2016-07-20
 * @version 0.3.1
 */

 // The two following lines are there to allow the script to build a nice GUI for the text interface.
 // SwingBuilder is a Groovy class used to program GUI, nearly as easily as a HTML form. 
import groovy.swing.SwingBuilder
// BorderLayout -- shortened as 'BL' to be less verbose in the script -- is a Java component used to
// tell the GUI where to put the different elements.
import java.awt.BorderLayout as BL

// The @Bindable annotation is required to take into accounts the values in the form 
import groovy.beans.Bindable

// The SearchReplaceForm class is here to hold the data from the form.
// search and replace are two fields representing the text to search and to replace, 
// respectively. These fields are used by the SwingBuilder component which will 
// update the variables when the user changes them in the GUI. 
@Bindable
class FormData { String search, replace }

def search_string  = "" // You can use these two variables to pre-define the values
def replace_string = "" // of the search and replace fields

def formData = new FormData()
formData.search = search_string
formData.replace = replace_string

// The doReplace function above currently does nothing on its own. We will
// build a simple GUI to allow the user to enter the search and replace strings,
// and a button to launch the replacement.
new SwingBuilder().edt {
	frame(title:res.getString("name"), size: [350, 200], show: true) {
		borderLayout(vgap: 5)

		panel(constraints: BL.CENTER,
		border: compoundBorder([
			emptyBorder(10),
			titledBorder(res.getString("description"))
		])) {
	        // Just as in a HTML form, we can use a Table layout
	        // To with labels and Textfields.
			tableLayout {
				tr {
					td { label res.getString("search") }
					td { textField id:"searchField", text:formData.search, columns: 20  }
				}
				tr {
					td { label res.getString("replace") }
					td { textField id:"replaceField", text:formData.replace, columns: 20  }
				}
				tr {
					td { label "" }
					td {
						// When the button is clicked, the doReplace function will be called
						button(text:res.getString("button"),
						actionPerformed: {
							doReplace(formData.search, formData.replace)
						},
						constraints:BL.SOUTH)
					}
				}
			}

	 		// Binding of textfields to the formData object.
        		bean formData,
            		search:  bind { searchField.text },
            		replace: bind { replaceField.text }
		}
	}
}


// doReplace is a Groovy function. It will do the actual work of searching and replacing text. Note that the
// search_string and replace_string are the parameters of the function, and are not the same as the two 
// previously defined variables.
def doReplace(search_string, replace_string) {
	// The segment_count variable will be incremented each time a segment is modified.
	def segment_count = 0

	// "console" is an object from the OmegaT application. It represents the logging window, at the bottom of the 
	// script interface. It has only three methods: print, println and clear.
	// "res" is the ResourceBundle use to localize the script in different language.
	console.println(res.getString("description"));

	// Like "console", "project" is binded from OmegaT. This is the currently opened project.
	// The allEntries member will return all the segments of the project, and each segment will
	// be processed by the Groovy function between { ... }.
	// The current processed segment is named "ste" (for SourceTextEntry).
	project.allEntries.each { ste ->

		// Each segment has a source text, stored here in the source variable.
		source = ste.getSrcText();
		// If the segment has been translated, we get store translated text in the target variable.
		target = project.getTranslationInfo(ste) ? project.getTranslationInfo(ste).translation : null;
		
		// The translated text is copied to be able to compare it before and after the text replacement and
		// determine if the segment was modified.
		initial_target = target

		// Skip untranslated segments
		if (target == null) return

		// The search_string is replaced by the replace_string in the translated text. 
		target = target.replaceAll(search_string, replace_string)

		// The old translation is checked against the replaced text, if it is different,
		// we jump to the segment number and replace the old text by the new one.
		// "editor" is the OmegaT object used to manipulate the main OmegaT user interface.
		if (initial_target != target) {
			segment_count++
			// Jump to the segment number
			editor.gotoEntry(ste.entryNum())
			console.println(ste.entryNum() + "\t" + ste.srcText + "\t" + target )
			// Replace the translation
			editor.replaceEditText(target)
		}
	}

	console.println(res.getString("modified_segments") + segment_count);
}