da0765dd8020af359c1f47e8f2ae2e4e72a9ce5b
   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
  52
  53        if {![info exists msgid] || $mode == ""} {
  54                return
  55        }
  56        set mode ""
  57
  58        if {$msgid == ""} {
  59                set prefix "set ::msgcat::header"
  60        } else {
  61                set prefix "::msgcat::mcset $lang \"[u2a $msgid]\""
  62        }
  63
  64        puts $out "$prefix \"[u2a $msgstr]\""
  65}
  66
  67foreach file $files {
  68        regsub "^.*/\(\[^/\]*\)\.po$" $file "$output_directory\\1.msg" outfile
  69        set in [open $file "r"]
  70        fconfigure $in -encoding utf-8
  71        set out [open $outfile "w"]
  72
  73        set mode ""
  74        while {[gets $in line] >= 0} {
  75                if {[regexp "^#" $line]} {
  76                        flush_msg
  77                        continue
  78                } elseif {[regexp "^msgid \"(.*)\"$" $line dummy match]} {
  79                        flush_msg
  80                        set msgid $match
  81                        set mode "msgid"
  82                } elseif {[regexp "^msgstr \"(.*)\"$" $line dummy match]} {
  83                        set msgstr $match
  84                        set mode "msgstr"
  85                } elseif {$line == ""} {
  86                        flush_msg
  87                } elseif {[regexp "^\"(.*)\"$" $line dummy match]} {
  88                        if {$mode == "msgid"} {
  89                                append msgid $match
  90                        } elseif {$mode == "msgstr"} {
  91                                append msgstr $match
  92                        } else {
  93                                puts stderr "I do not know what to do: $match"
  94                        }
  95                } else {
  96                        puts stderr "Cannot handle $line"
  97                }
  98        }
  99        flush_msg
 100        close $in
 101        close $out
 102}
 103