-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathTransTractor.pm
1386 lines (1059 loc) · 42 KB
/
TransTractor.pm
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
require Exporter;
package Locale::Po4a::TransTractor;
use DynaLoader;
use 5.006;
use strict;
use warnings;
use subs qw(makespace);
use vars qw($VERSION @ISA @EXPORT);
$VERSION = "0.69-alpha";
@ISA = qw(DynaLoader);
@EXPORT = qw(new process translate
read write readpo writepo
getpoout setpoout get_out_charset handle_yaml);
# Try to use a C extension if present.
eval("bootstrap Locale::Po4a::TransTractor $VERSION");
use Carp qw(croak confess);
use Locale::Po4a::Po;
use Locale::Po4a::Common;
use File::Path; # mkdir before write
use Encode;
use Encode::Guess;
use File::Spec;
=encoding UTF-8
=head1 NAME
Locale::Po4a::TransTractor - generic trans(lator ex)tractor.
=head1 DESCRIPTION
The po4a (PO for anything) project goal is to ease translations (and more
interestingly, the maintenance of translations) using gettext tools on
areas where they were not expected like documentation.
This class is the ancestor of every po4a parser used to parse a document, to
search translatable strings, to extract them to a PO file and to replace them by
their translation in the output document.
More formally, it takes the following arguments as input:
=over 2
=item -
a document to translate;
=item -
a PO file containing the translations to use.
=back
As output, it produces:
=over 2
=item -
another PO file, resulting of the extraction of translatable strings from
the input document;
=item -
a translated document, with the same structure than the one in input, but
with all translatable strings replaced with the translations found in the
PO file provided in input.
=back
Here is a graphical representation of this:
Input document --\ /---> Output document
\ / (translated)
+-> parse() function -----+
/ \
Input PO --------/ \---> Output PO
(extracted)
=head1 FUNCTIONS YOUR PARSER SHOULD OVERRIDE
=over 4
=item parse()
This is where all the work takes place: the parsing of input documents, the
generation of output, and the extraction of the translatable strings. This
is pretty simple using the provided functions presented in the section
B<INTERNAL FUNCTIONS> below. See also the B<SYNOPSIS>, which presents an
example.
This function is called by the process() function below, but if you choose
to use the new() function, and to add content manually to your document,
you will have to call this function yourself.
=item docheader()
This function returns the header we should add to the produced document,
quoted properly to be a comment in the target language. See the section
B<Educating developers about translations>, from L<po4a(7)|po4a.7>, for what
it is good for.
=back
=cut
sub docheader { }
sub parse { }
=head1 SYNOPSIS
The following example parses a list of paragraphs beginning with "<p>". For the sake
of simplicity, we assume that the document is well formatted, i.e. that '<p>'
tags are the only tags present, and that this tag is at the very beginning
of each paragraph.
sub parse {
my $self = shift;
PARAGRAPH: while (1) {
my ($paragraph,$pararef)=("","");
my $first=1;
my ($line,$lref)=$self->shiftline();
while (defined($line)) {
if ($line =~ m/<p>/ && !$first--; ) {
# Not the first time we see <p>.
# Reput the current line in input,
# and put the built paragraph to output
$self->unshiftline($line,$lref);
# Now that the document is formed, translate it:
# - Remove the leading tag
$paragraph =~ s/^<p>//s;
# - push to output the leading tag (untranslated) and the
# rest of the paragraph (translated)
$self->pushline( "<p>"
. $self->translate($paragraph,$pararef)
);
next PARAGRAPH;
} else {
# Append to the paragraph
$paragraph .= $line;
$pararef = $lref unless(length($pararef));
}
# Reinit the loop
($line,$lref)=$self->shiftline();
}
# Did not get a defined line? End of input file.
return;
}
}
Once you've implemented the parse function, you can use your document
class, using the public interface presented in the next section.
=head1 PUBLIC INTERFACE for scripts using your parser
=head2 Constructor
=over 4
=item process(%)
This function can do all you need to do with a po4a document in one
invocation. Its arguments must be packed as a hash. ACTIONS:
=over 3
=item a.
Reads all the PO files specified in po_in_name
=item b.
Reads all original documents specified in file_in_name
=item c.
Parses the document
=item d.
Reads and applies all the addenda specified
=item e.
Writes the translated document to file_out_name (if given)
=item f.
Writes the extracted PO file to po_out_name (if given)
=back
ARGUMENTS, beside the ones accepted by new() (with expected type):
=over 4
=item file_in_name (@)
List of filenames where we should read the input document.
=item file_in_charset ($)
Charset used in the input document (if it isn't specified, it will try
to detect it from the input document).
=item file_out_name ($)
Filename where we should write the output document.
=item file_out_charset ($)
Charset used in the output document (if it isn't specified, it will use
the PO file charset).
=item po_in_name (@)
List of filenames where we should read the input PO files from, containing
the translation which will be used to translate the document.
=item po_out_name ($)
Filename where we should write the output PO file, containing the strings
extracted from the input document.
=item addendum (@)
List of filenames where we should read the addenda from.
=item addendum_charset ($)
Charset for the addenda.
=back
=item new(%)
Create a new po4a document. Accepted options (in the hash passed as a parameter):
=over 4
=item verbose ($)
Sets the verbosity.
=item debug ($)
Sets the debugging.
=back
=cut
sub process {
## Determine if we were called via an object-ref or a classname
my $self = shift;
## Any remaining arguments are treated as initial values for the
## hash that is used to represent this object.
my %params = @_;
# Build the args for new()
my %newparams = ();
foreach ( keys %params ) {
next
if ( $_ eq 'po_in_name'
|| $_ eq 'po_out_name'
|| $_ eq 'file_in_name'
|| $_ eq 'file_in_charset'
|| $_ eq 'file_out_name'
|| $_ eq 'file_out_charset'
|| $_ eq 'addendum'
|| $_ eq 'addendum_charset' );
$newparams{$_} = $params{$_};
}
$self->detected_charset( $params{'file_in_charset'} ) if defined $params{'file_in_charset'};
$self->{TT}{'file_out_charset'} = $params{'file_out_charset'};
if ( length( $self->{TT}{'file_out_charset'} ) ) {
$self->{TT}{'file_out_encoder'} = find_encoding( $self->{TT}{'file_out_charset'} );
}
$self->{TT}{'addendum_charset'} = $params{'addendum_charset'};
our ( $destdir, $srcdir, $calldir ) = ( $params{'destdir'}, $params{'srcdir'}, $params{'calldir'} );
sub _input_file {
my $filename = $_[0];
return $filename if ( File::Spec->file_name_is_absolute($filename) );
foreach ( ( $destdir, $srcdir, $calldir ) ) {
next unless defined $_;
my $p = File::Spec->catfile( $_, $filename );
return $p if -e $p;
}
return $filename;
}
sub _output_file {
my $filename = $_[0];
return $filename if ( File::Spec->file_name_is_absolute($filename) );
foreach ( ( $destdir, $calldir ) ) {
next unless defined $_;
return File::Spec->catfile( $_, $filename ) if -d $_ and -w $_;
}
return $filename;
}
foreach my $file ( @{ $params{'po_in_name'} } ) {
my $infile = _input_file($file);
print STDERR wrap_mod( "po4a::transtractor::process", "Read PO file $infile" )
if $self->debug();
$self->readpo($infile);
}
foreach my $file ( @{ $params{'file_in_name'} } ) {
my $infile = _input_file($file);
print STDERR wrap_mod( "po4a::transtractor::process", "Read document $infile" )
if $self->debug();
$self->read( $infile, $file );
}
print STDERR wrap_mod( "po4a::transtractor::process", "Call parse()" ) if $self->debug();
$self->parse();
print STDERR wrap_mod( "po4a::transtractor::process", "Done parse()" ) if $self->debug();
foreach my $file ( @{ $params{'addendum'} } ) {
my $infile = _input_file($file);
print STDERR wrap_mod( "po4a::transtractor::process", "Apply addendum $infile" )
if $self->debug();
$self->addendum($file) || die "An addendum failed\n";
}
if ( defined $params{'file_out_name'} ) {
my $outfile = _output_file( $params{'file_out_name'} );
print STDERR wrap_mod( "po4a::transtractor::process", "Write document $outfile" )
if $self->debug();
$self->write($outfile);
}
if ( defined $params{'po_out_name'} ) {
my $outfile = _output_file( $params{'po_out_name'} );
print STDERR wrap_mod( "po4a::transtractor::process", "Write PO file $outfile" )
if $self->debug();
$self->writepo($outfile);
}
return $self;
}
sub new {
## Determine if we were called via an object-ref or a classname
my $this = shift;
my $class = ref($this) || $this;
my $self = {};
my %options = @_;
## Bless ourselves into the desired class and perform any initialization
bless $self, $class;
## initialize the plugin
# prevent the plugin from croaking on the options intended for Po.pm
$self->{options}{'porefs'} = '';
$self->{options}{'copyright-holder'} = '';
$self->{options}{'msgid-bugs-address'} = '';
$self->{options}{'package-name'} = '';
$self->{options}{'package-version'} = '';
$self->{options}{'wrap-po'} = '';
# let the plugin parse the options and such
$self->initialize(%options);
## Create our private data
my %po_options;
$po_options{'porefs'} = $options{'porefs'};
$po_options{'copyright-holder'} = $options{'copyright-holder'};
$po_options{'msgid-bugs-address'} = $options{'msgid-bugs-address'};
$po_options{'package-name'} = $options{'package-name'};
$po_options{'package-version'} = $options{'package-version'};
$po_options{'wrap-po'} = $options{'wrap-po'};
# private data
$self->{TT} = ();
$self->{TT}{po_in} = Locale::Po4a::Po->new( \%po_options );
$self->{TT}{po_out} = Locale::Po4a::Po->new( \%po_options );
# Warning, $self->{TT}{doc_in} is an array of array:
# The document is split on lines, and for each array in array
# [0] is the line content, [1] is the reference $filename:$linenum
$self->{TT}{doc_in} = ();
$self->{TT}{doc_out} = ();
if ( defined $options{'verbose'} ) {
$self->{TT}{verbose} = $options{'verbose'};
}
if ( defined $options{'debug'} ) {
$self->{TT}{debug} = $options{'debug'};
}
# Input document is in ascii until we prove the opposite (in read())
$self->{TT}{ascii_input} = 1;
# We try not to use utf unless it's forced from the outside (in case the
# document isn't in ascii)
$self->{TT}{utf_mode} = 0;
return $self;
}
=back
=head2 Manipulating document files
=over 4
=item read($$)
Add another input document data at the end of the existing array
C<< @{$self->{TT}{doc_in}} >>. The argument is the filename to read. If a second
argument is provided, it is the filename to use in the references.
This array C<< @{$self->{TT}{doc_in}} >> holds this input document data as an
array of strings with alternating meanings.
* The string C<$textline> holding each line of the input text data.
* The string C<< $filename:$linenum >> holding its location and called as
"reference" (C<linenum> starts with 1).
Please note that it does not parse anything. You should use the parse()
function when you're done with packing input files into the document.
=cut
sub read() {
my $self = shift;
my ( $filename, $refname ) = ( shift, shift );
confess "read() requires a filename. Please report that bug."
unless defined $filename;
$refname //= $filename;
my $linenum = 0;
open INPUT, "<$filename"
or croak wrap_msg( dgettext( "po4a", "Cannot read from %s: %s" ), $filename, $! );
while ( defined( my $textline = <INPUT> ) ) {
$linenum++;
my $ref = "$refname:$linenum";
$textline =~ s/\r$//;
my @entry = ( $textline, $ref );
push @{ $self->{TT}{doc_in} }, @entry;
if ( !defined( $self->{TT}{'file_in_charset'} ) ) {
# Detect if this file has non-ascii characters
if ( $self->{TT}{ascii_input} ) {
my $decoder = guess_encoding($textline);
if ( !ref($decoder) or $decoder !~ /Encode::XS=/ ) {
# We have detected a non-ascii line
$self->{TT}{ascii_input} = 0;
# Save the reference for future error message
$self->{TT}{non_ascii_ref} ||= $ref;
}
}
}
}
close INPUT
or croak wrap_msg( dgettext( "po4a", "Cannot close %s after reading: %s" ), $filename, $! );
}
=item write($)
Write the translated document to the given filename.
This translated document data are provided by:
* C<< $self->docheader() >> holding the header text for the plugin, and
* C<< @{$self->{TT}{doc_out}} >> holding each line of the main translated text in the array.
=cut
sub write {
my $self = shift;
my $filename = shift
or croak wrap_msg( dgettext( "po4a", "Cannot write to a file without filename" ) );
my $fh;
if ( $filename eq '-' ) {
$fh = \*STDOUT;
} else {
# make sure the directory in which we should write the localized file exists
my $dir = $filename;
if ( $dir =~ m|/| ) {
$dir =~ s|/[^/]*$||;
File::Path::mkpath( $dir, 0, 0755 ) # Croaks on error
if ( length($dir) && !-e $dir );
}
open $fh, ">$filename"
or croak wrap_msg( dgettext( "po4a", "Cannot write to %s: %s" ), $filename, $! );
}
map { print $fh $_ } $self->docheader();
map { print $fh $_ } @{ $self->{TT}{doc_out} };
if ( $filename ne '-' ) {
close $fh or croak wrap_msg( dgettext( "po4a", "Cannot close %s after writing: %s" ), $filename, $! );
}
}
=back
=head2 Manipulating PO files
=over 4
=item readpo($)
Add the content of a file (which name is passed as argument) to the
existing input PO. The old content is not discarded.
=item writepo($)
Write the extracted PO file to the given filename.
=item stats()
Returns some statistics about the translation done so far. Please note that
it's not the same statistics than the one printed by msgfmt
--statistic. Here, it's stats about recent usage of the PO file, while
msgfmt reports the status of the file. It is a wrapper to the
Locale::Po4a::Po::stats_get function applied to the input PO file. Example
of use:
[normal use of the po4a document...]
($percent,$hit,$queries) = $document->stats();
print "We found translations for $percent\% ($hit from $queries) of strings.\n";
=back
=cut
sub getpoout {
return $_[0]->{TT}{po_out};
}
sub setpoout {
$_[0]->{TT}{po_out} = $_[1];
}
sub readpo {
$_[0]->{TT}{po_in}->read( $_[1] );
}
sub writepo {
$_[0]->{TT}{po_out}->write( $_[1] );
}
sub stats {
return $_[0]->{TT}{po_in}->stats_get();
}
=head2 Manipulating addenda
=over 4
=item addendum($)
Please refer to L<po4a(7)|po4a.7> for more information on what addenda are,
and how translators should write them. To apply an addendum to the translated
document, simply pass its filename to this function and you are done ;)
This function returns a non-null integer on error.
=cut
# Internal function to read the header.
sub addendum_parse {
my ( $filename, $header ) = shift;
my ( $errcode, $mode, $position, $boundary, $bmode, $content ) = ( 1, "", "", "", "", "" );
unless ( open( INS, "<$filename" ) ) {
warn wrap_msg( dgettext( "po4a", "Cannot read from %s: %s" ), $filename, $! );
goto END_PARSE_ADDFILE;
}
unless ( defined( $header = <INS> ) && $header ) {
warn wrap_msg( dgettext( "po4a", "Cannot read po4a header from %s." ), $filename );
goto END_PARSE_ADDFILE;
}
unless ( $header =~ s/PO4A-HEADER://i ) {
warn wrap_msg( dgettext( "po4a", "First line of %s does not look like a po4a header." ), $filename );
goto END_PARSE_ADDFILE;
}
foreach my $part ( split( /;/, $header ) ) {
unless ( $part =~ m/^\s*([^=]*)=(.*)$/ ) {
warn wrap_msg( dgettext( "po4a", "Syntax error in po4a header of %s, near \"%s\"" ), $filename, $part );
goto END_PARSE_ADDFILE;
}
my ( $key, $value ) = ( $1, $2 );
$key = lc($key);
if ( $key eq 'mode' ) {
$mode = lc($value);
} elsif ( $key eq 'position' ) {
$position = $value;
} elsif ( $key eq 'endboundary' ) {
$boundary = $value;
$bmode = 'after';
} elsif ( $key eq 'beginboundary' ) {
$boundary = $value;
$bmode = 'before';
} else {
warn wrap_msg( dgettext( "po4a", "Invalid argument in the po4a header of %s: %s" ), $filename, $key );
goto END_PARSE_ADDFILE;
}
}
unless ( length($mode) ) {
warn wrap_msg( dgettext( "po4a", "The po4a header of %s does not define the mode." ), $filename );
goto END_PARSE_ADDFILE;
}
unless ( $mode eq "before" || $mode eq "after" || $mode eq "eof" ) {
warn wrap_msg(
dgettext(
"po4a",
"Mode invalid in the po4a header of %s: should be 'before', 'after' or 'eof'. Instead, it is '%s'."
),
$filename,
$mode
);
goto END_PARSE_ADDFILE;
}
unless ( length($position) || $mode eq "eof" ) {
warn wrap_msg( dgettext( "po4a", "The po4a header of %s does not define the position." ), $filename );
goto END_PARSE_ADDFILE;
}
if ( $mode eq "after" && length($boundary) == 0 ) {
warn wrap_msg( dgettext( "po4a", "No ending boundary given in the po4a header, but mode=after." ) );
goto END_PARSE_ADDFILE;
}
if ( $mode eq "eof" && length($position) ) {
warn wrap_msg( dgettext( "po4a", "No position needed when mode=eof." ) );
goto END_PARSE_ADDFILE;
}
if ( $mode eq "eof" && length($boundary) ) {
warn wrap_msg( dgettext( "po4a", "No ending boundary needed when mode=eof." ) );
goto END_PARSE_ADDFILE;
}
while ( defined( my $line = <INS> ) ) {
$content .= $line;
}
close INS;
$errcode = 0;
END_PARSE_ADDFILE:
return ( $errcode, $mode, $position, $boundary, $bmode, $content );
}
sub mychomp {
my ($str) = shift;
chomp($str);
return $str;
}
sub addendum {
my ( $self, $filename ) = @_;
print STDERR wrap_mod( "po4a::transtractor::addendum", "Apply addendum %s", $filename )
if $self->debug();
unless ($filename) {
warn wrap_msg( dgettext( "po4a", "Cannot apply addendum when not given the filename" ) );
return 0;
}
die wrap_msg( dgettext( "po4a", "Addendum %s does not exist." ), $filename )
unless -e $filename;
my ( $errcode, $mode, $position, $boundary, $bmode, $content ) = addendum_parse($filename);
return 0 if ($errcode);
# We only recode the addendum if an origin charset is specified, else we
# suppose it's already in the output document's charset
if ( length( $self->{TT}{'addendum_charset'} ) ) {
Encode::from_to( $content, $self->{TT}{'addendum_charset'}, $self->get_out_charset );
}
# In order to make addendum more intuitive, each array item of
# @{$self->{TT}{doc_out}} must not have internal "\n". But previous parser
# code may put multiple internal "\n" to address things like placeholder
# tag handling. Let's normalize array content.
# Use internal "\n" as delimiter but keep it by using the lookbehind trick.
@{ $self->{TT}{doc_out} } = map { split /(?<=\n)/, $_ } @{ $self->{TT}{doc_out} };
# Bugs around addendum is hard to understand. So let's print involved data explicitly.
if ( $self->debug() ) {
print STDERR "Addendum position regex=$position\n";
print STDERR "Addendum mode=$mode\n";
if ( $mode eq "after" ) {
print STDERR "Addendum boundary regex=$boundary\n";
print STDERR "Addendum boundary mode=$bmode\n";
}
print STDERR "Addendum content (begin):\n";
print STDERR "$content";
print STDERR "Addendum content (end)\n";
print STDERR "Output items searched for the addendum insertion position:\n";
foreach my $item ( @{ $self->{TT}{doc_out} } ) {
print STDERR $item;
print STDERR "\n----- [ search item end marker with a preceding newline ] -----\n";
}
print STDERR "Start searching addendum insertion position...\n";
}
unless ( $mode eq 'eof' ) {
my $found = scalar grep { /$position/ } @{ $self->{TT}{doc_out} };
if ( $found == 0 ) {
warn wrap_msg( dgettext( "po4a", "No candidate position for the addendum %s." ), $filename );
return 0;
}
if ( $found > 1 ) {
warn wrap_msg( dgettext( "po4a", "More than one candidate position found for the addendum %s." ),
$filename );
return 0;
}
}
if ( $mode eq "eof" ) {
push @{ $self->{TT}{doc_out} }, $content;
} elsif ( $mode eq "before" ) {
if ( $self->verbose() > 1 || $self->debug() ) {
map {
print STDERR wrap_msg( dgettext( "po4a", "Addendum '%s' applied before this line: %s" ), $filename, $_ )
if (/$position/);
} @{ $self->{TT}{doc_out} };
}
@{ $self->{TT}{doc_out} } = map { /$position/ ? ( $content, $_ ) : $_ } @{ $self->{TT}{doc_out} };
} else {
my @newres = ();
do {
# make sure it doesn't whine on empty document
my $line = scalar @{ $self->{TT}{doc_out} } ? shift @{ $self->{TT}{doc_out} } : "";
push @newres, $line;
my $outline = mychomp($line);
$outline =~ s/^[ \t]*//;
if ( $line =~ m/$position/ ) {
while ( $line = shift @{ $self->{TT}{doc_out} } ) {
last if ( $line =~ /$boundary/ );
push @newres, $line;
}
if ( defined $line ) {
if ( $bmode eq 'before' ) {
print wrap_msg( dgettext( "po4a", "Addendum '%s' applied before this line: %s" ),
$filename, $outline )
if ( $self->verbose() > 1 || $self->debug() );
push @newres, $content;
push @newres, $line;
} else {
print wrap_msg( dgettext( "po4a", "Addendum '%s' applied after the line: %s." ),
$filename, $outline )
if ( $self->verbose() > 1 || $self->debug() );
push @newres, $line;
push @newres, $content;
}
} else {
print wrap_msg( dgettext( "po4a", "Addendum '%s' applied at the end of the file." ), $filename )
if ( $self->verbose() > 1 || $self->debug() );
push @newres, $content;
}
}
} while ( scalar @{ $self->{TT}{doc_out} } );
@{ $self->{TT}{doc_out} } = @newres;
}
print STDERR wrap_mod( "po4a::transtractor::addendum", "Done with addendum %s", $filename )
if $self->debug();
return 1;
}
=back
=head1 INTERNAL FUNCTIONS used to write derivative parsers
=head2 Getting input, providing output
Four functions are provided to get input and return output. They are very
similar to shift/unshift and push/pop of Perl.
* Perl shift returns the first array item and drop it from the array.
* Perl unshift prepends an item to the array as the first array item.
* Perl pop returns the last array item and drop it from the array.
* Perl push appends an item to the array as the last array item.
The first pair is about input, while the second is about output. Mnemonic: in
input, you are interested in the first line, what shift gives, and in output
you want to add your result at the end, like push does.
=over 4
=item shiftline()
This function returns the first line to be parsed and its corresponding
reference (packed as an array) from the array C<< @{$self->{TT}{doc_in}} >> and
drop these first 2 array items. Here, the reference is provided by a string
C<< $filename:$linenum >>.
=item unshiftline($$)
Unshifts the last shifted line of the input document and its corresponding
reference back to the head of C<< {$self->{TT}{doc_in}} >>.
=item pushline($)
Push a new line to the end of C<< {$self->{TT}{doc_out}} >>.
=item popline()
Pop the last pushed line from the end of C<< {$self->{TT}{doc_out}} >>.
=back
=cut
sub shiftline {
my ( $line, $ref ) = ( shift @{ $_[0]->{TT}{doc_in} }, shift @{ $_[0]->{TT}{doc_in} } );
return ( $line, $ref );
}
sub unshiftline {
my $self = shift;
unshift @{ $self->{TT}{doc_in} }, @_;
}
sub pushline { push @{ $_[0]->{TT}{doc_out} }, $_[1] if defined $_[1]; }
sub popline { return pop @{ $_[0]->{TT}{doc_out} }; }
=head2 Marking strings as translatable
One function is provided to handle the text which should be translated.
=over 4
=item translate($$$)
Mandatory arguments:
=over 2
=item -
A string to translate
=item -
The reference of this string (i.e. position in inputfile)
=item -
The type of this string (i.e. the textual description of its structural role;
used in Locale::Po4a::Po::gettextization(); see also L<po4a(7)|po4a.7>,
section B<Gettextization: how does it work?>)
=back
This function can also take some extra arguments. They must be organized as
a hash. For example:
$self->translate("string","ref","type",
'wrap' => 1);
=over
=item B<wrap>
boolean indicating whether we can consider that whitespaces in string are
not important. If yes, the function canonizes the string before looking for
a translation or extracting it, and wraps the translation.
=item B<wrapcol>
the column at which we should wrap (default: 76).
=item B<comment>
an extra comment to add to the entry.
=back
Actions:
=over 2
=item -
Pushes the string, reference and type to po_out.
=item -
Returns the translation of the string (as found in po_in) so that the
parser can build the doc_out.
=item -
Handles the charsets to recode the strings before sending them to
po_out and before returning the translations.
=back
=back
=cut
sub translate {
my $self = shift;
my ( $string, $ref, $type ) = ( shift, shift, shift );
my (%options) = @_;
return "" unless length($string);
# my %validoption;
# map { $validoption{$_}=1 } (qw(wrap wrapcoll));
# foreach (keys %options) {
# Carp::confess "internal error: translate() called with unknown arg $_. Valid options: $validoption"
# unless $validoption{$_};
# }
my $in_charset;
if ( $self->{TT}{ascii_input} ) {
$in_charset = "UTF-8";
} else {
if ( ( $self->{TT}{'file_in_charset'} // '' ) !~ m/ascii/i ) { # // '' to have a default value
$in_charset = $self->{TT}{'file_in_charset'} // "UTF-8";
} else {
# The document charset have to be determined *before* we see the first string to recode.
die wrap_mod(
"po4a",
dgettext(
"po4a",
"Couldn't determine the input document's charset. Please specify it on the command line. (non-ASCII char at %s)"
),
$self->{TT}{non_ascii_ref}
);
}
}
if ( $self->{TT}{po_in}->get_charset ne "CHARSET" ) {
$string = encode_from_to( $string, $self->{TT}{'file_in_encoder'}, $self->{TT}{po_in}{encoder} );
}
if ( defined $options{'wrapcol'} && $options{'wrapcol'} < 0 ) {
# FIXME: should be the parameter given with --width
$options{'wrapcol'} = 76 + $options{'wrapcol'};
}
my $transstring = $self->{TT}{po_in}->gettext(
$string,
'wrap' => $options{'wrap'} || 0,
'wrapcol' => $options{'wrapcol'}
);
if ( $self->{TT}{po_in}->get_charset ne "CHARSET" ) {
my $out_encoder = $self->{TT}{'file_out_encoder'};
unless ( defined $out_encoder ) {
$out_encoder = find_encoding( $self->get_out_charset );
}
$transstring = encode_from_to( $transstring, $self->{TT}{po_in}{encoder}, $out_encoder );
}
# If the input document isn't completely in ascii, we should see what to
# do with the current string
unless ( $self->{TT}{ascii_input} ) {
my $out_charset = $self->{TT}{po_out}->get_charset // "UTF-8";
# We set the output po charset
if ( $out_charset eq "CHARSET" ) {
if ( $self->{TT}{utf_mode} ) {
$out_charset = "UTF-8";
} else {
$out_charset = $in_charset;
}
$self->{TT}{po_out}->set_charset($out_charset);
}
if ( $in_charset !~ /^$out_charset$/i ) {
Encode::from_to( $string, $in_charset, $out_charset );
if ( length( $options{'comment'} ) ) {
Encode::from_to( $options{'comment'}, $in_charset, $out_charset );
}
}
}