Locale-MO-File

 view release on metacpan or  search on metacpan

lib/Locale/MO/File.pm  view on Meta::CPAN

    my ($self, $message) = @_;

    my ($msgid, $msgstr) = map {
        ( exists $message->{$_} && defined $message->{$_} )
        ? $message->{$_}
        : q{};
    } qw(msgid msgstr);

    # original
    $msgid = $self->_encode_and_replace_newline(
        (
            (
                exists $message->{msgctxt}
                && defined $message->{msgctxt}
                && length $message->{msgctxt}
            )
            ? $message->{msgctxt} . $CONTEXT_SEPARATOR . $msgid
            : $msgid
        )
        . (
            (
                exists $message->{msgid_plural}
                && defined $message->{msgid_plural}
                && length $message->{msgid_plural}
            )
            ? $PLURAL_SEPARATOR . $message->{msgid_plural}
            : q{}
        ),
    );

    # translation
    $msgstr = $self->_encode_and_replace_newline(
        length $msgstr
        ? $msgstr
        : join
            $PLURAL_SEPARATOR,
            map {
                defined $_ ? $_ : q{}
            } @{ $message->{msgstr_plural} || [] }
    );

    return {
        msgid  => $msgid,
        msgstr => $msgstr,
    };
}

sub _unpack_message {
    my ($self, $message) = @_;

    my ($msgid, $msgstr) = map {
        ( defined && length )
        ? $self->_decode_and_replace_newline($_)
        : q{};
    } @{$message}{qw(msgid msgstr)};

    # return value
    my %message;

    # split original
    my @strings = split m{ \Q$CONTEXT_SEPARATOR\E }xms, $msgid;
    if ( @strings > 1 ) {
        ( $message{msgctxt}, $msgid ) = @strings;
    }
    my @plurals = split m{ \Q$PLURAL_SEPARATOR\E }xms, $msgid;
    my $is_plural = @plurals > 1;
    if ( $is_plural ) {
        @message{qw(msgid msgid_plural)} = @plurals;
    }
    else {
        $message{msgid} = $msgid;
    }

    # split translation
    @plurals = split m{ \Q$PLURAL_SEPARATOR\E }xms, $msgstr, -1; ## no critic (MagicNumbers)
    if ( $is_plural ) {
        $message{msgstr_plural} = \@plurals;
    }
    else {
        $message{msgstr} = $plurals[0];
    }

    return \%message;
}

before 'write_file' => sub {
    my $self = shift;

    my $index = 0;
    my $chars_callback = sub {
        my $string = shift;
        STRING: for ( ref $string ? @{$string} : $string ) {
            defined
                or next STRING;
            m{ \Q$CONTEXT_SEPARATOR\E | \Q$PLURAL_SEPARATOR\E }xmso
                and return;
        }
        return 1;
    };
    for my $message ( @{ $self->get_messages } ) {
        validate_with(
            params => (
                ref $message eq 'HASH'
                ? $message
                : confess "messages[$index] is not a hash reference"
            ),
            spec => {
                msgctxt => {
                    type      => SCALAR,
                    optional  => 1,
                    callbacks => {
                        'no control chars' => $chars_callback,
                    },
                },
                msgid => {
                    type      => SCALAR,
                    optional  => 1,
                    callbacks => {
                        'no control chars' => $chars_callback,
                    },
                },
                msgid_plural => {
                    type      => SCALAR,
                    optional  => 1,
                    callbacks => {
                        'no control chars' => $chars_callback,
                    },
                },
                msgstr => {
                    type      => SCALAR,
                    optional  => 1,
                    callbacks => {
                        'no control chars' => $chars_callback,
                    },
                },



( run in 1.589 second using v1.01-cache-2.11-cpan-71847e10f99 )