]> asedeno.scripts.mit.edu Git - git.git/blob - po/po2msg.sh
91d420b4fbda333856dba25e8b112d37e4a0eb0c
[git.git] / po / po2msg.sh
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} {
14                         # escape '[', '\' and ']'
15                         if {$c == 0x5b || $c == 0x5d} {
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]
29
30 # parse options
31 for {set i 1} {$i < $argc} {incr i} {
32         set arg [lindex $argv $i]
33         if {$arg == "--statistics" || $arg == "--tcl"} {
34                 continue
35         }
36         if {$arg == "-l"} {
37                 incr i
38                 set lang [lindex $argv $i]
39                 continue
40         }
41         if {$arg == "-d"} {
42                 incr i
43                 set tmp [lindex $argv $i]
44                 regsub "\[^/\]$" $tmp "&/" output_directory
45                 continue
46         }
47         lappend files $arg
48 }
49
50 proc flush_msg {} {
51         global msgid msgstr mode lang out fuzzy
52
53         if {![info exists msgid] || $mode == ""} {
54                 return
55         }
56         set mode ""
57         if {$fuzzy == 1} {
58                 set fuzzy 0
59                 return
60         }
61
62         if {$msgid == ""} {
63                 set prefix "set ::msgcat::header"
64         } else {
65                 if {$msgstr == ""} {
66                         return
67                 }
68                 set prefix "::msgcat::mcset $lang \"[u2a $msgid]\""
69         }
70
71         puts $out "$prefix \"[u2a $msgstr]\""
72 }
73
74 set fuzzy 0
75 foreach file $files {
76         regsub "^.*/\(\[^/\]*\)\.po$" $file "$output_directory\\1.msg" outfile
77         set in [open $file "r"]
78         fconfigure $in -encoding utf-8
79         set out [open $outfile "w"]
80
81         set mode ""
82         while {[gets $in line] >= 0} {
83                 if {[regexp "^#" $line]} {
84                         if {[regexp ", fuzzy" $line]} {
85                                 set fuzzy 1
86                         } else {
87                                 flush_msg
88                         }
89                         continue
90                 } elseif {[regexp "^msgid \"(.*)\"$" $line dummy match]} {
91                         flush_msg
92                         set msgid $match
93                         set mode "msgid"
94                 } elseif {[regexp "^msgstr \"(.*)\"$" $line dummy match]} {
95                         set msgstr $match
96                         set mode "msgstr"
97                 } elseif {$line == ""} {
98                         flush_msg
99                 } elseif {[regexp "^\"(.*)\"$" $line dummy match]} {
100                         if {$mode == "msgid"} {
101                                 append msgid $match
102                         } elseif {$mode == "msgstr"} {
103                                 append msgstr $match
104                         } else {
105                                 puts stderr "I do not know what to do: $match"
106                         }
107                 } else {
108                         puts stderr "Cannot handle $line"
109                 }
110         }
111         flush_msg
112         close $in
113         close $out
114 }
115