/
Po.pm
1363 lines (1108 loc) · 39.3 KB
/
Po.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
# Locale::Po4a::Po -- manipulation of po files
# $Id: Po.pm,v 1.77 2007-06-17 00:19:39 nekral-guest Exp $
#
# This program is free software; you may redistribute it and/or modify it
# under the terms of GPL (see COPYING).
############################################################################
# Modules and declarations
############################################################################
=head1 NAME
Locale::Po4a::Po - po file manipulation module
=head1 SYNOPSIS
use Locale::Po4a::Po;
my $pofile=Locale::Po4a::Po->new();
# Read po file
$pofile->read('file.po');
# Add an entry
$pofile->push('msgid' => 'Hello', 'msgstr' => 'bonjour',
'flags' => "wrap", 'reference'=>'file.c:46');
# Extract a translation
$pofile->gettext("Hello"); # returns 'bonjour'
# Write back to a file
$pofile->write('otherfile.po');
=head1 DESCRIPTION
Locale::Po4a::Po is a module that allows you to manipulate message
catalogs. You can load and write from/to a file (which extension is often
I<po>), you can build new entries on the fly or request for the translation
of a string.
For a more complete description of message catalogs in the po format and
their use, please refer to the documentation of the gettext program.
This module is part of the PO4A project, which objective is to use po files
(designed at origin to ease the translation of program messages) to
translate everything, including documentation (man page, info manual),
package description, debconf templates, and everything which may benefit
from this.
=head1 OPTIONS ACCEPTED BY THIS MODULE
=over 4
=item porefs
This specifies the reference format. It can be one of 'none' to not produce
any reference, 'noline' to not specify the line number, and 'full' to
include complete references.
=back
=cut
use IO::File;
require Exporter;
package Locale::Po4a::Po;
use DynaLoader;
use Locale::Po4a::Common qw(wrap_msg wrap_mod wrap_ref_mod dgettext);
use subs qw(makespace);
use vars qw(@ISA @EXPORT_OK);
@ISA = qw(Exporter DynaLoader);
@EXPORT = qw(%debug);
@EXPORT_OK = qw(&move_po_if_needed);
use Locale::Po4a::TransTractor;
# Try to use a C extension if present.
eval("bootstrap Locale::Po4a::Po $Locale::Po4a::TransTractor::VERSION");
use 5.006;
use strict;
use warnings;
use Carp qw(croak);
use File::Path; # mkdir before write
use File::Copy; # move
use POSIX qw(strftime floor);
use Encode;
my @known_flags=qw(wrap no-wrap c-format fuzzy);
our %debug=('canonize' => 0,
'quote' => 0,
'escape' => 0,
'encoding' => 0,
'filter' => 0);
=head1 Functions about whole message catalogs
=over 4
=item new()
Creates a new message catalog. If an argument is provided, it's the name of
a po file we should load.
=cut
sub new {
my ($this, $options) = (shift, shift);
my $class = ref($this) || $this;
my $self = {};
bless $self, $class;
$self->initialize($options);
my $filename = shift;
$self->read($filename) if defined($filename) && length($filename);
return $self;
}
# Return the numerical timezone (e.g. +0200)
# The %z format of strftime is not portable.
# For example, it indicates "2006-10-25 19:36E. Europe Standard Time" on
# MS Windows.
sub timezone {
my @g = gmtime();
my @l = localtime();
my $diff = floor(strftime("%s", @l)/60 +0.5) - floor(strftime("%s", @g)/60 + 0.5);
my $h = floor($diff / 60) + $l[8]; # $l[8] indicates if we are currently
# in a daylight saving time zone
my $m = $diff%60;
return sprintf "%+03d%02d\n", $h, $m;
}
sub initialize {
my ($self, $options) = (shift, shift);
my $date = strftime("%Y-%m-%d %H:%M", localtime).timezone();
chomp $date;
# $options = ref($options) || $options;
$self->{options}{'porefs'}= 'full';
$self->{options}{'msgid-bugs-address'}= undef;
$self->{options}{'copyright-holder'}= "Free Software Foundation, Inc.";
foreach my $opt (keys %$options) {
if ($options->{$opt}) {
die wrap_mod("po4a::po", dgettext ("po4a", "Unknown option: %s"), $opt) unless exists $self->{options}{$opt};
$self->{options}{$opt} = $options->{$opt};
}
}
$self->{options}{'porefs'} =~ /^(full|noline|none)$/ ||
die wrap_mod("po4a::po",
dgettext ("po4a", "Invalid value for option 'porefs' ('%s' is not one of 'full', 'noline' or 'none')"),
$self->{options}{'porefs'});
$self->{po}=();
$self->{count}=0; # number of msgids in the PO
# count_doc: number of strings in the document
# (duplicate strings counted multiple times)
$self->{count_doc}=0;
$self->{header_comment}=
escape_text( " SOME DESCRIPTIVE TITLE\n"
." Copyright (C) YEAR ".$self->{options}{'copyright-holder'}."\n"
." FIRST AUTHOR <EMAIL\@ADDRESS>, YEAR.\n"
." \n"
.", fuzzy");
# $self->header_tag="fuzzy";
$self->{header}=escape_text("Project-Id-Version: PACKAGE VERSION\n".
((defined $self->{options}{'msgid-bugs-address'})?
"Report-Msgid-Bugs-To: ".$self->{options}{'msgid-bugs-address'}."\n":
"").
"POT-Creation-Date: $date\n".
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n".
"Last-Translator: FULL NAME <EMAIL\@ADDRESS>\n".
"Language-Team: LANGUAGE <LL\@li.org>\n".
"MIME-Version: 1.0\n".
"Content-Type: text/plain; charset=CHARSET\n".
"Content-Transfer-Encoding: ENCODING");
$self->{encoder}=find_encoding("ascii");
# To make stats about gettext hits
$self->stats_clear();
}
=item read($)
Reads a po file (which name is given as argument). Previously existing
entries in self are not removed, the new ones are added to the end of the
catalog.
=cut
sub read {
my $self=shift;
my $filename=shift
|| croak wrap_mod("po4a::po", dgettext("po4a", "Please provide a non-null filename"));
my $fh;
if ($filename eq '-') {
$fh=*STDIN;
} else {
open $fh,"<$filename"
|| croak wrap_mod("po4a::po", dgettext("po4a", "Can't read from %s: %s"), $filename, $!);
}
## Read paragraphs line-by-line
my $pofile="";
my $textline;
while (defined ($textline = <$fh>)) {
$pofile .= $textline;
}
# close INPUT || croak (sprintf(dgettext("po4a","Can't close %s after reading: %s"),$filename,$!)."\n");
my $linenum=0;
foreach my $msg (split (/\n\n/,$pofile)) {
my ($msgid,$msgstr,$comment,$automatic,$reference,$flags,$buffer);
foreach my $line (split (/\n/,$msg)) {
$linenum++;
if ($line =~ /^#\. ?(.*)/) { # Automatic comment
$automatic .= (defined($automatic) ? "\n" : "").$1;
} elsif ($line =~ /^#: ?(.*)/) { # reference
$reference .= (defined($reference) ? "\n" : "").$1;
} elsif ($line =~ /^#, ?(.*)/) { # flags
$flags .= (defined($flags) ? "\n" : "").$1;
} elsif ($line =~ /^#(.*)/ || $line =~ /^#$/) { # Translator comments
$comment .= (defined($comment) ? "\n" : "").($1||"");
} elsif ($line =~ /^msgid (".*")/) { # begin of msgid
$buffer .= (defined($buffer) ? "\n" : "").$1;
} elsif ($line =~ /^msgstr (".*")/) { # begin of msgstr, end of msgid
$msgid = $buffer;
$buffer = "$1";
} elsif ($line =~ /^(".*")/) { # continuation of a line
$buffer .= "\n$1";
} else {
warn wrap_ref_mod("$filename:$linenum", "po4a::po", dgettext("po4a", "Strange line: -->%s<--"), $line);
}
}
$linenum++;
$msgstr=$buffer;
$msgid = unquote_text($msgid) if (defined($msgid));
$msgstr = unquote_text($msgstr) if (defined($msgstr));
$self->push_raw ('msgid' => $msgid,
'msgstr' => $msgstr,
'reference' => $reference,
'flags' => $flags,
'comment' => $comment,
'automatic' => $automatic);
}
}
=item write($)
Writes the current catalog to the given file.
=cut
sub write{
my $self=shift;
my $filename=shift
or croak (dgettext("po4a","Can't write to a file without filename")."\n");
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"
|| croak wrap_mod("po4a::po", dgettext("po4a", "Can't write to %s: %s"), $filename, $!);
}
print $fh "".format_comment(unescape_text($self->{header_comment}),"")
if defined($self->{header_comment}) && length($self->{header_comment});
print $fh "msgid \"\"\n";
print $fh "msgstr ".quote_text($self->{header})."\n\n";
my $first=1;
foreach my $msgid ( sort { ($self->{po}{"$a"}{'pos'}) <=>
($self->{po}{"$b"}{'pos'})
} keys %{$self->{po}}) {
my $output="";
if ($first) {
$first=0;
} else {
$output .= "\n";
}
$output .= format_comment($self->{po}{$msgid}{'comment'},"")
if defined($self->{po}{$msgid}{'comment'})
&& length ($self->{po}{$msgid}{'comment'});
if ( defined($self->{po}{$msgid}{'automatic'})
&& length ($self->{po}{$msgid}{'automatic'})) {
foreach my $comment (split(/\\n/,$self->{po}{$msgid}{'automatic'}))
{
$output .= format_comment($comment, ". ")
}
}
$output .= format_comment($self->{po}{$msgid}{'type'}," type: ")
if defined($self->{po}{$msgid}{'type'})
&& length ($self->{po}{$msgid}{'type'});
$output .= format_comment($self->{po}{$msgid}{'reference'},": ")
if defined($self->{po}{$msgid}{'reference'})
&& length ($self->{po}{$msgid}{'reference'});
$output .= "#, ". join(", ", sort split(/\s+/,$self->{po}{$msgid}{'flags'}))."\n"
if defined($self->{po}{$msgid}{'flags'})
&& length ($self->{po}{$msgid}{'flags'});
if ($self->get_charset =~ /^utf-8$/i) {
my $msgstr = Encode::decode_utf8($self->{po}{$msgid}{'msgstr'});
$msgid = Encode::decode_utf8($msgid);
$output .= Encode::encode_utf8("msgid ".quote_text($msgid)."\n");
$output .= Encode::encode_utf8("msgstr ".quote_text($msgstr)."\n");
} else {
$output .= "msgid ".quote_text($msgid)."\n";
$output .= "msgstr ".quote_text($self->{po}{$msgid}{'msgstr'})."\n";
}
print $fh $output;
}
# print STDERR "$fh";
# if ($filename ne '-') {
# close $fh || croak (sprintf(dgettext("po4a","Can't close %s after writing: %s\n"),$filename,$!));
# }
}
=item write_if_needed($$)
Like write, but if the PO or POT file already exists, the object will be
written in a temporary file which will be compared with the existing file
to check that the update is needed (this avoids to change a POT just to
update a line reference or the POT-Creation-Date field).
=cut
sub move_po_if_needed {
my ($new_po, $old_po, $backup) = (shift, shift, shift);
my $diff;
if (-e $old_po) {
$diff = qx(diff -q -I'^#:' -I'^"POT-Creation-Date:' -I'^"PO-Revision-Date:' $old_po $new_po);
if ( $diff eq "" ) {
unlink $new_po
or die wrap_msg(dgettext("po4a","Can't unlink %s: %s."),
$new_po, $!);
# touch the old PO
my ($atime, $mtime) = (time,time);
utime $atime, $mtime, $old_po;
} else {
if ($backup) {
copy $old_po, $old_po."~"
or die wrap_msg(dgettext("po4a","Can't copy %s to %s: %s."),
$old_po, $old_po."~", $!);
} else {
}
move $new_po, $old_po
or die wrap_msg(dgettext("po4a","Can't move %s to %s: %s."),
$new_po, $old_po, $!);
}
} else {
move $new_po, $old_po
or die wrap_msg(dgettext("po4a","Can't move %s to %s: %s."),
$new_po, $old_po, $!);
}
}
sub write_if_needed {
my $self=shift;
my $filename=shift
or croak (dgettext("po4a","Can't write to a file without filename")."\n");
if (-e $filename) {
my ($tmp_filename);
(undef,$tmp_filename)=File::Temp->tempfile($filename."XXXX",
DIR => "/tmp",
OPEN => 0,
UNLINK => 0);
$self->write($tmp_filename);
move_po_if_needed($tmp_filename, $filename);
} else {
$self->write($filename);
}
}
=item gettextize($$)
This function produces one translated message catalog from two catalogs, an
original and a translation. This process is described in L<po4a(7)|po4a.7>,
section I<Gettextization: how does it work?>.
=cut
sub gettextize {
my $this = shift;
my $class = ref($this) || $this;
my ($poorig,$potrans)=(shift,shift);
my $pores=Locale::Po4a::Po->new();
my $please_fail = 0;
my $toobad = dgettext("po4a",
"\nThe gettextization failed (once again). Don't give up, gettextizing is a subtle art, ".
"but this is only needed once to convert a project to the gorgeous luxus offered ".
"by po4a to translators.".
"\nPlease refer to the po4a(7) documentation, the section \"HOWTO convert a pre-existing ".
"translation to po4a?\" contains several hints to help you in your task");
# Don't fail right now when the entry count does not match. Instead, give it a try so that the user
# can see where we fail (which is probably where the problem is).
if ($poorig->count_entries_doc() > $potrans->count_entries_doc()) {
warn wrap_mod("po4a gettextize", dgettext("po4a",
"Original has more strings than the translation (%d>%d). ".
"Please fix it by editing the translated version to add some dummy entry."),
$poorig->count_entries_doc() , $potrans->count_entries_doc());
$please_fail = 1;
} elsif ($poorig->count_entries_doc() < $potrans->count_entries_doc()) {
warn wrap_mod("po4a gettextize", dgettext("po4a",
"Original has less strings than the translation (%d<%d). ".
"Please fix it by removing the extra entry from the translated file. ".
"You may need an addendum (cf po4a(7)) to reput the chunk in place after gettextization. ".
"A possible cause is that a text duplicated in the original is not translated the same way each time. Remove one of the translations, and you're fine."),
$poorig->count_entries_doc(), $potrans->count_entries_doc());
$please_fail = 1;
}
if ( $poorig->get_charset =~ /^utf-8$/i ) {
$potrans->to_utf8;
$pores->set_charset("utf-8");
} else {
if ($potrans->get_charset eq "CHARSET") {
$pores->set_charset("ascii");
} else {
$pores->set_charset($potrans->get_charset);
}
}
print "Po character sets:\n".
" original=".$poorig->get_charset."\n".
" translated=".$potrans->get_charset."\n".
" result=".$pores->get_charset."\n"
if $debug{'encoding'};
for (my ($o,$t)=(0,0) ;
$o<$poorig->count_entries_doc() && $t<$potrans->count_entries_doc();
$o++,$t++) {
#
# Extract some informations
#
my ($orig,$trans)=($poorig->msgid_doc($o),$potrans->msgid_doc($t));
# print STDERR "Matches [[$orig]]<<$trans>>\n";
my ($reforig,$reftrans)=($poorig->{po}{$orig}{'reference'},
$potrans->{po}{$trans}{'reference'});
my ($typeorig,$typetrans)=($poorig->{po}{$orig}{'type'},
$potrans->{po}{$trans}{'type'});
#
# Make sure the type of both string exist
#
die wrap_mod("po4a gettextize", "Internal error: type of original string number %s isn't provided", $o)
if ($typeorig eq '');
die wrap_mod("po4a gettextize", "Internal error: type of translated string number %s isn't provided", $o)
if ($typetrans eq '');
#
# Make sure both type are the same
#
if ($typeorig ne $typetrans){
$pores->write("gettextization.failed.po");
die wrap_msg(dgettext("po4a",
"po4a gettextization: Structure disparity between original and translated files:\n".
"msgid (at %s) is of type '%s' while\n".
"msgstr (at %s) is of type '%s'.\n".
"Original text: %s\n".
"Translated text: %s\n".
"(result so far dumped to gettextization.failed.po)")."%s",
$reforig, $typeorig, $reftrans, $typetrans, $orig, $trans,$toobad);
}
#
# Push the entry
#
$pores->push_raw('msgid' => $orig, 'msgstr' => $trans,
'flags' => ($poorig->{po}{$orig}{'flags'} ? $poorig->{po}{$orig}{'flags'} :"")." fuzzy",
'type' => $typeorig,
'reference' => $reforig,
'conflict' => 1)
unless (defined($pores->{po}{$orig})
and ($pores->{po}{$orig}{'msgstr'} eq $trans))
# FIXME: maybe we should be smarter about what reference should be
# sent to push_raw.
}
die "$toobad\n" if $please_fail; # make sure we return a useful error message when entry count differ
return $pores;
}
=item filter($)
This function extracts a catalog from an existing one. Only the entries having
a reference in the given file will be placed in the resulting catalog.
This function parses its argument, converts it to a perl function definition,
eval this definition and filter the fields for which this function returns
true.
I love perl sometimes ;)
=cut
sub filter {
my $self=shift;
our $filter=shift;
my $res;
$res = Locale::Po4a::Po->new();
# Parse the filter
our $code="sub apply { return ";
our $pos=0;
our $length = length $filter;
our @filter = split(//,$filter); # explode chars to parts. How to subscript a string in Perl?
sub gloups {
my $fmt=shift;
my $space = "";
for (1..$pos){
$space .= ' ';
}
die wrap_msg("$fmt\n$filter\n$space^ HERE");
}
sub showmethecode {
return unless $debug{'filter'};
my $fmt=shift;
my $space="";
for (1..$pos){
$space .= ' ';
}
print STDERR "$filter\n$space^ $fmt\n";#"$code\n";
}
# I dream of a lex in perl :-/
sub parse_expression {
showmethecode("Begin expression")
if $debug{'filter'};
gloups("Begin of expression expected, got '%s'",$filter[$pos])
unless ($filter[$pos] eq '(');
$pos ++; # pass the '('
if ($filter[$pos] eq '&') {
# AND
$pos++;
showmethecode("Begin of AND")
if $debug{'filter'};
$code .= "(";
while (1) {
gloups ("Unfinished AND statement.")
if ($pos == $length);
parse_expression();
if ($filter[$pos] eq '(') {
$code .= " && ";
} elsif ($filter[$pos] eq ')') {
last; # do not eat that char
} else {
gloups("End of AND or begin of sub-expression expected, got '%s'", $filter[$pos]);
}
}
$code .= ")";
} elsif ($filter[$pos] eq '|') {
# OR
$pos++;
$code .= "(";
while (1) {
gloups("Unfinished OR statement.")
if ($pos == $length);
parse_expression();
if ($filter[$pos] eq '(') {
$code .= " || ";
} elsif ($filter[$pos] eq ')') {
last; # do not eat that char
} else {
gloups("End of OR or begin of sub-expression expected, got '%s'",$filter[$pos]);
}
}
$code .= ")";
} elsif ($filter[$pos] eq '!') {
# NOT
$pos++;
$code .= "(!";
gloups("Missing sub-expression in NOT statement.")
if ($pos == $length);
parse_expression();
$code .= ")";
} else {
# must be an equal. Let's get field and argument
my ($field,$arg,$done);
$field = substr($filter,$pos);
gloups("EQ statement contains no '=' or invalid field name")
unless ($field =~ /([a-z]*)=/i);
$field = lc($1);
$pos += (length $field) + 1;
# check that we've got a valid field name, and the number it referes to
my @names=qw(msgid msgstr reference flags comment automatic); # DO NOT CHANGE THE ORDER
my $fieldpos;
for ($fieldpos = 0;
$fieldpos < scalar @names && $field ne $names[$fieldpos];
$fieldpos++) {}
gloups("Invalid field name: %s",$field)
if $fieldpos == scalar @names; # not found
# Now, get the argument value. It has to be between quotes, which can be escaped
# We point right on the first char of the argument (first quote already ate)
my $escaped = 0;
my $quoted = 0;
if ($filter[$pos] eq '"') {
$pos++;
$quoted = 1;
}
showmethecode(($quoted?"Quoted":"Unquoted")." argument of field '$field'")
if $debug{'filter'};
while (!$done) {
gloups("Unfinished EQ argument.")
if ($pos == $length);
if ($quoted) {
if ($filter[$pos] eq '\\') {
if ($escaped) {
$arg .= '\\';
$escaped = 0;
} else {
$escaped = 1;
}
} elsif ($escaped) {
if ($filter[$pos] eq '"') {
$arg .= '"';
$escaped = 0;
} else {
gloups("Invalid escape sequence in argument: '\\%s'",$filter[$pos]);
}
} else {
if ($filter[$pos] eq '"') {
$done = 1;
} else {
$arg .= $filter[$pos];
}
}
} else {
if ($filter[$pos] eq ')') {
$pos--; # counter the next ++ since we don't want to eat this char
$done = 1;
} else {
$arg .= $filter[$pos];
}
}
$pos++;
}
# and now, add the code to check this equality
$code .= "(\$_[$fieldpos] =~ m/$arg/)";
}
showmethecode("End of expression")
if $debug{'filter'};
gloups("Unfinished statement.")
if ($pos == $length);
gloups("End of expression expected, got '%s'",$filter[$pos])
unless ($filter[$pos] eq ')');
$pos++;
}
# And now, launch the beast, finish the function and use eval to construct this function.
# Ok, the lack of lexer is a fair price for the eval ;)
parse_expression();
gloups("Garbage at the end of the expression")
if ($pos != $length);
$code .= "; }";
print STDERR "CODE = $code\n"
if $debug{'filter'};
eval $code;
die wrap_mod("po4a::po", dgettext("po4a", "Eval failure: %s"), $@)
if $@;
for (my $cpt=(0) ;
$cpt<$self->count_entries();
$cpt++) {
my ($msgid,$ref,$msgstr,$flags,$type,$comment,$automatic);
$msgid = $self->msgid($cpt);
$ref=$self->{po}{$msgid}{'reference'};
$msgstr= $self->{po}{$msgid}{'msgstr'};
$flags = $self->{po}{$msgid}{'flags'};
$type = $self->{po}{$msgid}{'type'};
$comment = $self->{po}{$msgid}{'comment'};
$automatic = $self->{po}{$msgid}{'automatic'};
$res->push_raw('msgid' => $msgid,
'msgstr' => $msgstr,
'flags' => $flags,
'type' => $type,
'reference' => $ref,
'comment' => $comment,
'automatic' => $automatic)
if (apply($msgid,$msgstr,$ref,$flags,$comment,$automatic)); # DO NOT CHANGE THE ORDER
}
# delete the apply subroutine
# otherwise it will be redefined.
undef &apply;
return $res;
}
=item to_utf8()
Recodes to utf-8 the po's msgstrs. Does nothing if the charset is not
specified in the po file ("CHARSET" value), or if it's already utf-8 or
ascii.
=cut
sub to_utf8 {
my $this = shift;
my $charset = $this->get_charset();
unless ($charset eq "CHARSET" or
$charset =~ /^ascii$/i or
$charset =~ /^utf-8$/i) {
foreach my $msgid ( keys %{$this->{po}} ) {
Encode::from_to($this->{po}{$msgid}{'msgstr'}, $charset, "utf-8");
}
$this->set_charset("utf-8");
}
}
=back
=head1 Functions to use a message catalog for translations
=over 4
=item gettext($%)
Request the translation of the string given as argument in the current catalog.
The function returns the original (untranslated) string if the string was not
found.
After the string to translate, you can pass a hash of extra
arguments. Here are the valid entries:
=over
=item 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, and wraps the result.
=item wrapcol
The column at which we should wrap (default: 76).
=back
=cut
sub gettext {
my $self=shift;
my $text=shift;
my (%opt)=@_;
my $res;
return "" unless defined($text) && length($text); # Avoid returning the header.
my $validoption="reference wrap wrapcol";
my %validoption;
map { $validoption{$_}=1 } (split(/ /,$validoption));
foreach (keys %opt) {
Carp::confess "internal error: unknown arg $_.\n".
"Here are the valid options: $validoption.\n"
unless $validoption{$_};
}
$text=canonize($text)
if ($opt{'wrap'});
my $esc_text=escape_text($text);
$self->{gettextqueries}++;
if ($self->{po}{$esc_text} &&
defined( $self->{po}{$esc_text}{'msgstr'} ) &&
length( $self->{po}{$esc_text}{'msgstr'} ) &&
!( ($self->{po}{$esc_text}{'flags'}||"") =~ /fuzzy/) ) {
$self->{gettexthits}++;
$res= unescape_text($self->{po}{$esc_text}{'msgstr'});
} else {
$res= $text;
}
if ($opt{'wrap'}) {
if ($self->get_charset =~ /^utf-8$/i) {
$res=Encode::decode_utf8($res);
$res=wrap ($res, $opt{'wrapcol'} || 76);
$res=Encode::encode_utf8($res);
} else {
$res=wrap ($res, $opt{'wrapcol'} || 76);
}
}
# print STDERR "Gettext >>>$text<<<(escaped=$esc_text)=[[[$res]]]\n\n";
return $res;
}
=item stats_get()
Returns statistics about the hit ratio of gettext since the last time that
stats_clear() was called. Please note that it's not the same
statistics than the one printed by msgfmt --statistic. Here, it's statistics
about recent usage of the po file, while msgfmt reports the status of the
file. Example of use:
[some use of the po file to translate stuff]
($percent,$hit,$queries) = $pofile->stats_get();
print "So far, we found translations for $percent\% ($hit of $queries) of strings.\n";
=cut
sub stats_get() {
my $self=shift;
my ($h,$q)=($self->{gettexthits},$self->{gettextqueries});
my $p = ($q == 0 ? 100 : int($h/$q*10000)/100);
# $p =~ s/\.00//;
# $p =~ s/(\..)0/$1/;
return ( $p,$h,$q );
}
=item stats_clear()
Clears the statistics about gettext hits.
=cut
sub stats_clear {
my $self = shift;
$self->{gettextqueries} = 0;
$self->{gettexthits} = 0;
}
=back
=head1 Functions to build a message catalog
=over 4
=item push(%)
Push a new entry at the end of the current catalog. The arguments should
form a hash table. The valid keys are:
=over 4
=item msgid
the string in original language.
=item msgstr
the translation.
=item reference
an indication of where this string was found. Example: file.c:46 (meaning
in 'file.c' at line 46). It can be a space-separated list in case of
multiple occurrences.
=item comment
a comment added here manually (by the translators). The format here is free.
=item automatic
a comment which was automatically added by the string extraction
program. See the I<--add-comments> option of the B<xgettext> program for
more information.
=item flags
space-separated list of all defined flags for this entry.
Valid flags are: c-text, python-text, lisp-text, elisp-text, librep-text,
smalltalk-text, java-text, awk-text, object-pascal-text, ycp-text,
tcl-text, wrap, no-wrap and fuzzy.
See the gettext documentation for their meaning.
=item type
This is mostly an internal argument: it is used while gettextizing
documents. The idea here is to parse both the original and the translation
into a po object, and merge them, using one's msgid as msgid and the
other's msgid as msgstr. To make sure that things get ok, each msgid in po
objects are given a type, based on their structure (like "chapt", "sect1",
"p" and so on in docbook). If the types of strings are not the same, that
means that both files do not share the same structure, and the process
reports an error.
This information is written as automatic comment in the po file since this
gives to translators some context about the strings to translate.
=item wrap
boolean indicating whether whitespaces can be mangled in cosmetic
reformattings. If true, the string is canonized before use.
This information is written to the po file using the 'wrap' or 'no-wrap' flag.
=item wrapcol
The column at which we should wrap (default: 76).
This information is not written to the po file.
=back
=cut
sub push {
my $self=shift;
my %entry=@_;
my $validoption="wrap wrapcol type msgid msgstr automatic flags reference";
my %validoption;
map { $validoption{$_}=1 } (split(/ /,$validoption));
foreach (keys %entry) {
Carp::confess "internal error: unknown arg $_.\n".
"Here are the valid options: $validoption.\n"
unless $validoption{$_};
}
unless ($entry{'wrap'}) {
$entry{'flags'} .= " no-wrap";
}
if (defined ($entry{'msgid'})) {
$entry{'msgid'} = canonize($entry{'msgid'})
if ($entry{'wrap'});
$entry{'msgid'} = escape_text($entry{'msgid'});
}
if (defined ($entry{'msgstr'})) {
$entry{'msgstr'} = canonize($entry{'msgstr'})
if ($entry{'wrap'});
$entry{'msgstr'} = escape_text($entry{'msgstr'});
}
$self->push_raw(%entry);
}
# The same as push(), but assuming that msgid and msgstr are already escaped
sub push_raw {
my $self=shift;
my %entry=@_;
my ($msgid,$msgstr,$reference,$comment,$automatic,$flags,$type)=
($entry{'msgid'},$entry{'msgstr'},
$entry{'reference'},$entry{'comment'},$entry{'automatic'},
$entry{'flags'},$entry{'type'});
my $keep_conflict = $entry{'conflict'};
# print STDERR "Push_raw\n";
# print STDERR " msgid=>>>$msgid<<<\n" if $msgid;
# print STDERR " msgstr=[[[$msgstr]]]\n" if $msgstr;
# Carp::cluck " flags=$flags\n" if $flags;
return unless defined($entry{'msgid'});
#no msgid => header definition