File: xml-i18n-merge.in

package info (click to toggle)
glade-nonus 0.6.4-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 12,152 kB
  • ctags: 3,879
  • sloc: ansic: 54,332; sh: 6,839; makefile: 743; sed: 93
file content (459 lines) | stat: -rw-r--r-- 11,229 bytes parent folder | download | duplicates (10)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#!@XML_I18N_TOOLS_PERL@ -w

#
#  The XML Translation Merge Tool
#
#  Copyright (C) 2000 Free Software Foundation.
#  Copyright (C) 2000, 2001 Eazel, Inc
#
#  This library 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 script 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 library; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#  Authors:  Maciej Stachowiak <mjs@eazel.com>
#            Kenneth Christiansen <kenneth@gnu.org>
#            Darin Adler <darin@eazel.com>
#


## Release information
my $PROGRAM      = "xml-i18n-merge";
my $PACKAGE      = "xml-i18n-tools";
my $VERSION      = "0.9";

## Script options - Enable by setting value to 1
my $ENABLE_XML   = "1";

## Loaded modules
use strict; 
use File::Basename;
use Getopt::Long;

## Scalars used by the option stuff
my $HELP_ARG 	= "0";
my $VERSION_ARG = "0";
my $OAF_STYLE_ARG = "0";
my $XML_STYLE_ARG = "0";
my $KEYS_STYLE_ARG = "0";
my $DESKTOP_STYLE_ARG = "0";
my $QUIET_ARG = "0";


## Handle options
GetOptions (
	    "help|h|?" => \$HELP_ARG,
	    "version|v" => \$VERSION_ARG,
            "quiet|q" => \$QUIET_ARG,
	    "oaf-style|o" => \$OAF_STYLE_ARG,
	    "xml-style|x" => \$XML_STYLE_ARG,
	    "keys-style|k" => \$KEYS_STYLE_ARG,
	    "desktop-style|d" => \$DESKTOP_STYLE_ARG
	    ) or &error;


my $PO_DIR;
my $FILE;
my $OUTFILE;

my @languages;
my %po_files_by_lang = ();
my %translations = ();

&split_on_argument;


## Check for options. 
## This section will check for the different options.

sub split_on_argument {

    if ($VERSION_ARG) {
	&version;

    } elsif ($HELP_ARG) {
	&help;
    } elsif ($OAF_STYLE_ARG && @ARGV > 2) {
	&place_normal;
	&message;
	&preparation;
	&oaf_merge_translations;
    } elsif ($XML_STYLE_ARG && @ARGV > 2) {
	&place_normal;
	&message;
	&preparation;
	&xml_merge_translations;
    } elsif ($KEYS_STYLE_ARG && @ARGV > 2) {
        &place_normal;
        &message;
        &preparation;
        &keys_merge_translations;
    } elsif ($DESKTOP_STYLE_ARG && @ARGV > 2) {
        &place_normal;
        &message;
        &preparation;
        &desktop_merge_translations;
    } else {
	&help;
    }  
}    


sub place_normal {
    $PO_DIR = $ARGV[0];
    $FILE = $ARGV[1];
    $OUTFILE = $ARGV[2];
}   


## Sub for printing release information
sub version{
    print "${PROGRAM} (${PACKAGE}) ${VERSION}\n";
    print "Written by Maciej Stachowiak and Kenneth Christiansen, 2000.\n\n";
    print "Copyright (C) 2000 Free Software Foundation, Inc.\n";
    print "Copyright (C) 2000, 2001 Eazel, Inc.\n";
    print "This is free software; see the source for copying conditions.  There is NO\n";
    print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
    exit;
}

## Sub for printing usage information
sub help{
    print "Usage: ${PROGRAM} [OPTIONS] PO_DIRECTORY FILENAME OUTPUT_FILE\n";
    print "Generates an xml file that includes translated versions of some attributes,\n";
    print "from an untranslated source and a po directory that includes translations.\n";
    print "  -v, --version                shows the version\n";
    print "  -h, --help                   shows this help page\n";
    print "  -q, --quiet                  quiet mode\n";
    print "  -o, --oaf-style              includes translations in the oaf style\n";
    print "  -x, --xml-style              includes translations in the xml style\n";
    print "  -k, --keys-style		  includes translations in the keys style\n";
    print "  -d, --desktop-style          includes translations in the desktop style\n";
    print "\nReport bugs to <mjs\@eazel.com>.\n";
    exit;
}


