Johannes Schindelin | cd38c86 | 2007-09-24 23:24:12 -0400 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # Tcl ignores the next line -*- tcl -*- \ |
| 3 | exec tclsh "$0" -- "$@" |
| 4 | |
| 5 | # This is a really stupid program, which serves as an alternative to |
| 6 | # msgfmt. It _only_ translates to Tcl mode, does _not_ validate the |
| 7 | # input, and does _not_ output any statistics. |
| 8 | |
| 9 | proc u2a {s} { |
| 10 | set res "" |
| 11 | foreach i [split $s ""] { |
| 12 | scan $i %c c |
| 13 | if {$c<128} { |
Alexander Gavrilov | 9dc3793 | 2008-08-31 01:12:26 +0400 | [diff] [blame] | 14 | # escape '[', '\', '$' and ']' |
| 15 | if {$c == 0x5b || $c == 0x5d || $c == 0x24} { |
Johannes Schindelin | cd38c86 | 2007-09-24 23:24:12 -0400 | [diff] [blame] | 16 | append res "\\" |
| 17 | } |
| 18 | append res $i |
| 19 | } else { |
| 20 | append res \\u[format %04.4x $c] |
| 21 | } |
| 22 | } |
| 23 | return $res |
| 24 | } |
| 25 | |
| 26 | set output_directory "." |
| 27 | set lang "dummy" |
| 28 | set files [list] |
Johannes Schindelin | 95a8b67 | 2007-10-30 11:25:31 +0000 | [diff] [blame] | 29 | set show_statistics 0 |
Johannes Schindelin | cd38c86 | 2007-09-24 23:24:12 -0400 | [diff] [blame] | 30 | |
| 31 | # parse options |
Johannes Schindelin | 95a8b67 | 2007-10-30 11:25:31 +0000 | [diff] [blame] | 32 | for {set i 0} {$i < $argc} {incr i} { |
Johannes Schindelin | cd38c86 | 2007-09-24 23:24:12 -0400 | [diff] [blame] | 33 | set arg [lindex $argv $i] |
Johannes Schindelin | 95a8b67 | 2007-10-30 11:25:31 +0000 | [diff] [blame] | 34 | if {$arg == "--statistics"} { |
| 35 | incr show_statistics |
| 36 | continue |
| 37 | } |
| 38 | if {$arg == "--tcl"} { |
| 39 | # we know |
Johannes Schindelin | cd38c86 | 2007-09-24 23:24:12 -0400 | [diff] [blame] | 40 | continue |
| 41 | } |
| 42 | if {$arg == "-l"} { |
| 43 | incr i |
| 44 | set lang [lindex $argv $i] |
| 45 | continue |
| 46 | } |
| 47 | if {$arg == "-d"} { |
| 48 | incr i |
| 49 | set tmp [lindex $argv $i] |
| 50 | regsub "\[^/\]$" $tmp "&/" output_directory |
| 51 | continue |
| 52 | } |
| 53 | lappend files $arg |
| 54 | } |
| 55 | |
| 56 | proc flush_msg {} { |
Johannes Schindelin | f94872d | 2007-10-30 11:24:37 +0000 | [diff] [blame] | 57 | global msgid msgstr mode lang out fuzzy |
Johannes Schindelin | 95a8b67 | 2007-10-30 11:25:31 +0000 | [diff] [blame] | 58 | global translated_count fuzzy_count not_translated_count |
Johannes Schindelin | cd38c86 | 2007-09-24 23:24:12 -0400 | [diff] [blame] | 59 | |
| 60 | if {![info exists msgid] || $mode == ""} { |
| 61 | return |
| 62 | } |
| 63 | set mode "" |
Johannes Schindelin | f94872d | 2007-10-30 11:24:37 +0000 | [diff] [blame] | 64 | if {$fuzzy == 1} { |
Johannes Schindelin | 95a8b67 | 2007-10-30 11:25:31 +0000 | [diff] [blame] | 65 | incr fuzzy_count |
Johannes Schindelin | f94872d | 2007-10-30 11:24:37 +0000 | [diff] [blame] | 66 | set fuzzy 0 |
| 67 | return |
| 68 | } |
Johannes Schindelin | cd38c86 | 2007-09-24 23:24:12 -0400 | [diff] [blame] | 69 | |
| 70 | if {$msgid == ""} { |
| 71 | set prefix "set ::msgcat::header" |
| 72 | } else { |
Johannes Schindelin | 9a25ae8 | 2007-10-30 11:24:53 +0000 | [diff] [blame] | 73 | if {$msgstr == ""} { |
Johannes Schindelin | 95a8b67 | 2007-10-30 11:25:31 +0000 | [diff] [blame] | 74 | incr not_translated_count |
Johannes Schindelin | 9a25ae8 | 2007-10-30 11:24:53 +0000 | [diff] [blame] | 75 | return |
| 76 | } |
Johannes Schindelin | cd38c86 | 2007-09-24 23:24:12 -0400 | [diff] [blame] | 77 | set prefix "::msgcat::mcset $lang \"[u2a $msgid]\"" |
Johannes Schindelin | 95a8b67 | 2007-10-30 11:25:31 +0000 | [diff] [blame] | 78 | incr translated_count |
Johannes Schindelin | cd38c86 | 2007-09-24 23:24:12 -0400 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | puts $out "$prefix \"[u2a $msgstr]\"" |
| 82 | } |
| 83 | |
Johannes Schindelin | f94872d | 2007-10-30 11:24:37 +0000 | [diff] [blame] | 84 | set fuzzy 0 |
Johannes Schindelin | 95a8b67 | 2007-10-30 11:25:31 +0000 | [diff] [blame] | 85 | set translated_count 0 |
| 86 | set fuzzy_count 0 |
| 87 | set not_translated_count 0 |
Johannes Schindelin | cd38c86 | 2007-09-24 23:24:12 -0400 | [diff] [blame] | 88 | foreach file $files { |
| 89 | regsub "^.*/\(\[^/\]*\)\.po$" $file "$output_directory\\1.msg" outfile |
| 90 | set in [open $file "r"] |
| 91 | fconfigure $in -encoding utf-8 |
| 92 | set out [open $outfile "w"] |
| 93 | |
| 94 | set mode "" |
| 95 | while {[gets $in line] >= 0} { |
| 96 | if {[regexp "^#" $line]} { |
Johannes Schindelin | f94872d | 2007-10-30 11:24:37 +0000 | [diff] [blame] | 97 | if {[regexp ", fuzzy" $line]} { |
| 98 | set fuzzy 1 |
| 99 | } else { |
| 100 | flush_msg |
| 101 | } |
Johannes Schindelin | cd38c86 | 2007-09-24 23:24:12 -0400 | [diff] [blame] | 102 | continue |
| 103 | } elseif {[regexp "^msgid \"(.*)\"$" $line dummy match]} { |
| 104 | flush_msg |
| 105 | set msgid $match |
| 106 | set mode "msgid" |
| 107 | } elseif {[regexp "^msgstr \"(.*)\"$" $line dummy match]} { |
| 108 | set msgstr $match |
| 109 | set mode "msgstr" |
| 110 | } elseif {$line == ""} { |
| 111 | flush_msg |
| 112 | } elseif {[regexp "^\"(.*)\"$" $line dummy match]} { |
| 113 | if {$mode == "msgid"} { |
| 114 | append msgid $match |
| 115 | } elseif {$mode == "msgstr"} { |
| 116 | append msgstr $match |
| 117 | } else { |
| 118 | puts stderr "I do not know what to do: $match" |
| 119 | } |
| 120 | } else { |
| 121 | puts stderr "Cannot handle $line" |
| 122 | } |
| 123 | } |
| 124 | flush_msg |
| 125 | close $in |
| 126 | close $out |
| 127 | } |
| 128 | |
Johannes Schindelin | 95a8b67 | 2007-10-30 11:25:31 +0000 | [diff] [blame] | 129 | if {$show_statistics} { |
Shawn O. Pearce | 2cd9ad2 | 2008-01-22 23:52:07 -0500 | [diff] [blame] | 130 | set str "" |
| 131 | |
| 132 | append str "$translated_count translated message" |
| 133 | if {$translated_count != 1} { |
| 134 | append str s |
| 135 | } |
| 136 | |
| 137 | if {$fuzzy_count > 1} { |
| 138 | append str ", $fuzzy_count fuzzy translation" |
| 139 | if {$fuzzy_count != 1} { |
| 140 | append str s |
| 141 | } |
| 142 | } |
| 143 | if {$not_translated_count > 0} { |
| 144 | append str ", $not_translated_count untranslated message" |
| 145 | if {$not_translated_count != 1} { |
| 146 | append str s |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | append str . |
| 151 | puts $str |
Johannes Schindelin | 95a8b67 | 2007-10-30 11:25:31 +0000 | [diff] [blame] | 152 | } |