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 2, or (at your option)
7# 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, write to the Free Software Foundation,
16# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18namespace eval msgcat {
19  namespace export mcset mcdump
20  variable header ""
21}
22
23proc msgcat::puts_po_string {str} {
24  # Replace " with \"
25  regsub -all "\"" $str "\\\"" str
26  # Replace \ with \\
27  regsub -all "\\\\" $str "\\\\\\" str
28  # Replace newline with \n
29  regsub -all [subst "\n"] $str "\\n" str
30  regsub -all [subst "\a"] $str "\\a" str
31  regsub -all [subst "\b"] $str "\\b" str
32  regsub -all [subst "\f"] $str "\\f" str
33  regsub -all [subst "\r"] $str "\\r" str
34  regsub -all [subst "\t"] $str "\\t" str
35  regsub -all [subst "\v"] $str "\\v" str
36  # Output it.
37  puts -nonewline "\"$str\""
38}
39
40proc msgcat::write_po_message {msgid msgstr} {
41  puts -nonewline "msgid "
42  puts_po_string $msgid
43  puts ""
44  puts -nonewline "msgstr "
45  puts_po_string $msgstr
46  puts ""
47  puts ""
48}
49
50# This gets called once for each message in the .msg catalog.
51proc msgcat::mcset {locale src {dest ""}} {
52  msgcat::write_po_message $src $dest
53}
54
55# Main function.
56proc msgcat::mcdump {langfile} {
57  if {[file exists $langfile]} {
58    # msgunfmt expects the output in UTF-8 encoding.
59    fconfigure stdout -encoding utf-8
60
61    set msgcat::header ""
62
63    set fd [open $langfile r]
64    # In newer tcl versions, the .msg files are in UTF-8 encoding.
65    fconfigure $fd -encoding utf-8
66    eval [read $fd]
67    close $fd
68
69    if {$msgcat::header == ""} {
70      # Provide a minimal header.
71      set msgcat::header [subst "MIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\n"]
72    }
73    msgcat::write_po_message "" $msgcat::header
74  } else {
75    # Tell msgunfmt to emit an internationalized error message.
76    exit 2
77  }
78}
79
80# Main code: call the main function on the first and only argument.
81msgcat::mcdump [lindex $argv 0]
82
83exit 0
84