Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Purpose: Insert custom string specified in external file, one by one
- * #Files: Requires 'custom_strings.ini' in '.ini' subfolder
- * in the current project's root
- * #File format: Plain text, where *each* line is:
- * [Sting in the source] [Tab] [String to insert];
- * only the last line *must* be empty.
- * #Details: http://wp.me/p3fHEs-86
- *
- * @author Kos Ivantsov
- * @date 2013-10-15
- * @version 0.3
- */
- import org.omegat.util.StaticUtils
- import static javax.swing.JOptionPane.*
- import static org.omegat.util.Platform.*
- use_seg_subs = 'no'
- def prop = project.projectProperties
- if (!prop) {
- final def title = 'Insert custom strings'
- final def msg = 'Please try again after you open a project.'
- console.println msg
- showMessageDialog null, msg, title, INFORMATION_MESSAGE
- return
- }
- def srcfile = editor.currentFile
- def ste = editor.currentEntry
- def cur_text = ste.getSrcText()
- def cur_seg = ste.entryNum()
- def configdir = StaticUtils.getConfigDir()
- def folder = prop.projectRoot
- def fileloc = folder+'.ini/custom_strings.ini'
- def stringfile = new File(fileloc)
- def infofile
- infofile = configdir+'script/custom_strings.txt/'
- infofile = new File(infofile)
- srcexport = new File(configdir+'script/source.txt/')
- srcmod = srcexport.lastModified().toString()
- subst_file = new File(folder+'.ini/segment_substitution.ini')
- if (! stringfile.exists()) {
- final def title = 'No file'
- final def msg = """\
- File $stringfile
- that defines strings to be inserted doesn't exist\
- """
- showMessageDialog null, msg, title, INFORMATION_MESSAGE
- console.println msg
- return
- }
- if (! subst_file.exists()) {
- use_seg_subs = 'no'
- }
- def length = stringfile.readLines().size()
- def search_array = []
- def replace_array = []
- def count = 0
- while ( count < length ) {
- ln = stringfile.readLines().get(count).tokenize('\t')
- sr = ln[0]
- rp = ln[1]
- search_array.add(sr)
- replace_array.add(rp)
- count++
- }
- def tag_index_array = []
- def cur_txt_array = []
- def range = 0..<search_array.size()
- for (i in range) {
- cur_text.findAll(search_array[i]) {
- if (it =~ "\\["){
- it = it[0]}
- tag_index_array.add("${cur_text.indexOf(it)}:$i")
- cur_txt_array.add("${cur_text.indexOf(it)}<rem>$it")
- }
- }
- tag_index_array.sort{it.findAll("\\d+:")}
- cur_txt_array.sort{it.findAll("\\d+<")}
- for (i in 0..<tag_index_array.size()){
- newitem = tag_index_array[i].replaceAll("\\d+:", "")
- tag_index_array.putAt(i, newitem)
- newword = cur_txt_array[i].replaceAll("\\d+<rem>", "")
- cur_txt_array.putAt(i, newword)
- }
- def seg_tag_count = tag_index_array.size()
- if (seg_tag_count == 0){
- console.println "no insert_strings in current segment"
- return
- }
- def curtag_number
- def writeinfo(file, src, seg, num, mod) {
- file.write(src+"\n",'UTF-8')
- file.append(seg+"\n", 'UTF-8')
- file.append(mod+"\n", 'UTF-8')
- file.append(num+"\n", 'UTF-8')
- }
- if (!infofile.exists() || infofile.readLines().get(0) != "$srcfile" || infofile.readLines().get(1) != "$cur_seg" || infofile.readLines().get(2) != "$srcmod") {
- writeinfo(infofile, srcfile, cur_seg, "0", srcmod)
- curtag_number = infofile.readLines().get(3) as int
- if (seg_tag_count > 1) {
- writeinfo(infofile, srcfile, cur_seg, "1", srcmod)
- }
- } else {
- curtag_number = infofile.readLines().get(3) as int
- if (curtag_number < seg_tag_count-1){
- writeinfo(infofile, srcfile, cur_seg, "${curtag_number+1}", srcmod)
- } else
- writeinfo(infofile, srcfile, cur_seg, "0", srcmod)
- }
- num = tag_index_array[curtag_number] as int
- text = cur_txt_array[curtag_number]
- ser = search_array[num]
- rep = (replace_array[num] =~ /\$(\d+)/ ).replaceAll( '\\${(it[$1] as String) }' )
- shell = new GroovyShell()
- eval = {statement, arg -> shell.setVariable 'it', arg; shell.evaluate '"' + statement + '"' }
- text = text.replaceAll(/null/, 'repl0')
- text = text.replaceAll(ser) { eval rep, it }
- text = text.replaceAll(/null/, '')
- text = text.replaceAll(/repl0/, 'null')
- if (use_seg_subs == 'yes'){
- length = subst_file.readLines().size()
- search_array = []
- replace_array = []
- 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++
- }
- range = 0..<search_array.size()
- 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 + '"' }
- text = text.replaceAll(/null/, 'repl0')
- text = text.replaceAll(ser) { eval rep, it }
- text = text.replaceAll(/null/, '')
- text = text.replaceAll(/repl0/, 'null')
- }
- }
- editor.insertText(text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement