From f0a9c930a9e16efb964190cdb9059610668b098e Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 24 Feb 2010 20:49:45 -0600 Subject: pretty major rewrite, now based on Locale::Maketext::Lexicon --- lib/Locale/POFileManager.pm | 20 +++++-- lib/Locale/POFileManager/File.pm | 112 ++++++++++++++++++--------------------- 2 files changed, 69 insertions(+), 63 deletions(-) (limited to 'lib') diff --git a/lib/Locale/POFileManager.pm b/lib/Locale/POFileManager.pm index b32b076..54317ca 100644 --- a/lib/Locale/POFileManager.pm +++ b/lib/Locale/POFileManager.pm @@ -154,9 +154,8 @@ sub stub_msgstr { my %args = @_; my $canonical_msgstr; $canonical_msgstr = - $weakself->canonical_language_file->entry_for($args{msgid})->msgstr + $weakself->canonical_language_file->msgstr($args{msgid}) if $weakself; - $canonical_msgstr =~ s/^"|"$//g if defined($canonical_msgstr); return $msgstr->( %args, defined($canonical_msgstr) ? (canonical_msgstr => $canonical_msgstr) : (), @@ -200,13 +199,26 @@ sub add_language { confess("Can't overwrite existing language file for $lang") if -e $file->stringify; + my $canon_pofile = $self->canonical_language_file; + + my $fh = $file->openw; + $fh->binmode(':utf8'); + $fh->print(qq{msgid ""\n}); + $fh->print(qq{msgstr ""\n}); + for my $header_key ($canon_pofile->headers) { + $fh->print(qq{"$header_key: } + . $canon_pofile->header($header_key) + . qq{\\n"\n}); + } + $fh->print(qq{\n}); + $fh->close; + my $msgstr = $self->stub_msgstr; my $pofile = Locale::POFileManager::File->new( file => $file, defined($msgstr) ? (stub_msgstr => $msgstr) : (), ); - $pofile->add_entry($self->canonical_language_file->entry_for('')); - $pofile->save; + $self->_add_file($pofile); } diff --git a/lib/Locale/POFileManager/File.pm b/lib/Locale/POFileManager/File.pm index 196ab41..b024c8c 100644 --- a/lib/Locale/POFileManager/File.pm +++ b/lib/Locale/POFileManager/File.pm @@ -4,9 +4,13 @@ use Moose; use MooseX::Types::Path::Class qw(File); use List::MoreUtils qw(any); use List::Util qw(first); -use Locale::PO; +use Locale::Maketext::Lexicon::Gettext; use Scalar::Util qw(reftype); +require Locale::Maketext::Lexicon; +Locale::Maketext::Lexicon::set_option(decode => 1); +Locale::Maketext::Lexicon::set_option(allow_empty => 1); + =head1 NAME Locale::POFileManager::File - A single .po file @@ -83,82 +87,74 @@ has stub_msgstr => ( isa => 'Str|CodeRef', ); -=head2 entries - -Returns a list of L objects corresponding to translation entries in -the file. - -=cut - -=head2 add_entry - -Adds a new translation entry to the file. This can be provided either as a -L object directly, or as a hash of options to pass to the -L constructor (except without the leading dashes). - -=cut - =head2 msgids Returns a list of msgids found in the file. =cut -has entries => ( - traits => [qw(Array)], - isa => 'ArrayRef[Locale::PO]', - lazy => 1, - builder => '_build_entries', - init_arg => undef, - handles => { - entries => 'elements', - _add_entry => 'push', - msgids => - [ map => sub { my $m = $_->msgid; $m =~ s/^"|"$//g; $m } ], +has lexicon => ( + traits => [qw(Hash)], + isa => 'HashRef', + lazy => 1, + default => sub { + my $self = shift; + return Locale::Maketext::Lexicon::Gettext->parse($self->file->slurp); + }, + handles => { + msgids => 'keys', + has_msgid => 'exists', + _remove_msgid => 'delete', + msgstr => 'get', + _add_lexicon_entry => 'set', }, ); -sub _build_entries { +has headers => ( + traits => [qw(Hash)], + isa => 'HashRef', + lazy => 1, + default => sub { + my $self = shift; + my $ret = {}; + $ret->{$_} = $self->_remove_msgid("__$_") + for map { s/^__//; $_ } grep { /^__/ } $self->msgids; + return $ret; + }, + handles => { + headers => 'keys', + header => 'get', + }, +); + +sub BUILD { my $self = shift; + my $filename = $self->file->stringify; + confess "Can't read file " . $filename + unless -r $filename; - return (-r $filename) ? Locale::PO->load_file_asarray($filename) : []; + # strip the headers out of the lexicon hash + $self->headers; } sub add_entry { my $self = shift; - if (@_ == 1) { - $self->_add_entry($_[0]); - } - else { - my %args = @_; - $args{"-$_"} = delete $args{$_} for keys %args; - $self->_add_entry(Locale::PO->new(%args)); - } -} - -=head2 entry_for - -Returns the L object corresponding to the given msgid. - -=cut + my %args = @_; + my ($msgid, $msgstr) = @args{qw(msgid msgstr)}; -sub entry_for { - my $self = shift; - my ($msgid) = @_; - return first { $_->msgid eq '"' . $msgid . '"' } $self->entries; -} + return if $self->has_msgid($msgid); -=head2 save + my $needs_newline = ($self->file->slurp !~ /\n\n$/); + my $fh = $self->file->open('>>'); + $fh->binmode(':utf8'); + $fh->print(qq{\n}) if $needs_newline; -Writes the current contents of the file back out to disk. - -=cut + $fh->print(qq{msgid "$msgid"\n}); + $fh->print(qq{msgstr "$msgstr"\n}) if defined $msgstr; + $fh->print(qq{\n}); -sub save { - my $self = shift; - - Locale::PO->save_file_fromarray($self->file->stringify, [$self->entries]); + $self->_add_lexicon_entry($msgid => $msgstr); } =head2 language @@ -218,8 +214,6 @@ sub add_stubs_from { defined($msgstr) ? (msgstr => $msgstr) : (), ); } - - $self->save; } __PACKAGE__->meta->make_immutable; -- cgit v1.2.3-54-g00ecf