forked from zdia/gorilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcset2mcmset.tcl
executable file
·132 lines (99 loc) · 3.66 KB
/
mcset2mcmset.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#! /bin/sh
# the next line restarts using wish \
exec tclsh8.5 "$0" ${1+"$@"}
# Convert the output of GNU gettext msgfmt from plural calls to
# ::msgcat::mcset into a single call to ::msgcat::mcmset
#
# Copyright (c) 2011 by Richard Ellis
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation 51
# Franklin Street, Suite 500 Boston, MA 02110-1335
# A copy of the GNU GPL may be found in the LICENCE.txt file in the main
# gorilla/sources directory.
# check command line parameters
if { [ llength $argv ] == 0 } {
puts stderr "This utility should be called with a list of filenames as command line parameters."
exit 1
}
foreach filename $argv {
if { ( ! [ file exists $filename ] )
&& ( ! [ file readable $filename ] ) } {
lappend error_files $filename
}
}
if { [ info exists error_files ] } {
puts stderr "The following files were not found or are unreadable\n [ join $error_files "\n " ]"
exit 1
}
# now the converstion system #
proc open.utf-8 { filename {access ""} {mode ""} } {
# encapsulate adjustment of the file character set encoding
set params [ list $filename ]
if { $access ne "" } {
lappend params $access
}
if { $mode ne "" } {
lappend params $mode
}
set fd [ open {*}$params ]
fconfigure $fd -encoding utf-8
return $fd
} ; # end proc open.utf-8
# fake msgcat proc that makes the magic work
namespace eval ::msgcat {
variable msgdata
proc mcset { lang fromstr tostr } {
variable msgdata
dict lappend msgdata $lang $fromstr $tostr
}
} ; # end namespace eval ::msgcat
# now read each file in and output a converted file
# converted output files will be named for the original filename with
# ".conv" appended. Any existing ".conv" files will be silently overwritten
# Usually a single msg file will contain translations for a single language.
# But this converter will convert and group plural language translations
# that might exist in a single input file.
foreach filename $argv {
# the input half of the loop body
# initialize for each new input file
set ::msgcat::msgdata [ dict create ]
unset -nocomplain ::msgcat::header
set fd [ open.utf-8 $filename {RDONLY} ]
# let the Tcl parser to the real work of parsing the input file
eval [ read $fd ]
close $fd
# the output half of the loop body
set fd [ open.utf-8 ${filename}.conv {WRONLY CREAT TRUNC} ]
puts $fd {#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by utilities/rebuild-msg-files using the
# *.po files as templates from utilities/gettext and /utilities/help2po
# directories. Modify those files instead and then re-run the
# rebuild-msg-files script.
#
#}
if { [ info exists ::msgcat::header ] } {
puts $fd "# [ join [ split [ string trim $::msgcat::header ] "\n" ] "\n# " ]\n"
}
# The loop below outputs a separate "mcmset lang { ... }" group for each
# unique input language in the input file.
dict for {key value} $::msgcat::msgdata {
puts $fd "mcmset $key \{"
foreach {to from} $value {
puts $fd "[ list $to ] [ list $from ]"
}
puts $fd "\}"
}
close $fd
} ; # end foreach filename $argv