source: trunk/gsdl/bin/script/translate.pl@ 1469

Last change on this file since 1469 was 1469, checked in by sjboddie, 24 years ago

Added Potuguese interface (thanks to Eliel Carneiro de Oliveira at the
Payson Center).

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 10.7 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# translate.pl
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 1999 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
27
28# translate.pl takes a translated macro file (filename passed in on command
29# line) and generates any images required by it. Check out english.dm for
30# an example of the format translate.pl expects
31
32# translate.pl uses gimp to generate images so needs gimp installed and set
33# up for scripting with perl
34
35BEGIN {
36 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
37 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
38}
39
40use Gimp;
41use parsargv;
42use util;
43
44# these html entities will be translated correctly when occurring in
45# images. Any entities not in this list will not.
46my %rmap = ('auml' => chr (228),
47 'euml' => chr (235),
48 'iuml' => chr (239),
49 'ouml' => chr (246),
50 'uuml' => chr (252),
51 'Auml' => chr (196),
52 'Euml' => chr (203),
53 'Iuml' => chr (207),
54 'Ouml' => chr (214),
55 'Uuml' => chr (220),
56 'szlig' => chr (223),
57 'aacute' => chr (225),
58 'eacute' => chr (233),
59 'iacute' => chr (237),
60 'oacute' => chr (243),
61 'uacute' => chr (250),
62 'Aacute' => chr (193),
63 'Eacute' => chr (201),
64 'Iacute' => chr (205),
65 'Oacute' => chr (211),
66 'Uacute' => chr (218),
67 'agrave' => chr (224),
68 'egrave' => chr (232),
69 'igrave' => chr (236),
70 'ograve' => chr (242),
71 'ugrave' => chr (249),
72 'Agrave' => chr (192),
73 'Egrave' => chr (200),
74 'Igrave' => chr (204),
75 'Ograve' => chr (210),
76 'Ugrave' => chr (217),
77 'ntilde' => chr (241),
78 'Ntilde' => chr (209),
79 'atilde' => chr (227),
80 'Atilde' => chr (195),
81 'otilde' => chr (245),
82 'Otilde' => chr (213),
83 'ccedil' => chr (231),
84 'Ccedil' => chr (199),
85 'ecirc' => chr (234),
86 'Ecirc' => chr (202),
87 'acirc' => chr (226),
88 'Acirc' => chr (194),
89 );
90
91my $hand_made = 0;
92
93sub print_usage {
94 print STDERR "\n usage: $0 [options] macrofile\n\n";
95 print STDERR " options:\n";
96 print STDERR " -save_orig_file edited macrofile will be written to\n";
97 print STDERR " macrofile.new leaving macrofile unchanged\n";
98 print STDERR " -language_symbol ISO abbreviation of language (e.g. German=de,\n";
99 print STDERR " French=fr, Maori=mi)\n";
100 print STDERR " -image_dir directory full path to directory in which to create images\n";
101 print STDERR " (defaults to `pwd`/images)\n\n";
102}
103
104sub gsdl_translate {
105
106 if (!parsargv::parse(\@ARGV,
107 'save_orig_file', \$save_orig_file,
108 'language_symbol/[A-Za-z]{2}', \$language_symbol,
109 'image_dir/.*/images', \$image_dir)) {
110 &print_usage();
111 die "\n";
112 }
113
114 if ($image_dir eq "images") {
115 $image_dir = `pwd`;
116 chomp $image_dir;
117 $image_dir = &util::filename_cat ($image_dir, "images");
118 }
119
120 if (!defined $ARGV[0]) {
121 print STDERR "no macro file supplied\n\n";
122 &print_usage();
123 die "\n";
124 }
125 my $macrofile = $ARGV[0];
126 die "\nmacrofile $macrofile does not exist\n\n" unless -e $macrofile;
127
128 if (!-e $image_dir) {
129 mkdir ($image_dir, 511) || die "\ncouldn't create image_dir $image_dir\n\n";
130 }
131
132 open (INPUT, $macrofile) || die "\ncouldn't open $macrofile for reading\n\n";
133 open (OUTPUT, ">$macrofile.new") || die "\ncouldn't open temporary file $macrofile.new for writing\n\n";
134
135 &parse_file (INPUT, OUTPUT);
136
137 close OUTPUT;
138 close INPUT;
139
140 if (!$save_orig_file) {
141 `mv $macrofile.new $macrofile`;
142 }
143
144 print STDERR "\n\n";
145 print STDERR "translation of macro file $macrofile completed\n";
146 print STDERR "the translated macro file is $macrofile.new\n" if $save_orig_file;
147 print STDERR "\n";
148 if ($hand_made) {
149 print STDERR "$hand_made hand made images were found within $macrofile,\n";
150 print STDERR "these will need to be made by hand (grep $macrofile for 'hand_made'\n\n";
151 }
152 print STDERR "To add your new interface translation to Greenstone you'll need to:\n";
153 print STDERR " 1. Copy your new macro file to your GSDLHOME/macros directory\n";
154 print STDERR " 2. Add your new macro file to the macrofiles list in your\n";
155 print STDERR " GSDLHOME/etc/main.cfg configuration file\n";
156 print STDERR " 3. Copy your newly created images from $image_dir to \n";
157 print STDERR " GSDLHOME/images/$language_symbol/\n\n";
158 print STDERR "Access your new interface language by setting the language (l) cgi\n";
159 print STDERR "argument to '$language_symbol'\n\n";
160
161}
162
163sub parse_file {
164 my ($input, $output) = @_;
165
166 undef $/;
167 my $dmfile = <$input>;
168 $/ = "\n";
169
170 # process all the images
171
172 $dmfile =~ s/\n\#\#\s*\"([^\"]*)\"\s*\#\#\s*([^\s\#]*)\s*\#\#\s*([^\s\#]*)\s*\#\#(.*?)(?=(\n\#|\s*\Z))/&process_image ($1, $2, $3, $4)/esg;
173
174 # add language parameter to each macro
175 $dmfile =~ s/(\n\s*)(_[^_]*_)\s*(\[[^\]]*\])?\s*\{/$1 . &add_language_param ($2, $3)/esg;
176
177 print $output $dmfile;
178}
179
180sub process_image {
181 my ($text, $image_type, $image_name, $image_macros) = @_;
182
183 my $origtext = $text;
184 $text =~ s/&(\d{3,4});/chr($1)/ge;
185 $text =~ s/&([^;]*);/$rmap{$1}/g;
186
187 # edit image macros
188 $image_macros =~ s/(_httpimg_\/)(?:[^\/\}]*\/)?([^\}]*\.(?:gif|jpe?g|png))/$1$language_symbol\/$2/gs;
189
190 if ($image_type eq "top_nav_button") {
191
192 # generate images
193 my $options = "-text \"$text\" -filenamestem $image_name";
194 $options .= " -fontsize 12 -height 20 -whitespace -image_dir $image_dir";
195 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button.pl $options`;
196
197 # get width of new images and edit width macro
198 my $fullfilename = &util::filename_cat ($image_dir, "${image_name}on.gif");
199 &process_width_macro ($fullfilename, $image_name, \$image_macros);
200
201 } elsif ($image_type eq "nav_bar_button") {
202
203 # generate on and off images
204 my $options = "-text \"$text\" -filenamestem $image_name";
205 $options .= " -fontsize 17 -height 17 -fixed_width -width 87";
206 $options .= " -image_dir $image_dir";
207 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button.pl $options`;
208
209 # generate green image
210 $options = "-text \"$text\" -filename ${image_name}gr.gif";
211 $options .= " -image_dir $image_dir";
212 `$ENV{'GSDLHOME'}/bin/script/gimp/green_bar.pl $options`;
213
214 # get width of new images and edit width macro
215 my $fullfilename = &util::filename_cat ($image_dir, "${image_name}on.gif");
216 &process_width_macro ($fullfilename, $image_name, \$image_macros);
217
218 } elsif ($image_type eq "document_button") {
219
220 # generate on and off images
221 my $options = "-text \"$text\" -filenamestem $image_name";
222 $options .= " -fixed_width -whitespace -image_dir $image_dir";
223 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button.pl $options`;
224
225 # get width of new images and edit width macro
226 my $fullfilename = &util::filename_cat ($image_dir, "${image_name}on.gif");
227 &process_width_macro ($fullfilename, $image_name, \$image_macros);
228
229 } elsif ($image_type eq "green_bar_left_aligned") {
230
231 # generate green bar image (we're assuming these bars are always 537
232 # pixels and are never stretched by excess text
233 my $options = "-text \"$text\" -filename ${image_name}.gif -dont_center";
234 $options .= " -width 537 -width_space 15 -image_dir $image_dir";
235 `$ENV{'GSDLHOME'}/bin/script/gimp/green_bar.pl $options`;
236
237 } elsif ($image_type eq "green_title") {
238
239 # read the width if it is specified in $image_macros
240 my ($width) = $image_macros =~ /_width${image_name}x?_\s*[^\{]*\{(\d+)\}/;
241 $width = 200 unless ($width);
242
243 # generate green title image
244 my $options = "-text \"$text\" -filename ${image_name}.gif -image_dir $image_dir";
245 $options .= " -width $width -height 57 -stripe_alignment right -text_alignment right";
246 $options .= " -fontsize 26 -fontweight bold";
247 `$ENV{'GSDLHOME'}/bin/script/gimp/title_icon.pl $options`;
248
249 # get width of resulting image and edit _width..._ macro in $image_macros
250 # (no longer needed since we always resize to the width read from $image_macros.)
251 # my $fullfilename = &util::filename_cat ($image_dir, "${image_name}.gif");
252 # &process_width_macro ($fullfilename, $image_name, \$image_macros);
253
254 } elsif ($image_type eq "hand_made") {
255
256 $hand_made ++;
257
258 } else {
259
260 print STDERR "WARNING (translate.pl): unknown image type found ($image_type)\n";
261
262 }
263
264 return "\n\#\# \"$origtext\" \#\# $image_type \#\# $image_name \#\#$image_macros";
265}
266
267sub process_width_macro {
268 my ($filename, $image_name, $image_macros) = @_;
269
270 my $img_info = &get_img_info ($filename);
271 $$image_macros =~ s/(_width${image_name}x?_\s*(?:\[[^\]]*\])?\s*\{)(\d+)(\})/$1$img_info->{'width'}$3/s;
272}
273
274sub add_language_param {
275 my ($macroname, $paramlist) = @_;
276
277 my $first = 1;
278 if (defined $paramlist) {
279 $paramlist =~ s/^\[//;
280 $paramlist =~ s/\]$//;
281 my @params = split /\,/, $paramlist;
282 $paramlist = "";
283 foreach $param (@params) {
284 # remove any existing language parameter
285 if ($param !~ /^l=/) {
286 $paramlist .= "," unless $first;
287 $paramlist .= $param;
288 $first = 0;
289 }
290 }
291 }
292 $paramlist .= "," unless $first;
293 $paramlist .= "l=" . $language_symbol;
294 return "$macroname [$paramlist] {";
295}
296
297sub get_img_info {
298 my ($imagefile) = @_;
299 my %info = ();
300
301 if (!-r $imagefile) {
302 print STDERR "ERROR (translate.pl): couldn't open $imagefile to get dimensions\n";
303 $info{'width'} = 0;
304 $info{'height'} = 0;
305 } else {
306 my $image = gimp_file_load (RUN_NONINTERACTIVE, $imagefile, $imagefile);
307 $info{'width'} = gimp_image_width ($image);
308 $info{'height'} = gimp_image_height ($image);
309 }
310
311 return \%info;
312}
313
314sub query {
315
316 gimp_install_procedure("gsdl_translate", "translate macro files and create images",
317 "", "Stefan Boddie", "Stefan Boddie", "2000-03-14",
318 "<Toolbox>/Xtns/gsdl_translate", "*", &PROC_EXTENSION,
319 [[PARAM_INT32, "run_mode", "Interactive, [non-interactive]"]], []);
320}
321
322sub net {
323 gsdl_translate;
324}
325
326exit main;
Note: See TracBrowser for help on using the repository browser.