48a2669967c77980f7953c886203e5deee96289b
1#!/bin/sh
2# Tcl ignores the next line -*- tcl -*- \
3exec 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
9proc 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
26set output_directory "."
27set lang "dummy"
28set files [list]
29
30# parse options
31for {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
50proc 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 set prefix "::msgcat::mcset $lang \"[u2a $msgid]\""
66 }
67
68 puts $out "$prefix \"[u2a $msgstr]\""
69}
70
71set fuzzy 0
72foreach file $files {
73 regsub "^.*/\(\[^/\]*\)\.po$" $file "$output_directory\\1.msg" outfile
74 set in [open $file "r"]
75 fconfigure $in -encoding utf-8
76 set out [open $outfile "w"]
77
78 set mode ""
79 while {[gets $in line] >= 0} {
80 if {[regexp "^#" $line]} {
81 if {[regexp ", fuzzy" $line]} {
82 set fuzzy 1
83 } else {
84 flush_msg
85 }
86 continue
87 } elseif {[regexp "^msgid \"(.*)\"$" $line dummy match]} {
88 flush_msg
89 set msgid $match
90 set mode "msgid"
91 } elseif {[regexp "^msgstr \"(.*)\"$" $line dummy match]} {
92 set msgstr $match
93 set mode "msgstr"
94 } elseif {$line == ""} {
95 flush_msg
96 } elseif {[regexp "^\"(.*)\"$" $line dummy match]} {
97 if {$mode == "msgid"} {
98 append msgid $match
99 } elseif {$mode == "msgstr"} {
100 append msgstr $match
101 } else {
102 puts stderr "I do not know what to do: $match"
103 }
104 } else {
105 puts stderr "Cannot handle $line"
106 }
107 }
108 flush_msg
109 close $in
110 close $out
111}
112