summaryrefslogtreecommitdiffstats
path: root/lib/Locale/POFileManager/File.pm
blob: b3aa78befe511e7e0deb74a25398c8a0652afb8c (plain) (blame)
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
package Locale::POFileManager::File;
use Moose;

use MooseX::Types::Path::Class qw(File);
use List::MoreUtils qw(any);
use Locale::PO;

has file => (
    is       => 'ro',
    isa      => File,
    coerce   => 1,
    required => 1,
);

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 { $_->msgid } ],
    },
);

sub _build_entries {
    my $self = shift;
    my $filename = $self->file->stringify;

    return (-r $filename) ? Locale::PO->load_file_asarray($filename) : [];
}

sub save {
    my $self = shift;

    Locale::PO->save_file_fromarray($self->file->stringify, [$self->entries]);
}

sub find_missing_from {
    my $self = shift;
    my ($other) = @_;
    $other = blessed($self)->new(file => $other) unless blessed($other);

    my @ret;
    my @msgids = $self->msgids;
    for my $msgid ($other->msgids) {
        push @ret, $msgid unless any { $msgid eq $_ } @msgids;
    }

    return @ret;
}

sub add_stubs_from {
    my $self = shift;
    my ($other) = @_;

    $self->add_entry($_) for map { Locale::PO->new(-msgid => $_) }
                                 $self->find_missing_from($other);
    $self->save;
}

__PACKAGE__->meta->make_immutable;
no Moose;

1;