1#! /usr/bin/tclsh
2#
3#   Copyright (C) 1987-2015 by Jeffery P. Hansen
4#   Copyright (C) 2015-2018 by Andrey V. Skvortsov
5#
6#   This program is free software; you can redistribute it and/or modify
7#   it under the terms of the GNU General Public License as published by
8#   the Free Software Foundation; either version 2 of the License, or
9#   (at your option) any later version.
10#
11#   This program is distributed in the hope that it will be useful,
12#   but WITHOUT ANY WARRANTY; without even the implied warranty of
13#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14#   GNU General Public License for more details.
15#
16#   You should have received a copy of the GNU General Public License along
17#   with this program; if not, write to the Free Software Foundation, Inc.,
18#   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#
20########################################################################
21#
22# Parse string of the messages file
23# State machine with state variables currentKey and currentValue
24# \param line: text line to parse
25# \param sourceLine
26# \param msgHash: Hash id to use
27#
28proc parseString {line sourceLine msgHash} {
29
30	global messagesHash
31	global messagesLine2Key
32	global currentKey
33	global currentValue
34
35	# New key found
36	if {[string equal $currentKey ""]} {
37		set start [string first "\t" $line]
38		set stop [string last "\t" $line]
39		if {$start > 0} {
40			set currentKey [string range $line 0 [expr {$start-1}]]
41			lappend messagesLine2Key($msgHash) [list $sourceLine $currentKey]
42			set value [string trim [string range $line $stop [string length $line]]]
43			if {([string equal $value "-fillbegin-"] == 0) &&
44			    ([string equal $value "-begin-"] == 0)} {
45				set currentValue $value
46				set messagesHash($currentKey,$msgHash) $currentValue
47				set currentKey ""
48			} else {
49				set currentValue ""
50			}
51		}
52	# Next line of the current value
53	} else {
54		if {[string equal $line "-end-"]} {
55			set messagesHash($currentKey,$msgHash) $currentValue
56			set currentKey ""
57			set currentValue ""
58		} else {
59			if {[string length $currentValue] > 0} {
60				set currentValue "$currentValue\n"
61			}
62			set currentValue "$currentValue$line"
63		}
64	}
65}
66
67proc parseFile {msgFile msgHash} {
68	set sourceLine 1
69
70	while {1} {
71		set line [gets $msgFile]
72		if {[eof $msgFile]} {
73			close $msgFile
74			break
75		}
76		# trim line and reject of the comment and service lines
77		set line [string trim $line]
78		if {([string index $line 0] != "#") &&
79		    ([string index $line 0] != "@") &&
80		    ([string index $line 0] != "\\") &&
81		    ([string length $line] > 0)} {
82			parseString $line $sourceLine $msgHash
83		}
84		incr sourceLine
85	}
86}
87
88global messagesHash
89global messagesLine2Key
90global currentKey
91global currentValue
92global options_m
93global options_pot
94
95set options_m "en/messages"
96set options_pot "messages.pot"
97
98set currentKey ""
99set currentValue ""
100
101# try to open original (english) messages file
102set f [open $options_m r]
103puts "Messages file '$options_m' opened"
104
105# try to open template output file
106set f0 [open $options_pot w]
107puts "Gettext template file '$options_pot' opened"
108
109parseFile $f m
110
111puts $f0 "# This file is part of TkGate project"
112puts $f0 "# Copyright (C) 1987-2015 by Jeffery P. Hansen"
113puts $f0 "# Copyright (C) 2015-2018 by Andrey V. Skvortsov"
114puts $f0 "# This file is distributed under the same license as the TkGate package."
115puts $f0 "#"
116puts $f0 "# ##############################################################################"
117puts $f0 "# Generated by messages2po script"
118puts $f0 "# ##############################################################################"
119puts $f0 "#"
120puts $f0 "#, fuzzy"
121puts $f0 "msgid \"\""
122puts $f0 "msgstr \"\""
123puts $f0 "\"Project-Id-Version: tkgate\\n\""
124puts $f0 "\"Report-Msgid-Bugs-To: \\n\""
125puts $f0 "\"POT-Creation-Date: [clock format [clock seconds] -format {%Y-%m-%d %H:%M}]\+0300\\n\""
126puts $f0 "\"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n\""
127puts $f0 "\"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n\""
128puts $f0 "\"Language-Team: LANGUAGE <LL@li.org>\\n\""
129puts $f0 "\"Language: \\n\""
130puts $f0 "\"MIME-Version: 1.0\\n\""
131puts $f0 "\"Content-Type: text/plain; charset=UTF-8\\n\""
132puts $f0 "\"Content-Transfer-Encoding: 8bit\\n\""
133
134foreach pair $messagesLine2Key(m) {
135	set str [lindex $pair 0]
136	set keyName [lindex $pair 1]
137	set msgVal $messagesHash($keyName,m)
138	regsub -all "\"" $msgVal "\\\"" msgVal
139	if {([string index $msgVal 0] != "`") &&
140	    ([string equal $msgVal "-empty-"] == 0)} {
141		puts $f0 ""
142		puts $f0 "#: $options_m:$str"
143		puts $f0 "msgctxt \"$keyName\""
144		set line "msgid "
145		set strList [split $msgVal "\n"]
146		if {[llength $strList] > 1} {
147			set line "$line\"\"\n\"[join $strList \\n\"\n\"]\""
148		} else {
149			set line "$line\"[lindex $strList 0]\""
150		}
151		puts $f0 $line
152		puts $f0 "msgstr \"\""
153    }
154}
155
156close $f0
157