## Sub for printing error messages
sub error{
#   print "xml-i18n-merge: invalid option @ARGV\n";
    print "Try `${PROGRAM} --help' for more information.\n";
    exit;
}


sub message {
    print "Merging translations into $OUTFILE.\n" unless $QUIET_ARG;
}



sub preparation {
   &gather_po_files;
   &create_translation_database;   
}



# General-purpose code for looking up translations in .po files

sub gather_po_files
{
    my @po_files = glob("${PO_DIR}/*.po");

    @languages = map (&po_file2lang, @po_files);

    foreach my $lang (@languages) {
	$po_files_by_lang{$lang} = shift (@po_files);
    }
}

sub po_file2lang 
{ 
    my $tmp = $_; 
    $tmp =~ s/^.*\/(.*)\.po$/$1/; 
    return $tmp; 
}


sub create_translation_database
{
    foreach my $lang (@languages) {

    	my $po_file = $po_files_by_lang{$lang};

    	open PO_FILE, "<$po_file";	

        while (<PO_FILE>) {
            if (/^#,.*fuzzy/) {
                $_ = <PO_FILE>; next;
            }
            if (/^msgid "(.*)"/ ) {
		my $msgid = $1;
                $_ = <PO_FILE>;
		
		if (/^msgstr "(.+)"/) {
		    my $msgstr = $1;
		    $translations{$lang . "|" . $msgid} = $msgstr; 
		    # print "[$lang]$msgstr\n";
		}
	    }            
        }
    }
}

sub lookup_translations 
{
    my ($value) = @_;
 
    my %transmap = ();

    foreach my $lang (@languages) {
        my $translation = lookup_translation ($value, $lang);
            
        if ($translation) {
            $transmap{$lang} = $translation;
        }
    }

    return %transmap;
}


sub lookup_translation
{
    my ($string, $lang) = @_;
    $string =~ s/\+/\\+/g;
  
    my $salt = "$lang|$string";
      
    if ($translations{$salt}) {
        return $translations{$salt};
    }
  
    return "";
}


sub entity_encode_translations
{
    my %transmap = @_;

    foreach my $key (keys %transmap) {
	$transmap{$key} = entity_encode ($transmap{$key});
    }

    return %transmap;
}


sub entity_encode
{
    my ($pre_encoded) = @_;

    $pre_encoded =~ s/\\(.)/$1/g;
    my @list_of_chars = unpack ('C*', $pre_encoded);

    return join ('', map (&entity_encode_int, @list_of_chars));
}

sub entity_encode_int
{
    if ($_ > 127 || $_ == 34 || $_ == 38) {
	return "&#" . $_ . ";";
    } else {
	return chr $_;
    }
}


## XML/OAF-specific merge code
 
