1# Reading tcl/msgcat .msg files.
2# Copyright (C) 2002 Free Software Foundation, Inc.
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17namespace eval msgcat {
18  namespace export mcset mcdump
19  variable header ""
20}
21
22proc msgcat::puts_po_string {str} {
23  # Replace " with \"
24  regsub -all "\"" $str "\\\"" str
25  # Replace \ with \\
26  regsub -all "\\\\" $str "\\\\\\" str
27  # Replace newline with \n
28  regsub -all [subst "\n"] $str "\\n" str
29  regsub -all [subst "\a"] $str "\\a" str
30  regsub -all [subst "\b"] $str "\\b" str
31  regsub -all [subst "\f"] $str "\\f" str
32  regsub -all [subst "\r"] $str "\\r" str
33  regsub -all [subst "\t"] $str "\\t" str
34  regsub -all [subst "\v"] $str "\\v" str
35  # Output it.
36  puts -nonewline "\"$str\""
37}
38
39proc msgcat::write_po_message {msgid msgstr} {
40  puts -nonewline "msgid "
41  puts_po_string $msgid
42  puts ""
43  puts -nonewline "msgstr "
44  puts_po_string $msgstr
45  puts ""
46  puts ""
47}
48
49# This gets called once for each message in the .msg catalog.
50proc msgcat::mcset {locale src {dest ""}} {
51  msgcat::write_po_message $src $dest
52}
53
54# Main function.
55proc msgcat::mcdump {langfile} {
56  if {[file exists $langfile]} {
57    # msgunfmt expects the output in UTF-8 encoding.
58    fconfigure stdout -encoding utf-8
59
60    set msgcat::header ""
61
62    set fd [open $langfile r]
63    # In newer tcl versions, the .msg files are in UTF-8 encoding.
64    fconfigure $fd -encoding utf-8
65    eval [read $fd]
66    close $fd
67
68    if {$msgcat::header == ""} {
69      # Provide a minimal header.
70      set msgcat::header [subst "MIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\n"]
71    }
72    msgcat::write_po_message "" $msgcat::header
73  } else {
74    # Tell msgunfmt to emit an internationalized error message.
75    exit 2
76  }
77}
78
79# Main code: call the main function on the first and only argument.
80msgcat::mcdump [lindex $argv 0]
81
82exit 0
83