Subject: | [PATCH] Make Locale::PO correctly subclassable |
Locale::PO (as in v0.20) is not subclassable, because of the hardcoded
'Locale::PO' package name. This patch tries to make it correctly
subclassable and adds some tests for it (duplicated for singular,
though, sorry about that).
Please can you publish an updated version?
Thank you very much.
Subject: | locale-po-subclassable.patch |
diff -Naur Locale-PO-0.20/Changes Locale-PO-0.20_cosimo/Changes
--- Locale-PO-0.20/Changes 2008-03-15 17:05:05.000000000 +0100
+++ Locale-PO-0.20_cosimo/Changes 2008-05-16 14:37:23.000000000 +0200
@@ -1,5 +1,8 @@
Revision history for Perl extension Locale::PO.
+0.21 May 16 2008
+ - FEATURE: Locale::PO is now correctly subclassable
+ (patch provided by Cosimo Streppone)
0.20 Mar 15 2008
- BUG #34110 - fixed a rather serious problem with the dist not building on
anything but a Mac
diff -Naur Locale-PO-0.20/MANIFEST Locale-PO-0.20_cosimo/MANIFEST
--- Locale-PO-0.20/MANIFEST 2005-10-17 21:42:52.000000000 +0200
+++ Locale-PO-0.20_cosimo/MANIFEST 2008-05-16 14:35:30.000000000 +0200
@@ -4,8 +4,10 @@
Makefile.PL
PO.pm
README
+t/Locale/PO/Subclass.pm
t/plurals.pot
t/plurals.t
t/singular.t
t/test.pot
t/test1.pot
+t/subclass.t
diff -Naur Locale-PO-0.20/META.yml Locale-PO-0.20_cosimo/META.yml
--- Locale-PO-0.20/META.yml 2008-03-15 17:07:04.000000000 +0100
+++ Locale-PO-0.20_cosimo/META.yml 2008-05-16 14:36:02.000000000 +0200
@@ -9,4 +9,7 @@
Test::More: 0
distribution_type: module
+no_index:
+ t/Locale/PO/Subclass.pm
+
generated_by: ExtUtils::MakeMaker version 6.17
diff -Naur Locale-PO-0.20/PO.pm Locale-PO-0.20_cosimo/PO.pm
--- Locale-PO-0.20/PO.pm 2008-03-15 17:06:33.000000000 +0100
+++ Locale-PO-0.20_cosimo/PO.pm 2008-05-16 14:36:37.000000000 +0200
@@ -1,7 +1,7 @@
package Locale::PO;
use strict;
use warnings;
-our $VERSION = '0.20';
+our $VERSION = '0.21';
use Carp;
@@ -314,6 +314,8 @@
my $self = shift;
my $file = shift;
my $ashash = shift;
+ my $class = ref $self || $self;
+
my ( @entries, %entries );
my $line_number = 0;
my $po;
@@ -355,7 +357,7 @@
elsif ( /^#\s+(.*)/ or /^#()$/ ) {
# Translator comments
- $po = Locale::PO->new( -loaded_line_number => $line_number ) unless defined($po);
+ $po = $class->new( -loaded_line_number => $line_number ) unless defined($po);
if ( defined( $po->comment ) ) {
$po->comment( $po->comment . "\n$1" );
}
@@ -366,7 +368,7 @@
elsif (/^#\.\s*(.*)/) {
# Automatic comments
- $po = Locale::PO->new( -loaded_line_number => $line_number ) unless defined($po);
+ $po = $class->new( -loaded_line_number => $line_number ) unless defined($po);
if ( defined( $po->automatic ) ) {
$po->automatic( $po->automatic . "\n$1" );
}
@@ -377,7 +379,7 @@
elsif (/^#:\s+(.*)/) {
# reference
- $po = Locale::PO->new( -loaded_line_number => $line_number ) unless defined($po);
+ $po = $class->new( -loaded_line_number => $line_number ) unless defined($po);
if ( defined( $po->reference ) ) {
$po->reference( $po->reference . "\n$1" );
}
@@ -389,26 +391,26 @@
# flags
my @flags = split /\s*[,]\s*/, $1;
- $po = Locale::PO->new( -loaded_line_number => $line_number ) unless defined($po);
+ $po = $class->new( -loaded_line_number => $line_number ) unless defined($po);
foreach my $flag (@flags)
{
$po->add_flag($flag);
}
}
elsif (/^(#~\s+)?msgctxt\s+(.*)/) {
- $po = Locale::PO->new( -loaded_line_number => $line_number ) unless defined($po);
+ $po = $class->new( -loaded_line_number => $line_number ) unless defined($po);
$buffer{msgctxt} = $self->dequote($2);
$last_buffer = \$buffer{msgctxt};
$po->obsolete(1) if $1;
}
elsif (/^(#~\s+)?msgid\s+(.*)/) {
- $po = Locale::PO->new( -loaded_line_number => $line_number ) unless defined($po);
+ $po = $class->new( -loaded_line_number => $line_number ) unless defined($po);
$buffer{msgid} = $self->dequote($2);
$last_buffer = \$buffer{msgid};
$po->obsolete(1) if $1;
}
elsif (/^(#~\s+)?msgid_plural\s+(.*)/) {
- $po = Locale::PO->new( -loaded_line_number => $line_number ) unless defined($po);
+ $po = $class->new( -loaded_line_number => $line_number ) unless defined($po);
$buffer{msgid_plural} = $self->dequote($2);
$last_buffer = \$buffer{msgid_plural};
$po->obsolete(1) if $1;
diff -Naur Locale-PO-0.20/t/Locale/PO/Subclass.pm Locale-PO-0.20_cosimo/t/Locale/PO/Subclass.pm
--- Locale-PO-0.20/t/Locale/PO/Subclass.pm 1970-01-01 01:00:00.000000000 +0100
+++ Locale-PO-0.20_cosimo/t/Locale/PO/Subclass.pm 2008-05-16 14:32:48.000000000 +0200
@@ -0,0 +1,7 @@
+package Locale::PO::Subclass;
+
+use strict;
+use warnings;
+use base q(Locale::PO);
+
+1;
diff -Naur Locale-PO-0.20/t/subclass.t Locale-PO-0.20_cosimo/t/subclass.t
--- Locale-PO-0.20/t/subclass.t 1970-01-01 01:00:00.000000000 +0100
+++ Locale-PO-0.20_cosimo/t/subclass.t 2008-05-16 14:34:38.000000000 +0200
@@ -0,0 +1,76 @@
+#
+# Check that Locale::PO is correctly subclassable
+#
+
+use strict;
+use warnings;
+
+use Test::More 'no_plan';
+use File::Slurp;
+use lib 't';
+use_ok 'Locale::PO::Subclass';
+
+my $po = new Locale::PO::Subclass(
+ -msgid => 'This is not a pipe',
+ -msgstr => "",
+ -comment =>
+"The entry below is\ndesigned to test the ability of the comment fill code to properly wrap long comments as also to properly normalize the po entries. Apologies to Magritte.",
+ -fuzzy => 1
+);
+ok $po, "got a po object";
+
+my $out = $po->dump;
+ok $out, "dumped the po object";
+
+my @po = $po;
+$po = new Locale::PO::Subclass(
+ -msgid => '',
+ -msgstr => "Project-Id-Version: PACKAGE VERSION\\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\\n"
+);
+ok @po, "got the array";
+ok $po, "got the po object";
+
+unshift @po, $po;
+ok Locale::PO::Subclass->save_file_fromarray( "t/test1.pot.out", \@po ),
+ "save file from array";
+
+ok -e "t/test1.pot.out", "the file was created";
+
+is(
+ read_file("t/test1.pot"),
+ read_file("t/test1.pot.out"),
+ "found no matches - good"
+ )
+ && unlink("t/test1.pot.out");
+
+################################################################################
+#
+# Do some roundtrip tests.
+#
+
+my $pos = Locale::PO::Subclass->load_file_asarray("t/test.pot");
+ok $pos, "loaded test.pot file";
+
+$out = $pos->[0]->dump;
+ok $out, "dumped po object";
+
+is($pos->[1]->loaded_line_number, 16, "got line number of 2nd po entry");
+
+ok Locale::PO::Subclass->save_file_fromarray( "t/test.pot.out", $pos ), "save to file";
+
+
+ok -e "t/test.pot.out", "the file now exists";
+
+is(
+ read_file("t/test.pot"),
+ read_file("t/test.pot.out"),
+ "found no matches - good"
+ )
+ && unlink("t/test.pot.out");
+