File: nbsp.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 (56 lines) | stat: -rw-r--r-- 2,309 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
/* :name=Non-breaking space :description=Replace spaces with non-breakable spaces where appropriate in French
 *
 * @author  Didier Briel
 * @author  Briac Pilpre
 * @date    2016-20-04
 * @version 0.3
 */

// search_string_before and replace_string_before are two variables representing the text to search and to replace, 
// respectively. search_string_after and replace_string_after have the same function
def search_string_before  = /\s([:?!;»])/
def replace_string_before = /\u00A0$1/
def search_string_percent = /(\d)\s%/
def replace_string_percent = /$1\u00A0%/
def search_string_after = /«\s/
def replace_string_after = /«\u00A0/

// The segment_count variable will be incremented each time a segment is modified.
def segment_count = 0
// cur_num stores the current segment number
def cur_num = editor.getCurrentEntry().entryNum()

project.allEntries.each { ste ->
  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 || target.length()== 0) return

  // The search_string is replaced by the replace_string in the translated text. 
  target = target.replaceAll(search_string_before, replace_string_before)
  target = target.replaceAll(search_string_percent, replace_string_percent)
  target = target.replaceAll(search_string_after, replace_string_after)

  // 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" + initial_target + "\t" + target )
    // Replace the translation
    editor.replaceEditText(target)
  }
 
}

 // We return to the initial segment
 editor.gotoEntry(cur_num)
 console.println("modified_segments: " + segment_count);