sub oaf_merge_translations
{
    my $xml_source; {
       local (*INPUT);
       local $/; # slurp mode
       open INPUT, "<$FILE" or die "can't open $FILE: $!";
       $xml_source = <INPUT>;
       close INPUT;
    }

    open OUTPUT, ">$OUTFILE";

    while ($xml_source =~ /[ \t]*<[^<]*\s_\w+="[^"]*"[^<]*>/m) {
        print OUTPUT $`;
        my $orig_node = $&;
	$xml_source = $';

        my $non_translated_line = $orig_node;
        $non_translated_line =~ s/_(\w+)="/$1="/;
            
        my $new_node = $non_translated_line;
            
        my $value_str = $orig_node;
        $value_str =~ s/.*_\w+="([^"]*)".*/$1/s;

        if ($value_str) {
            my %value_translation_map = entity_encode_translations
                (lookup_translations ($value_str));

            foreach my $key (sort keys %value_translation_map) {
                my $translation = $value_translation_map{$key};
                    
                my $translated_line = $orig_node;
                $translated_line =~ s/name="([^"]*)"/name="$1-$key"/;
                $translated_line =~ s/(\s*)_(\w+)="[^"]*"/$1$2="$translation"/;

                $new_node .= "\n$translated_line";
            }
        }

	$xml_source = $new_node . $xml_source;
    }

    print OUTPUT $xml_source;

    close OUTPUT;
}


## XML (non-OAF) merge code
 
sub xml_merge_translations
{
    my $xml_source; {
       local (*INPUT);
       local $/; # slurp mode
       open INPUT, "<$FILE" or die "can't open $FILE: $!";
       $xml_source = <INPUT>;
       close INPUT;
    }

    open OUTPUT, ">$OUTFILE";

    # FIXME: support attribute translations

    # First just unmark for translation all empty nodes
    # for example <_foo/> is just replaced by <foo/>
    $xml_source =~ s/<_(\w+)\/>/<$1\/>/mg;

    # Support for XML <_foo>blah</_foo> style translations
    while ($xml_source =~ /([ \t]*)<_(\w+)>([^<]+)<\/_\2>/m) {
        print OUTPUT $`;
	$xml_source = $';

        my $spaces = $1;
        my $tag_name = $2;
        my $value_str = $3;

        my $non_translated_line = "$spaces<$tag_name>$value_str</$tag_name>";
            
        my $new_node = $non_translated_line;

        if ($value_str) {
            my %value_translation_map = entity_encode_translations
                (lookup_translations ($value_str));

            foreach my $key (sort keys %value_translation_map) {
                my $translation = $value_translation_map{$key};

                $new_node .= "\n$spaces<$tag_name xml:lang=\"$key\">$translation</$tag_name>";
            }
        }

	$xml_source = $new_node . $xml_source;
    }

    print OUTPUT $xml_source;

    close OUTPUT;
}

sub keys_merge_translations
{       
    open INPUT, "<${FILE}";

    open OUTPUT, ">${OUTFILE}";

    while (<INPUT>) {
        chomp;
        if (/^\s*_\w+=.*/)  {
            my $orig_line = $_;
    
            my $non_translated_line = $orig_line;
            $non_translated_line =~ s/_([^="]*)=/$1=/;
            
            print OUTPUT "${non_translated_line}\n";
            
            my $value_str = $orig_line;
            $value_str =~ s/.*_\w+=(.*)/$1/;
            
            if ($value_str) {
                my %value_translation_map = lookup_translations ($value_str);
            
                foreach my $key (sort keys %value_translation_map) {
                    my $translation = $value_translation_map{$key};

                    my $translated_line = $orig_line;  
                    $translated_line =~ s/_([^="]*)=([^\n]*)/\[$key]$1=$translation/;
                    print OUTPUT "$translated_line\n";
                }
            }
        } else {
            print OUTPUT "$_\n";
        }
    }
                 
    close OUTPUT;
    close INPUT;
}

sub desktop_merge_translations
{
    open INPUT, "<${FILE}";

    open OUTPUT, ">${OUTFILE}";

    while (<INPUT>) {
        chomp;
        if (/^\s*_\w+=.*/)  {
            my $orig_line = $_;

            my $non_translated_line = $orig_line;
            $non_translated_line =~ s/_([^="]*)=/$1=/;

            print OUTPUT "${non_translated_line}\n";

            my $value_str = $orig_line;
            $value_str =~ s/.*_\w+=(.*)/$1/;

            if ($value_str) {
                my %value_translation_map = lookup_translations ($value_str);

                foreach my $key (sort keys %value_translation_map) {
                    my $translation = $value_translation_map{$key};

                    my $translated_line = $orig_line;
                    $translated_line =~ s/^_([^="]*)=([^\n]*)/$1\[$key]=$translation/;
                    print OUTPUT "$translated_line\n";
                }
            }
        } else {
            print OUTPUT "$_\n";
        }
    }

    close OUTPUT;
    close INPUT;

}