NAME

Locale::MO::File - Write/read gettext MO files

$Id: File.pm 645 2018-12-24 10:16:32Z steffenw $

$HeadURL: svn+ssh://steffenw@svn.code.sf.net/p/dbd-po/code/Locale-MO-File/trunk/lib/Locale/MO/File.pm $

VERSION

0.09

SYNOPSIS

require Locale::MO::File;

my $mo = Locale::MO::File->new(
    filename => $filename,
    ...
    messages => [
        {
            msgid  => 'original',
            msgstr => 'translation',
            ...
        },
        ...
    ],
});
$mo->write_file;

$mo->read_file;
my $messages = $self->get_messages;

DESCRIPTION

The module allows to write or read gettext MO files.

Data to write are expected as array reference of hash references. Read data are stored in an array reference too.

Reading and writing is also available using an already open file handle. A given file handle will used but not closed.

Set encoding, newline and byte order to be compatible.

SUBROUTINES/METHODS

method new

This is the constructor method. All parameters are optional.

my $mo = Locale::MO::File->new(
    filename      => $string,
    file_handle   => $file_handle, # filename expected for error messages only
    encoding      => $string,      # e.g. 'UTF-8', if not set: bytes
    newline       => $string,      # e.g. $CRLF or "\n", if not set: no change
    is_big_endian => $boolean,     # if not set: little endian
    messages      => $arrayref,    # default []
);

methods to modify an existing object

set_filename, get_filename, clear_filename

Modification of attribute filename.

$mo->set_filename($string);
$string = $mo->get_filename;
$mo->clear_filename;

set_file_handle, get_file_handle, clear_file_handle

Modification of attribute file_handle.

set_encoding, get_encoding, clear_encoding

Modification of attribute encoding.

set_newline, get_newline, clear_newline

Modification of attribute newline.

set_is_big_endian, is_big_endian, clear_is_big_endian

Modification of attribute is_big_endian. Only needed to write files.

method set_messages, get_messages

Modification of attribute messages.

$mo->set_messages([
    # header
    {
        msgid   => q{},
        msgstr  => $header,
    },
    # typical
    {
        msgid   => $original,
        msgstr  => $translation,
    },
    # context
    {
        msgctxt => $context,
        msgid   => $original,
        msgstr  => $translation,
    },
    # plural
    {
        msgid         => $original_singular,
        msgid_plural  => $original_plural,
        msgstr_plural => [ $tanslation_0, ..., $translation_n ],
    },
    # context + plural
    {
        msgctxt       => $context,
        msgid         => $original_singular,
        msgid_plural  => $original_plural,
        msgstr_plural => [ $tanslation_0, ..., $translation_n ],
    },
]);

method write_file

The content of the "messages" array reference is first sorted and then written. So the header is always on top. The transferred "messages" array reference remains unchanged.

$mo->write_file;

method read_file

Big endian or little endian will be detected automaticly. The read data will be stored in attribute messages.

$mo = read_file;
my $messages = $mo->get_messages;

EXAMPLE

Inside of this distribution is a directory named example. Run the *.pl files.

DIAGNOSTICS

Full validation of messages array reference using Params::Validate.

CONFIGURATION AND ENVIRONMENT

none

DEPENDENCIES

charnames

namespace::autoclean

Carp

Const::Fast

Encode

English

IO::File

Moo

MooX::StrictConstructor

MooX::Types::MooseLike::Base

Params::Validate

INCOMPATIBILITIES

not known

BUGS AND LIMITATIONS

Hashing table not written of this module version. So very slim MO files are the result.

SEE ALSO

http://www.gnu.org/software/hello/manual/gettext/MO-Files.html

AUTHOR

Steffen Winkler

LICENSE AND COPYRIGHT

Copyright (c) 2011 - 2018, Steffen Winkler <steffenw at cpan.org>. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.