2006-01-05 20:02:06 -05:00
|
|
|
# This file has been changed from the "Standard Version" Locale-PO-0.16.
|
|
|
|
# The copyright, license, and change log are in the POD at the bottom.
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
package Locale::PO;
|
|
|
|
|
|
|
|
use Carp;
|
2006-02-05 10:47:36 -05:00
|
|
|
|
|
|
|
# Internally, each Locale::PO object is a reference to a hash
|
|
|
|
# (or pseudo-hash, for Perl < 5.9) with the following keys.
|
|
|
|
# The format of the hash is subject to change; other modules
|
|
|
|
# should use the accessor methods instead.
|
|
|
|
use fields
|
|
|
|
# Multiline strings including quotes and newlines, but excluding
|
|
|
|
# the initial keywords and any "#~ " obsoletion marks. Can be
|
2006-02-12 10:16:35 -05:00
|
|
|
# either undef or "" if not present. These normally do not end
|
|
|
|
# with a newline.
|
2006-02-05 10:47:36 -05:00
|
|
|
qw(msgid msgid_plural msgstr),
|
|
|
|
# A reference to a hash where keys are numbers (as strings)
|
|
|
|
# and values are in the same format as $self->{msgstr}.
|
|
|
|
qw(msgstr_n),
|
2006-02-12 12:25:27 -05:00
|
|
|
# Line numbers. The file name is not currently saved.
|
|
|
|
qw(_msgid_begin_lineno _msgstr_begin_lineno),
|
2006-02-05 10:47:36 -05:00
|
|
|
# Multiline strings excluding the trailing newline and comment
|
|
|
|
# markers, or undef if there are no such lines.
|
2006-02-18 13:30:40 -05:00
|
|
|
qw(comment automatic reference),
|
|
|
|
# Named flags. These are kept in two formats:
|
|
|
|
# - $self->{'_flaghash'} is undef Locale::PO has not yet parsed the flags.
|
|
|
|
# Otherwise, it refers to a hash where the keys are names of flags that
|
|
|
|
# have been set, and the values are all 1. (The hash can be empty.)
|
|
|
|
# - $self->{'_flagstr'} is undef if there are no flags; or a string in the
|
|
|
|
# same format as $self->{'automatic'}; or a reference to the same hash as
|
|
|
|
# $self->{'_flaghash'} if Locale::PO has changed a flag and not yet
|
|
|
|
# reformatted the flags as a string.
|
|
|
|
qw(_flagstr _flaghash),
|
|
|
|
# 1 if the entry is obsolete; undef if not.
|
|
|
|
qw(_obsolete);
|
2006-01-01 16:32:42 -05:00
|
|
|
|
|
|
|
#use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
|
|
|
|
#use locale;
|
|
|
|
|
|
|
|
#require Exporter;
|
|
|
|
#require AutoLoader;
|
|
|
|
|
|
|
|
#@ISA = qw(Exporter AutoLoader);
|
|
|
|
#@EXPORT = qw();
|
2006-01-05 20:02:06 -05:00
|
|
|
our $VERSION = '0.16.kon';
|
2006-01-01 16:32:42 -05:00
|
|
|
|
|
|
|
# Preloaded methods go here.
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
my $this = shift;
|
|
|
|
my %options = @_;
|
2006-01-06 10:56:08 -05:00
|
|
|
my Locale::PO $self = ref($this) ? $this : fields::new($this);
|
2006-01-01 16:32:42 -05:00
|
|
|
$self->msgid( $options{'-msgid'} ) if defined( $options{'-msgid'} );
|
|
|
|
$self->msgid_plural( $options{'-msgid_plural'} )
|
|
|
|
if defined( $options{'-msgid_plural'} );
|
|
|
|
$self->msgstr( $options{'-msgstr'} ) if defined( $options{'-msgstr'} );
|
|
|
|
$self->msgstr_n( $options{'-msgstr_n'} )
|
|
|
|
if defined( $options{'-msgstr_n'} );
|
|
|
|
$self->comment( $options{'-comment'} ) if defined( $options{'-comment'} );
|
|
|
|
$self->fuzzy( $options{'-fuzzy'} ) if defined( $options{'-fuzzy'} );
|
|
|
|
$self->automatic( $options{'-automatic'} )
|
|
|
|
if defined( $options{'-automatic'} );
|
|
|
|
$self->reference( $options{'-reference'} )
|
|
|
|
if defined( $options{'-reference'} );
|
|
|
|
$self->c_format(1) if defined( $options{'-c-format'} );
|
|
|
|
$self->c_format(1) if defined( $options{'-c_format'} );
|
|
|
|
$self->c_format(0) if defined( $options{'-no-c-format'} );
|
|
|
|
$self->c_format(0) if defined( $options{'-no_c_format'} );
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub msgid {
|
2006-01-06 10:56:08 -05:00
|
|
|
my Locale::PO $self = shift;
|
2006-02-12 12:25:27 -05:00
|
|
|
if (@_) {
|
|
|
|
$self->{msgid} = $self->quote(shift);
|
|
|
|
# TODO: Should this erase $self->{_msgid_begin_lineno}?
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $self->{msgid};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub msgid_begin_lineno {
|
|
|
|
my Locale::PO $self = shift;
|
|
|
|
# We should have a way to pass extra arguments (e.g. quoting
|
|
|
|
# level) to getters, without making them behave as setters. That
|
|
|
|
# may require an incompatible API change, which in turn requires
|
|
|
|
# extra methods in order to preserve compatibility. Don't allow
|
|
|
|
# setting msgid_begin_lineno yet; thus this method won't have to
|
|
|
|
# be duplicated.
|
|
|
|
croak "Setting msgid_begin_lineno is not currently allowed" if @_;
|
|
|
|
return $self->{_msgid_begin_lineno};
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sub msgid_plural {
|
2006-01-06 10:56:08 -05:00
|
|
|
my Locale::PO $self = shift;
|
2006-01-01 16:32:42 -05:00
|
|
|
@_
|
|
|
|
? $self->{'msgid_plural'} =
|
|
|
|
$self->quote(shift)
|
|
|
|
: $self->{'msgid_plural'};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub msgstr {
|
2006-01-06 10:56:08 -05:00
|
|
|
my Locale::PO $self = shift;
|
2006-02-12 12:25:27 -05:00
|
|
|
if (@_) {
|
|
|
|
$self->{msgstr} = $self->quote(shift);
|
|
|
|
# TODO: Should this erase $self->{_msgstr_begin_lineno}?
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $self->{msgstr};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub msgstr_begin_lineno {
|
|
|
|
my Locale::PO $self = shift;
|
|
|
|
# We should have a way to pass extra arguments (e.g. quoting
|
|
|
|
# level) to getters, without making them behave as setters. That
|
|
|
|
# may require an incompatible API change, which in turn requires
|
|
|
|
# extra methods in order to preserve compatibility. Don't allow
|
|
|
|
# setting msgstr_begin_lineno yet; thus this method won't have to
|
|
|
|
# be duplicated.
|
|
|
|
croak "Setting msgstr_begin_lineno is not currently allowed" if @_;
|
|
|
|
return $self->{_msgstr_begin_lineno};
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sub msgstr_n {
|
2006-01-06 10:56:08 -05:00
|
|
|
my Locale::PO $self = shift;
|
2006-01-01 16:32:42 -05:00
|
|
|
if (@_) {
|
|
|
|
my $hashref = shift;
|
|
|
|
|
|
|
|
# check that we have a hashref.
|
|
|
|
croak
|
|
|
|
'Argument to msgstr_n must be a hashref: { n => "string n", ... }.'
|
|
|
|
unless ref($hashref) eq 'HASH';
|
|
|
|
|
|
|
|
# Check that the keys are all numbers.
|
|
|
|
croak 'Keys to msgstr_n hashref must be numbers'
|
|
|
|
if grep { m/\D/ } keys %$hashref;
|
|
|
|
|
|
|
|
# Quote all the values in the hashref.
|
|
|
|
$self->{'msgstr_n'}{$_} = $self->quote( $$hashref{$_} )
|
|
|
|
for keys %$hashref;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $self->{'msgstr_n'};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub comment {
|
2006-01-06 10:56:08 -05:00
|
|
|
my Locale::PO $self = shift;
|
2006-01-01 16:32:42 -05:00
|
|
|
@_ ? $self->{'comment'} = shift: $self->{'comment'};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub automatic {
|
2006-01-06 10:56:08 -05:00
|
|
|
my Locale::PO $self = shift;
|
2006-01-01 16:32:42 -05:00
|
|
|
@_ ? $self->{'automatic'} = shift: $self->{'automatic'};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub reference {
|
2006-01-06 10:56:08 -05:00
|
|
|
my Locale::PO $self = shift;
|
2006-01-01 16:32:42 -05:00
|
|
|
@_ ? $self->{'reference'} = shift: $self->{'reference'};
|
|
|
|
}
|
|
|
|
|
2006-02-18 13:30:40 -05:00
|
|
|
# Methods whose names begin with "_" may be changed or removed in
|
|
|
|
# future versions.
|
|
|
|
sub _update_flaghash {
|
2006-01-06 10:56:08 -05:00
|
|
|
my Locale::PO $self = shift;
|
2006-02-18 13:30:40 -05:00
|
|
|
if (!defined($self->{'_flaghash'})) {
|
|
|
|
my @flags;
|
|
|
|
@flags = split(/[\s,]+/, $self->{'_flagstr'})
|
|
|
|
if defined $self->{'_flagstr'};
|
|
|
|
$self->{'_flaghash'} = { map { $_ => 1 } @flags };
|
|
|
|
}
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
|
|
|
|
2006-02-18 13:30:40 -05:00
|
|
|
# Methods whose names begin with "_" may be changed or removed in
|
|
|
|
# future versions.
|
|
|
|
sub _update_flagstr {
|
2006-02-11 17:33:11 -05:00
|
|
|
my Locale::PO $self = shift;
|
2006-02-18 13:30:40 -05:00
|
|
|
if (ref($self->{'_flagstr'}) eq 'HASH') {
|
|
|
|
# GNU Gettext seems to put the "fuzzy" flag first.
|
|
|
|
# Do the same here, in case someone's relying on it.
|
|
|
|
# However, the other flags will be sorted differently.
|
|
|
|
my %flags = %{$self->{'_flagstr'}};
|
|
|
|
my @flags = ();
|
|
|
|
push @flags, 'fuzzy' if delete $flags{'fuzzy'};
|
|
|
|
push @flags, sort { $a cmp $b } keys %flags;
|
|
|
|
$self->{'_flagstr'} = (@flags ? join(', ', @flags) : undef);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Methods whose names begin with "_" may be changed or removed in
|
|
|
|
# future versions.
|
|
|
|
sub _flag {
|
|
|
|
my Locale::PO $self = shift;
|
|
|
|
my $name = shift;
|
|
|
|
$self->_update_flaghash();
|
|
|
|
if (@_) { # set or clear the flag
|
|
|
|
$self->{'_flagstr'} = $self->{'_flaghash'};
|
|
|
|
if (shift) {
|
|
|
|
$self->{'_flaghash'}{$name} = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
delete $self->{'_flaghash'}{$name};
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { # check the flag
|
|
|
|
return exists $self->{'_flaghash'}{$name};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Methods whose names begin with "_" may be changed or removed in
|
|
|
|
# future versions.
|
|
|
|
sub _tristate {
|
|
|
|
my Locale::PO $self = shift;
|
|
|
|
my $name = shift;
|
|
|
|
$self->_update_flaghash();
|
|
|
|
if (@_) { # set or clear the flags
|
|
|
|
$self->{'_flagstr'} = $self->{'_flaghash'};
|
|
|
|
my $val = shift;
|
|
|
|
if (!defined($val) || $val eq "") {
|
|
|
|
delete $self->{'_flaghash'}{"$name"};
|
|
|
|
delete $self->{'_flaghash'}{"no-$name"};
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
elsif ($val) {
|
|
|
|
$self->{'_flaghash'}{"$name"} = 1;
|
|
|
|
delete $self->{'_flaghash'}{"no-$name"};
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
delete $self->{'_flaghash'}{"$name"};
|
|
|
|
$self->{'_flaghash'}{"no-$name"} = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { # check the flags
|
|
|
|
return 1 if $self->{'_flaghash'}{"$name"};
|
|
|
|
return 0 if $self->{'_flaghash'}{"no-$name"};
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub fuzzy {
|
|
|
|
my Locale::PO $self = shift;
|
|
|
|
return $self->_flag('fuzzy', @_);
|
2006-02-11 17:33:11 -05:00
|
|
|
}
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
sub c_format {
|
2006-01-06 10:56:08 -05:00
|
|
|
my Locale::PO $self = shift;
|
2006-02-18 13:30:40 -05:00
|
|
|
return $self->_tristate('c-format', @_);
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sub php_format {
|
2006-01-06 10:56:08 -05:00
|
|
|
my Locale::PO $self = shift;
|
2006-02-18 13:30:40 -05:00
|
|
|
return $self->_tristate('php-format', @_);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub obsolete {
|
|
|
|
my Locale::PO $self = shift;
|
|
|
|
@_ ? $self->{_obsolete} = shift : $self->{_obsolete};
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
|
|
|
|
2006-02-05 11:21:25 -05:00
|
|
|
# Methods whose names begin with "_" may be changed or removed in
|
|
|
|
# future versions.
|
|
|
|
sub _normalize_str {
|
2006-01-06 10:56:08 -05:00
|
|
|
my $self = shift; # can be called as a class method
|
2006-01-01 16:32:42 -05:00
|
|
|
my $string = shift;
|
|
|
|
my $dequoted = $self->dequote($string);
|
|
|
|
|
|
|
|
# This isn't quite perfect, but it's fast and easy
|
|
|
|
if ( $dequoted =~ /(^|[^\\])(\\\\)*\\n./ ) {
|
|
|
|
|
|
|
|
# Multiline
|
|
|
|
my $output;
|
|
|
|
my @lines;
|
|
|
|
$output = '""' . "\n";
|
|
|
|
@lines = split( /\\n/, $dequoted, -1 );
|
|
|
|
my $lastline = pop @lines; # special treatment for this one
|
|
|
|
foreach (@lines) {
|
|
|
|
$output .= $self->quote("$_\\n") . "\n";
|
|
|
|
}
|
|
|
|
$output .= $self->quote($lastline) . "\n" if $lastline ne "";
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
# Single line
|
|
|
|
return "$string\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub dump {
|
2006-01-06 10:56:08 -05:00
|
|
|
my Locale::PO $self = shift;
|
2006-01-01 16:32:42 -05:00
|
|
|
my $dump;
|
2006-02-18 13:30:40 -05:00
|
|
|
$dump .= $self->_dump_multi_comment( $self->comment, "# " )
|
2006-02-11 17:33:11 -05:00
|
|
|
if defined( $self->comment );
|
2006-02-05 11:21:25 -05:00
|
|
|
$dump .= $self->_dump_multi_comment( $self->automatic, "#. " )
|
2006-02-11 17:33:11 -05:00
|
|
|
if defined( $self->automatic );
|
2006-02-05 11:21:25 -05:00
|
|
|
$dump .= $self->_dump_multi_comment( $self->reference, "#: " )
|
2006-02-11 17:33:11 -05:00
|
|
|
if defined( $self->reference );
|
2006-02-18 13:30:40 -05:00
|
|
|
$self->_update_flagstr();
|
|
|
|
$dump .= $self->_dump_multi_comment( $self->{'_flagstr'}, "#, " )
|
|
|
|
if defined( $self->{'_flagstr'} );
|
2006-02-05 11:21:25 -05:00
|
|
|
$dump .= "msgid " . $self->_normalize_str( $self->msgid );
|
|
|
|
$dump .= "msgid_plural " . $self->_normalize_str( $self->msgid_plural )
|
2006-01-01 16:32:42 -05:00
|
|
|
if $self->msgid_plural;
|
|
|
|
|
2006-02-05 11:21:25 -05:00
|
|
|
$dump .= "msgstr " . $self->_normalize_str( $self->msgstr ) if $self->msgstr;
|
2006-01-01 16:32:42 -05:00
|
|
|
|
|
|
|
if ( my $msgstr_n = $self->msgstr_n ) {
|
2006-02-05 11:21:25 -05:00
|
|
|
$dump .= "msgstr[$_] " . $self->_normalize_str( $$msgstr_n{$_} )
|
2006-01-01 16:32:42 -05:00
|
|
|
for sort { $a <=> $b } keys %$msgstr_n;
|
|
|
|
}
|
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
$dump =~ s/^(?!#)/#~ /gm if $self->{_obsolete};
|
2006-01-01 16:32:42 -05:00
|
|
|
$dump .= "\n";
|
|
|
|
return $dump;
|
|
|
|
}
|
|
|
|
|
2006-02-05 11:21:25 -05:00
|
|
|
# Methods whose names begin with "_" may be changed or removed in
|
|
|
|
# future versions.
|
|
|
|
sub _dump_multi_comment {
|
2006-01-06 10:56:08 -05:00
|
|
|
my $self = shift; # can be called as a class method
|
2006-01-01 16:32:42 -05:00
|
|
|
my $comment = shift;
|
|
|
|
my $leader = shift;
|
|
|
|
my $chopped = $leader;
|
|
|
|
chop($chopped);
|
|
|
|
my $result = $leader . $comment;
|
|
|
|
$result =~ s/\n/\n$leader/g;
|
|
|
|
$result =~ s/^$leader$/$chopped/gm;
|
|
|
|
$result .= "\n";
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Quote a string properly
|
|
|
|
sub quote {
|
2006-01-06 10:56:08 -05:00
|
|
|
my $self = shift; # can be called as a class method
|
2006-01-01 16:32:42 -05:00
|
|
|
my $string = shift;
|
|
|
|
$string =~ s/"/\\"/g;
|
|
|
|
return "\"$string\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub dequote {
|
2006-01-06 10:56:08 -05:00
|
|
|
my $self = shift; # can be called as a class method
|
2006-01-01 16:32:42 -05:00
|
|
|
my $string = shift;
|
2006-02-11 17:33:11 -05:00
|
|
|
$string =~ s/^"(.*)"/$1/gm;
|
2006-01-01 16:32:42 -05:00
|
|
|
$string =~ s/\\"/"/g;
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub save_file_fromarray {
|
2006-01-06 10:56:08 -05:00
|
|
|
my $self = shift; # normally called as a class method
|
2006-01-01 16:32:42 -05:00
|
|
|
$self->save_file( @_, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub save_file_fromhash {
|
2006-01-06 10:56:08 -05:00
|
|
|
my $self = shift; # normally called as a class method
|
2006-01-01 16:32:42 -05:00
|
|
|
$self->save_file( @_, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub save_file {
|
2006-01-06 10:56:08 -05:00
|
|
|
my $self = shift; # normally called as a class method
|
2006-01-01 16:32:42 -05:00
|
|
|
my $file = shift;
|
|
|
|
my $entries = shift;
|
|
|
|
my $ashash = shift;
|
2006-02-12 10:16:35 -05:00
|
|
|
my $err = undef;
|
2006-01-01 16:32:42 -05:00
|
|
|
open( OUT, ">$file" ) or return undef;
|
|
|
|
if ($ashash) {
|
|
|
|
foreach ( sort keys %$entries ) {
|
2006-02-12 10:16:35 -05:00
|
|
|
print OUT $entries->{$_}->dump or $err=$!, last;
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreach (@$entries) {
|
2006-02-12 10:16:35 -05:00
|
|
|
print OUT $_->dump or $err=$!, last;
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
|
|
|
}
|
2006-02-12 10:16:35 -05:00
|
|
|
close OUT or $err=$!;
|
|
|
|
if ($err) { $!=$err; return undef }
|
|
|
|
else { return 1 }
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sub load_file_asarray {
|
2006-01-06 10:56:08 -05:00
|
|
|
my $self = shift; # normally called as a class method
|
2006-02-11 17:33:11 -05:00
|
|
|
my $file = shift;
|
|
|
|
my @entries;
|
2006-01-01 16:32:42 -05:00
|
|
|
my $po;
|
|
|
|
my $last_buffer;
|
2006-02-11 17:33:11 -05:00
|
|
|
use constant {
|
|
|
|
# Nothing other than comments has been seen for $po yet.
|
|
|
|
# $po may also be undef.
|
|
|
|
# On comment: save it, and stay in this state.
|
|
|
|
# On msgid: save it, and switch to STATE_STRING.
|
|
|
|
# On some other string: warn that the msgid is missing,
|
|
|
|
# and redo in STATE_STRING.
|
|
|
|
STATE_COMMENT => 1,
|
|
|
|
|
|
|
|
# A non-comment string has been seen for $po.
|
|
|
|
# $po can not be undef.
|
|
|
|
# If $po->{msgid} is undef, that has already been warned about.
|
|
|
|
# On comment: close the entry, and redo in STATE_COMMENT.
|
|
|
|
# On msgid: close the entry, and redo in STATE_COMMENT.
|
|
|
|
# On some other string: save it, and stay in this state.
|
|
|
|
STATE_STRING => 2,
|
|
|
|
};
|
|
|
|
my $state = STATE_COMMENT;
|
2006-02-05 10:47:36 -05:00
|
|
|
local $/ = "\n";
|
|
|
|
local $_;
|
2006-01-01 16:32:42 -05:00
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
# "my sub" not yet implemented
|
|
|
|
my $check_and_push_entry = sub {
|
|
|
|
# Call this only if defined($po).
|
|
|
|
warn "$file:$.: Expected msgid\n"
|
|
|
|
unless defined $po->{msgid} or $state == STATE_STRING;
|
|
|
|
if (defined $po->{msgid_plural}) {
|
|
|
|
warn "$file:$.: Expected msgstr[n]\n"
|
|
|
|
unless defined $po->{msgstr_n};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
warn "$file:$.: Expected msgstr\n"
|
|
|
|
unless defined $po->{msgstr};
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
push( @entries, $po);
|
|
|
|
$po = undef;
|
|
|
|
$last_buffer = undef;
|
|
|
|
};
|
|
|
|
|
|
|
|
open( IN, "<$file" ) or return undef;
|
|
|
|
LINE: while (<IN>) {
|
|
|
|
chomp;
|
|
|
|
my $obsolete = s/^#~ ?//;
|
|
|
|
next LINE if /^$/;
|
|
|
|
|
|
|
|
if (/^"/) {
|
|
|
|
# Continued string. This is very common, so check it first.
|
|
|
|
warn("$file:$.: There is no string to be continued\n"), next LINE
|
|
|
|
unless defined $last_buffer;
|
|
|
|
$$last_buffer .= "\n$_";
|
|
|
|
}
|
|
|
|
elsif (/^#([,.:]?)()$/ or /^#([,.:]?) (.*)$/) {
|
|
|
|
&$check_and_push_entry if $state == STATE_STRING;
|
|
|
|
$state = STATE_COMMENT;
|
|
|
|
$po = new Locale::PO unless defined $po;
|
|
|
|
|
|
|
|
my $comment;
|
|
|
|
if ($1 eq "") { $comment = \$po->{comment} }
|
2006-02-18 13:30:40 -05:00
|
|
|
elsif ($1 eq ",") { $comment = \$po->{_flagstr} }
|
2006-02-11 17:33:11 -05:00
|
|
|
elsif ($1 eq ".") { $comment = \$po->{automatic} }
|
|
|
|
elsif ($1 eq ":") { $comment = \$po->{reference} }
|
|
|
|
else { warn "Bug: did not recognize '$1'"; next LINE }
|
|
|
|
|
|
|
|
if (defined( $$comment )) { $$comment .= "\n$2" }
|
|
|
|
else { $$comment = $2 }
|
|
|
|
}
|
|
|
|
elsif (/^msgid (.*)$/) {
|
|
|
|
&$check_and_push_entry if $state == STATE_STRING;
|
|
|
|
$state = STATE_STRING;
|
|
|
|
$po = new Locale::PO unless defined $po;
|
|
|
|
$last_buffer = \($po->{msgid} = $1);
|
2006-02-12 12:25:27 -05:00
|
|
|
$po->{_msgid_begin_lineno} = $.;
|
2006-02-11 17:33:11 -05:00
|
|
|
}
|
|
|
|
elsif (/^msgid_plural (.*)$/) {
|
|
|
|
if ($state == STATE_COMMENT) {
|
|
|
|
warn "$file:$.: Expected msgid\n";
|
|
|
|
$po = new Locale::PO unless defined $po;
|
|
|
|
$state = STATE_STRING;
|
|
|
|
}
|
|
|
|
if (defined $po->{msgid_plural}) {
|
|
|
|
warn "$file:$.: Replacing previous msgid_plural\n";
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
2006-02-11 17:33:11 -05:00
|
|
|
elsif (defined $po->{msgstr}) {
|
|
|
|
warn "$file:$.: Should not have msgid_plural with msgstr\n";
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
2006-02-11 17:33:11 -05:00
|
|
|
$last_buffer = \($po->{msgid_plural} = $1);
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
2006-02-11 17:33:11 -05:00
|
|
|
elsif (/^msgstr (.*)$/) {
|
|
|
|
if ($state == STATE_COMMENT) {
|
|
|
|
warn "$file:$.: Expected msgid\n";
|
|
|
|
$po = new Locale::PO unless defined $po;
|
|
|
|
$state = STATE_STRING;
|
|
|
|
}
|
|
|
|
if (defined $po->{msgstr}) {
|
|
|
|
warn "$file:$.: Replacing previous msgstr\n";
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
2006-02-11 17:33:11 -05:00
|
|
|
elsif (defined $po->{msgid_plural}) {
|
|
|
|
warn "$file:$.: Should not have msgstr with msgid_plural\n";
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
2006-02-11 17:33:11 -05:00
|
|
|
elsif (defined $po->{msgstr_n}) {
|
|
|
|
warn "$file:$.: Should not have msgstr with msgstr[n]\n";
|
|
|
|
}
|
|
|
|
$last_buffer = \($po->{msgstr} = $1);
|
2006-02-12 12:25:27 -05:00
|
|
|
$po->{_msgstr_begin_lineno} = $.;
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
2006-02-11 17:33:11 -05:00
|
|
|
elsif (/^msgstr\[(\d+)\] (.*)$/) {
|
|
|
|
if ($state == STATE_COMMENT) {
|
|
|
|
warn "$file:$.: Expected msgid\n";
|
|
|
|
$po = new Locale::PO unless defined $po;
|
|
|
|
$state = STATE_STRING;
|
|
|
|
}
|
|
|
|
if (defined $po->{msgstr_n}) {
|
|
|
|
warn "$file:$.: Replacing previous msgstr[$1]\n"
|
|
|
|
if defined $po->{msgstr_n}{$1};
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
2006-02-11 17:33:11 -05:00
|
|
|
elsif (defined $po->{msgstr}) {
|
|
|
|
warn "$file:$.: Should not have msgstr[n] with msgstr\n";
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
2006-02-11 17:33:11 -05:00
|
|
|
$last_buffer = \($po->{msgstr_n}{$1} = $2);
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
2006-02-11 17:33:11 -05:00
|
|
|
else {
|
|
|
|
warn "$file:$.: Ignoring strange line: $_\n";
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
2006-02-11 17:33:11 -05:00
|
|
|
$po->{_obsolete} = 1 if $obsolete && defined $po;
|
|
|
|
}
|
|
|
|
&$check_and_push_entry if defined $po;
|
|
|
|
close IN;
|
|
|
|
return \@entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub load_file_ashash {
|
|
|
|
my $self = shift; # normally called as a class method
|
|
|
|
my $file = shift;
|
|
|
|
my $entries = $self->load_file_asarray( $file );
|
|
|
|
my %entries;
|
|
|
|
|
|
|
|
ENTRY: foreach my $po (@$entries) {
|
|
|
|
my $msgid = $po->msgid;
|
|
|
|
if (!defined( $msgid )) {
|
|
|
|
# The entry had no msgid line in the input file.
|
|
|
|
# &load_file_asarray already warned about that. Such
|
|
|
|
# entries have no identity and cannot be put in the hash.
|
|
|
|
next ENTRY;
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
2006-02-11 17:33:11 -05:00
|
|
|
# The hash keys were quoted strings in Locale-PO-0.16,
|
|
|
|
# so keep them that way. However, if the same string has
|
|
|
|
# been quoted in two different ways, there should not be
|
|
|
|
# two different hash entries. Canonicalize the key.
|
|
|
|
my $key = $po->quote( $po->dequote( $msgid ));
|
|
|
|
if (exists( $entries{$key} ) && $entries{$key}->msgstr =~ /\w/) {
|
|
|
|
# This msgid has a translation already. Don't replace it.
|
|
|
|
next ENTRY;
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
2006-02-11 17:33:11 -05:00
|
|
|
$entries{$key} = $po;
|
|
|
|
}
|
2006-01-01 16:32:42 -05:00
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
return \%entries;
|
|
|
|
}
|
2006-01-01 16:32:42 -05:00
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
sub load_file {
|
|
|
|
my $self = shift; # normally called as a class method
|
|
|
|
my $file = shift;
|
|
|
|
my $ashash = shift;
|
2006-01-01 16:32:42 -05:00
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
if ($ashash) {
|
|
|
|
return $self->load_file_ashash($file);
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
2006-02-11 17:33:11 -05:00
|
|
|
else {
|
|
|
|
return $self->load_file_asarray($file);
|
2006-01-01 16:32:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Autoload methods go after =cut, and are processed by the autosplit program.
|
|
|
|
|
|
|
|
1;
|
|
|
|
__END__
|
|
|
|
|
|
|
|
# Below is the stub of documentation for your module. You better edit it!
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
Locale::PO - Perl module for manipulating .po entries from GNU gettext
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
use Locale::PO;
|
|
|
|
|
|
|
|
$po = new Locale::PO([-option=>value,...])
|
2006-02-12 10:16:35 -05:00
|
|
|
|
|
|
|
# get fields # set fields
|
|
|
|
$fully_quoted = $po->msgid; $po->msgid($backslashed);
|
|
|
|
$fully_quoted = $po->msgid_plural; $po->msgid_plural($backslashed);
|
|
|
|
$fully_quoted = $po->msgstr; $po->msgstr($backslashed);
|
2006-02-12 12:25:27 -05:00
|
|
|
$lineno = $po->msgstr_begin_lineno;
|
2006-02-12 10:16:35 -05:00
|
|
|
$fully_quoted_href = $po->msgstr_n; $po->msgstr_n($backslashed_href);
|
|
|
|
$string = $po->comment; $po->comment($string);
|
|
|
|
$string = $po->automatic; $po->automatic($string);
|
|
|
|
$string = $po->reference; $po->reference($string);
|
|
|
|
$flag = $po->fuzzy; $po->fuzzy($flag);
|
|
|
|
$flag = $po->obsolete; $po->obsolete($flag);
|
|
|
|
$tristate = $po->c_format; $po->c_format($tristate);
|
|
|
|
$tristate = $po->php_format; $po->php_format($tristate);
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
print $po->dump;
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$fully_quoted_string = $po->quote($backslashed_string);
|
|
|
|
$backslashed_string = $po->dequote($fully_quoted_string);
|
2006-01-01 16:32:42 -05:00
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$aref = Locale::PO->load_file_asarray($filename);
|
|
|
|
$href = Locale::PO->load_file_ashash($filename);
|
|
|
|
$ref = Locale::PO->load_file($filename, $ashash);
|
|
|
|
Locale::PO->save_file_fromarray($filename, $aref);
|
|
|
|
Locale::PO->save_file_fromhash($filename, $href);
|
|
|
|
Locale::PO->save_file($filename, $ref, $fromhash);
|
2006-01-01 16:32:42 -05:00
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
This module simplifies management of GNU gettext .po files and is an
|
|
|
|
alternative to using emacs po-mode. It provides an object-oriented
|
|
|
|
interface in which each entry in a .po file is a Locale::PO object.
|
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
=head2 Levels of Quoting
|
|
|
|
|
|
|
|
When you use methods of Locale::PO, you need to distinguish between
|
|
|
|
three possible levels of quoting strings. These levels are described
|
|
|
|
below. The descriptions also list how the following sample msgid
|
|
|
|
string would be quoted in each level.
|
|
|
|
|
|
|
|
msgid "The characters /\\ denote the \"AND\" operator.\n"
|
|
|
|
"Please enter all numbers in octal."
|
|
|
|
|
|
|
|
=over
|
|
|
|
|
|
|
|
=item FULLY-QUOTED
|
|
|
|
|
|
|
|
The same format as in a PO file: the string may consist of multiple
|
|
|
|
lines, and each line has double-quote characters around it and may
|
|
|
|
contain backslash sequences. Any double-quote or backslash characters
|
|
|
|
that should be part of the data must be escaped with backslashes.
|
|
|
|
|
|
|
|
The example in Perl syntax: C<qq("The characters /\\\\ denote the
|
|
|
|
\\"AND\\" operator.\\n"\n"Please enter all numbers in octal.")>
|
|
|
|
|
|
|
|
=item BACKSLASHED
|
|
|
|
|
|
|
|
The string may consist of multiple lines. The lines do not have
|
|
|
|
double-quote characters around them, but they may contain backslash
|
|
|
|
sequences. Any backslash characters that should be part of the data
|
|
|
|
must be escaped with backslashes, but double-quote characters must not.
|
|
|
|
|
|
|
|
The example in Perl syntax: C<qq(The characters /\\\\ denote the
|
|
|
|
"AND" operator.\\n\nPlease enter all numbers in octal.)>
|
|
|
|
|
|
|
|
=item NOT-QUOTED
|
|
|
|
|
|
|
|
This is the format that a C program would pass to the C<gettext>
|
|
|
|
function or output to a terminal device. Any remaining quotes are
|
|
|
|
part of the data itself, and backslash escapes have been replaced
|
|
|
|
with control characters. Locale::PO does not currently support
|
|
|
|
this quoting level.
|
|
|
|
|
|
|
|
The example in Perl syntax: C<qq(The characters /\\ denote the "AND"
|
|
|
|
operator.\nPlease enter all numbers in octal.)>
|
|
|
|
|
|
|
|
=back
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
=head1 METHODS
|
|
|
|
|
|
|
|
=over 4
|
|
|
|
|
|
|
|
=item new
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
my Locale::PO $po = new Locale::PO;
|
|
|
|
my Locale::PO $po = new Locale::PO(%options);
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
Create a new Locale::PO object to represent a po entry.
|
|
|
|
You can optionally set the attributes of the entry by passing
|
|
|
|
a list/hash of the form:
|
|
|
|
|
|
|
|
-option=>value, -option=>value, etc.
|
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
Where options are msgid, msgstr, comment, automatic, reference, fuzzy,
|
|
|
|
c-format, and no-c-format. See accessor methods below. Currently, you
|
|
|
|
cannot set the "obsolete" or "php-format" flags via options.
|
2006-01-01 16:32:42 -05:00
|
|
|
|
|
|
|
To generate a po file header, add an entry with an empty
|
|
|
|
msgid, like this:
|
|
|
|
|
|
|
|
$po = new Locale::PO(-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");
|
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
Note that the C<msgid> and C<msgstr> must be in BACKSLASHED form.
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
=item msgid
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$fully_quoted_string = $po->msgid;
|
|
|
|
# $backslashed_string = $po->dequote($fully_quoted_string);
|
|
|
|
$po->msgid($backslashed_string);
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
Set or get the untranslated string from the object.
|
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
The value is C<undef> if there is no such string for this message, not
|
|
|
|
even an empty string. If the message was loaded from a PO file, this
|
|
|
|
can occur if there are comments after the last C<msgid> line. The
|
|
|
|
loading functions warn about such omissions.
|
|
|
|
|
|
|
|
This method expects the new string in BACKSLASHED form
|
|
|
|
but returns the current string in FULLY-QUOTED form.
|
2006-01-05 20:26:26 -05:00
|
|
|
|
2006-02-12 12:25:27 -05:00
|
|
|
=item msgid_begin_lineno
|
|
|
|
|
|
|
|
$line_number = $po->msgid_begin_lineno;
|
|
|
|
|
|
|
|
Get the line number at which the C<msgid> string begins in the PO file.
|
|
|
|
This is undef if the entry was not loaded from a file.
|
|
|
|
There is currently no setter method for this field.
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
=item msgid_plural
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$fully_quoted_string = $po->msgid_plural;
|
|
|
|
# $backslashed_string = $po->dequote($fully_quoted_string);
|
|
|
|
$po->msgid_plural($backslashed_string);
|
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
Set or get the untranslated plural string from the object. The value
|
|
|
|
is C<undef> if there is no plural form for this message.
|
2006-01-01 16:32:42 -05:00
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
This method expects the new string in BACKSLASHED form
|
|
|
|
but returns the current string in FULLY-QUOTED form.
|
2006-01-05 20:26:26 -05:00
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
=item msgstr
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$fully_quoted_string = $po->msgstr;
|
|
|
|
# $backslashed_string = $po->dequote($fully_quoted_string);
|
|
|
|
$po->msgstr($backslashed_string);
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
Set or get the translated string from the object.
|
|
|
|
|
2006-02-12 12:25:27 -05:00
|
|
|
If the string has plural forms, then they are instead accessible via
|
|
|
|
C<msgstr_n>, and C<msgstr> normally returns C<undef>. However, if the
|
|
|
|
entry has been loaded from an incorrectly formatted PO file, then it
|
|
|
|
is also possible that both C<msgstr> and C<msgstr_n> return C<undef>,
|
|
|
|
or that they both return defined values. The loading functions warn
|
|
|
|
about such transgressions.
|
2006-02-11 17:33:11 -05:00
|
|
|
|
|
|
|
This method expects the new string in BACKSLASHED form
|
|
|
|
but returns the current string in FULLY-QUOTED form.
|
2006-01-05 20:26:26 -05:00
|
|
|
|
2006-02-12 12:25:27 -05:00
|
|
|
=item msgstr_begin_lineno
|
|
|
|
|
|
|
|
$line_number = $po->msgstr_begin_lineno;
|
|
|
|
|
|
|
|
Get the line number at which the C<msgstr> string begins in the PO file.
|
|
|
|
This is undef if the entry was not loaded from a file.
|
|
|
|
There is currently no setter method for this field.
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
=item msgstr_n
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$fully_quoted_hashref = $po->msgstr_n;
|
|
|
|
# $backslashed_hashref = { %$fully_quoted_hashref };
|
|
|
|
# $_ = $po->dequote($_) foreach values(%$backslashed_hashref);
|
|
|
|
$po->msgstr_n($backslashed_hashref);
|
|
|
|
|
2006-01-05 20:02:06 -05:00
|
|
|
Get or set the translations if there are plurals involved. Takes and
|
2006-01-01 16:32:42 -05:00
|
|
|
returns a hashref where the keys are the 'N' case and the values are
|
|
|
|
the strings. eg:
|
|
|
|
|
|
|
|
$po->msgstr_n(
|
|
|
|
{
|
|
|
|
0 => 'found %d plural translations',
|
|
|
|
1 => 'found %d singular translation',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
The value C<undef> should be treated the same as an empty hash.
|
2006-02-12 10:16:35 -05:00
|
|
|
Callers should neither modify the hash to which the returned reference
|
|
|
|
points, nor assume it to remain valid if C<msgstr_n> is later called
|
|
|
|
to install a new set of translations.
|
2006-02-11 17:33:11 -05:00
|
|
|
|
|
|
|
This method expects the new strings in BACKSLASHED form
|
|
|
|
but returns the current strings in FULLY-QUOTED form.
|
2006-01-05 20:26:26 -05:00
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
=item comment
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$string = $po->comment;
|
|
|
|
$po->comment($string);
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
Set or get translator comments from the object.
|
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
If there are no such comments, then the value is C<undef>. Otherwise,
|
2006-01-05 20:26:26 -05:00
|
|
|
the value is a string that contains the comment lines delimited with
|
|
|
|
"\n". The string includes neither the S<"# "> at the beginning of
|
|
|
|
each comment line nor the newline at the end of the last comment line.
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
=item automatic
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$string = $po->automatic;
|
|
|
|
$po->automatic($string);
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
Set or get automatic comments from the object (inserted by
|
|
|
|
emacs po-mode or xgettext).
|
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
If there are no such comments, then the value is C<undef>. Otherwise,
|
2006-01-05 20:26:26 -05:00
|
|
|
the value is a string that contains the comment lines delimited with
|
|
|
|
"\n". The string includes neither the S<"#. "> at the beginning of
|
|
|
|
each comment line nor the newline at the end of the last comment line.
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
=item reference
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$string = $po->reference;
|
|
|
|
$po->reference($string);
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
Set or get reference marking comments from the object (inserted
|
|
|
|
by emacs po-mode or gettext).
|
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
If there are no such comments, then the value is C<undef>. Otherwise,
|
2006-01-05 20:26:26 -05:00
|
|
|
the value is a string that contains the comment lines delimited with
|
|
|
|
"\n". The string includes neither the S<"#: "> at the beginning of
|
|
|
|
each comment line nor the newline at the end of the last comment line.
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
=item fuzzy
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$flag = $po->fuzzy;
|
|
|
|
$po->fuzzy($flag);
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
Set or get the fuzzy flag on the object ("check this translation").
|
|
|
|
When setting, use 1 to turn on fuzzy, and 0 to turn it off.
|
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
=item obsolete
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$flag = $po->obsolete;
|
|
|
|
$po->obsolete($flag);
|
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
Set or get the obsolete flag on the object ("no longer used").
|
|
|
|
When setting, use 1 to turn on obsolete, and 0 to turn it off.
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
=item c_format
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$tristate = $po->obsolete;
|
|
|
|
$po->obsolete($tristate);
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
Set or get the c-format or no-c-format flag on the object.
|
|
|
|
This can take 3 values: 1 implies c-format, 0 implies no-c-format,
|
|
|
|
and blank or undefined implies neither.
|
|
|
|
|
2006-01-07 12:45:54 -05:00
|
|
|
=item php_format
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$tristate = $po->php_format;
|
|
|
|
$po->php_format($tristate);
|
|
|
|
|
2006-01-07 12:45:54 -05:00
|
|
|
Set or get the php-format or no-php-format flag on the object.
|
|
|
|
This can take 3 values: 1 implies php-format, 0 implies no-php-format,
|
|
|
|
and blank or undefined implies neither.
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
=item dump
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
print $po->dump;
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
Returns the entry as a string, suitable for output to a po file.
|
|
|
|
|
|
|
|
=item quote
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$fully_quoted_string = $po->quote($backslashed_string);
|
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
Converts a string from BACKSLASHED to FULLY-QUOTED form.
|
|
|
|
Specifically, the quoted string will have all existing double-quote
|
|
|
|
characters escaped by backslashes, and each line will be enclosed in
|
|
|
|
double quotes. Preexisting backslashes will not be doubled.
|
2006-01-01 16:32:42 -05:00
|
|
|
|
|
|
|
=item dequote
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$backslashed_string = $po->dequote($fully_quoted_string);
|
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
Converts a string from FULLY-QUOTED to BACKSLASHED form.
|
|
|
|
Specifically, it first removes the double-quote characters that
|
|
|
|
surround each line. After this, each remaining double-quote character
|
|
|
|
should have a backslash in front of it; the method then removes those
|
|
|
|
backslashes. Backslashes in any other position will be left intact.
|
2006-01-01 16:32:42 -05:00
|
|
|
|
|
|
|
=item load_file_asarray
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$arrayref = Locale::PO->load_file_asarray($filename);
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
Given the filename of a po-file, reads the file and returns a
|
|
|
|
reference to a list of Locale::PO objects corresponding to the contents of
|
|
|
|
the file, in the same order.
|
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
If the file cannot be read, then this method returns C<undef>, and you
|
|
|
|
can check C<$!> for the actual error. If the file does not follow the
|
|
|
|
expected syntax, then the method generates warnings but keeps going.
|
|
|
|
Other errors (e.g. out of memory) may result in C<die> being called,
|
|
|
|
of course.
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
=item load_file_ashash
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$hashref = Locale::PO->load_file_ashash($filename);
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
Given the filename of a po-file, reads the file and returns a
|
|
|
|
reference to a hash of Locale::PO objects corresponding to the contents of
|
2006-02-11 17:33:11 -05:00
|
|
|
the file. The hash keys are the untranslated strings (in BACKSLASHED form),
|
|
|
|
so this is a cheap way to remove duplicates. The method will prefer to keep
|
|
|
|
entries that have been translated.
|
|
|
|
|
|
|
|
This handles errors in the same way as C<load_file_asarray> does.
|
2006-01-01 16:32:42 -05:00
|
|
|
|
2006-02-05 12:16:06 -05:00
|
|
|
=item load_file
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$ref = Locale::PO->load_file($filename, $ashash);
|
|
|
|
|
2006-02-05 12:16:06 -05:00
|
|
|
This method behaves as C<load_file_asarray> if the C<$ashash>
|
|
|
|
parameter is 0, or as C<load_file_ashash> if C<$ashash> is 1.
|
|
|
|
Your code will probably be easier to understand if you call either
|
|
|
|
of those methods instead of this one.
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
=item save_file_fromarray
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$ok = Locale::PO->save_file_fromarray($filename, \@objects)
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
Given a filename and a reference to a list of Locale::PO objects,
|
|
|
|
saves those objects to the file, creating a po-file.
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
Returns true if successful.
|
|
|
|
Returns C<undef> and sets C<$!> if an I/O error occurs.
|
|
|
|
Dies if a serious error occurs.
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
=item save_file_fromhash
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$ok = Locale::PO->save_file_fromhash($filename, \%objects);
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
Given a filename and a reference to a hash of Locale::PO objects,
|
|
|
|
saves those objects to the file, creating a po-file. The entries
|
|
|
|
are sorted alphabetically by untranslated string.
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
Returns true if successful.
|
|
|
|
Returns C<undef> and sets C<$!> if an I/O error occurs.
|
|
|
|
Dies if a serious error occurs.
|
|
|
|
|
2006-02-05 12:16:06 -05:00
|
|
|
=item save_file
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
$ok = Locale::PO->save_file($filename, $ref, $fromhash);
|
|
|
|
|
2006-02-05 12:16:06 -05:00
|
|
|
This method behaves as C<save_file_fromarray> if the C<$fromhash>
|
|
|
|
parameter is 0, or as C<save_file_fromhash> if C<$fromhash> is 1.
|
|
|
|
Your code will probably be easier to understand if you call either
|
|
|
|
of those methods instead of this one.
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
=back
|
|
|
|
|
|
|
|
=head1 AUTHOR
|
|
|
|
|
|
|
|
Alan Schwartz, alansz@pennmush.org
|
|
|
|
|
2006-01-05 20:02:06 -05:00
|
|
|
=head1 DIFFERENCES FROM Locale-PO-0.16
|
|
|
|
|
|
|
|
List of changes in this file, as stipulated in section 3. of the
|
|
|
|
L<"Artistic License"|perlartistic> and subsection 2. a) of L<GNU
|
|
|
|
General Public License version 2|perlgpl>:
|
|
|
|
|
|
|
|
=over 4
|
|
|
|
|
|
|
|
=item Z<>2006-01-06 Kalle Olavi Niemitalo <kon@iki.fi>
|
|
|
|
|
2006-01-08 07:25:04 -05:00
|
|
|
Appended ".kon" to C<$VERSION>.
|
|
|
|
Use C<fields>, and C<my Locale::PO> where applicable.
|
|
|
|
|
|
|
|
POD changes:
|
|
|
|
Added the copyright notice (from F<README>) and this history.
|
2006-01-05 20:02:06 -05:00
|
|
|
Corrected a typo in the documentation.
|
2006-01-05 20:26:26 -05:00
|
|
|
Documented quoting in the C<msgid>, C<msgid_plural>, C<msgstr>, and C<msgstr_n> methods.
|
|
|
|
Documented newlines in the C<comment>, C<automatic>, and C<reference> methods.
|
2006-01-05 20:02:06 -05:00
|
|
|
|
2006-01-07 12:45:54 -05:00
|
|
|
=item Z<>2006-01-07 Kalle Olavi Niemitalo <kon@iki.fi>
|
|
|
|
|
2006-01-08 07:25:04 -05:00
|
|
|
POD changes:
|
2006-01-07 12:45:54 -05:00
|
|
|
Documented the C<php_format> method.
|
|
|
|
|
2006-01-08 07:25:04 -05:00
|
|
|
=item Z<>2006-01-08 Kalle Olavi Niemitalo <kon@iki.fi>
|
|
|
|
|
|
|
|
POD changes:
|
|
|
|
Greatly expanded the L</BUGS> section.
|
|
|
|
Reformatted this list of changes.
|
|
|
|
|
2006-02-05 10:47:36 -05:00
|
|
|
=item Z<>2006-02-05 Kalle Olavi Niemitalo <kon@iki.fi>
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
Added comments about the fields of Locale::PO objects.
|
2006-02-11 17:33:11 -05:00
|
|
|
The C<load_file> function binds C<$/> and C<$_> dynamically.
|
|
|
|
Renamed C<normalize_str> to C<_normalize_str>, and C<dump_multi_comment> to C<_dump_multi_comment>.
|
2006-02-05 10:47:36 -05:00
|
|
|
|
2006-02-05 12:16:06 -05:00
|
|
|
POD changes:
|
|
|
|
Documented C<load_file> and C<save_file>.
|
|
|
|
|
2006-02-11 17:33:11 -05:00
|
|
|
=item Z<>2006-02-11 Kalle Olavi Niemitalo <kon@iki.fi>
|
|
|
|
|
|
|
|
Rewrote the file parser. It now warns about various inconsistencies, and no longer relies on empty lines.
|
|
|
|
The parser writes directly to fields of Locale::PO objects, preserving internal newlines in strings. Changed C<dequote> to process all lines of multi-line strings.
|
|
|
|
The parser supports obsolete entries. Added the C<_obsolete> field and the C<obsolete> method. C<dump> comments the entry out if it is obsolete.
|
|
|
|
The parser preserves the original string of flags and scans it more carefully. Added the C<_flag> field. Implemented "no-php-format".
|
|
|
|
C<dump> dumps comments even if they are C<eq "0">.
|
|
|
|
|
|
|
|
POD changes:
|
|
|
|
Documented the bugs fixed with the changes above.
|
2006-02-12 10:16:35 -05:00
|
|
|
Documented levels of quoting, and the exact behaviour of C<quote> and C<dequote>.
|
2006-02-11 17:33:11 -05:00
|
|
|
Documented the C<obsolete> method.
|
|
|
|
Documented error handling in C<load_file_asarray> and C<load_file_ashash>.
|
|
|
|
Documented when C<msgid>, C<msgstr> etc. can return C<undef>.
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
=item Z<>2006-02-12 Kalle Olavi Niemitalo <kon@iki.fi>
|
|
|
|
|
|
|
|
C<save_file> returns C<undef> and remembers C<$!> if C<print> fails.
|
2006-02-12 12:25:27 -05:00
|
|
|
C<load_file_asarray> saves line numbers of C<msgid> and C<msgstr>. New fields C<_msgid_begin_lineno> and C<_msgstr_begin_lineno>; new methods C<msgid_begin_lineno> and C<msgstr_begin_lineno>.
|
2006-02-12 10:16:35 -05:00
|
|
|
|
|
|
|
POD changes:
|
|
|
|
Revised the synopses of the methods, paying attention to levels of quoting.
|
|
|
|
Repeat the synopsis above the description of each method.
|
|
|
|
Never write C<< CZ<><Locale::PO> >>; it looks bad in B<pod2text>.
|
2006-02-12 12:25:27 -05:00
|
|
|
Documented that C<msgstr> normally returns C<undef> if there are plurals.
|
|
|
|
Documented the new methods C<msgid_begin_lineno> and C<msgstr_begin_lineno>.
|
2006-02-12 10:16:35 -05:00
|
|
|
|
2006-02-18 13:30:40 -05:00
|
|
|
=item Z<>2006-02-28 Kalle Olavi Niemitalo <kon@iki.fi>
|
|
|
|
|
|
|
|
Locale::PO now preserves unrecognized flags, although there is still no documented way to access them. It also preserves the order of flags, if no flags are modified. Replaced the C<fuzzy>, C<c_format>, and C<php_format> fields with C<_flaghash>, and renamed the C<_flag> field to C<_flagstr>.
|
|
|
|
Flag-setting functions silently map unsupported values (e.g. 42) to supported ones (e.g. 1), which they also return.
|
|
|
|
The C<c_format> and C<php_format> methods treat empty strings as C<undef>, rather than as 0.
|
|
|
|
Names of flags are case-sensitive, like in GNU Gettext.
|
|
|
|
|
|
|
|
POD changes:
|
|
|
|
Unlisted the bugs that have now been fixed.
|
|
|
|
|
2006-01-05 20:02:06 -05:00
|
|
|
=back
|
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE
|
|
|
|
|
|
|
|
Copyright (c) 2000-2004 Alan Schwartz <alansz@pennmush.org>.
|
|
|
|
All rights reserved. This program is free software; you can
|
|
|
|
redistribute it and/or modify it under the same terms as Perl itself.
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
=head1 BUGS
|
|
|
|
|
2006-02-12 10:16:35 -05:00
|
|
|
If you C<load_file> then C<save_file>, the output file may have slight
|
2006-01-01 16:32:42 -05:00
|
|
|
cosmetic differences from the input file (an extra blank line here or there).
|
|
|
|
|
2006-01-08 07:39:18 -05:00
|
|
|
The C<quote> and C<dequote> methods assume Perl knows the encoding
|
|
|
|
of the string. If it doesn't, they'll treat each 0x5C byte as a
|
|
|
|
backslash even if it's actually part of a multibyte character.
|
|
|
|
Therefore, Locale::PO should parse the charset parameter from the
|
|
|
|
header entry, and decode the strings with that. It is unclear whether
|
|
|
|
the charset must be decoded even before the newlines and quotes are
|
|
|
|
parsed; this would mainly be a requirement with UTF-16, which GNU
|
|
|
|
Gettext doesn't support.
|
|
|
|
|
2006-01-08 07:25:04 -05:00
|
|
|
=head2 Almost Bugs
|
|
|
|
|
2006-02-12 14:48:43 -05:00
|
|
|
C<msgid_begin_lineno> and C<msgstr_begin_lineno> are read-only.
|
|
|
|
Perhaps there should also be ways to get the line numbers of the
|
|
|
|
other strings. Probably not line numbers of comments, though.
|
|
|
|
|
|
|
|
The C<msgid>, C<msgid_plural>, C<msgstr>, and C<msgstr_n> methods
|
|
|
|
output FULLY-QUOTED strings, but they expect BACKSLASHED strings as
|
|
|
|
input. It would be better to have both FULLY-QUOTED or both
|
|
|
|
BACKSLASHED; or perhaps C<< $po->msgid(-level => 'BACKSLASHED') >>.
|
2006-01-08 07:25:04 -05:00
|
|
|
|
|
|
|
Locale::PO discards all types of comments it does not recognize.
|
|
|
|
The B<msgmerge> program of GNU gettext-tools 0.14.3 does the same,
|
|
|
|
so perhaps it's not a problem.
|
|
|
|
|
2006-01-01 16:32:42 -05:00
|
|
|
=head1 SEE ALSO
|
|
|
|
|
|
|
|
xgettext(1).
|
|
|
|
|
|
|
|
=cut
|