
Locale::MO::File - Write/read gettext MO files
$Id: File.pm 615 2012-03-13 13:28:39Z steffenw $
$HeadURL: https://dbd-po.svn.sourceforge.net/svnroot/dbd-po/Locale-MO-File/trunk/lib/Locale/MO/File.pm $

0.03

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;

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.

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 []
);
Modification of attribute filename.
$mo->set_filename($string);
$string = $mo->get_filename;
$mo->clear_filename;
Modification of attribute file_handle.
Modification of attribute encoding.
Modification of attribute newline.
Modification of attribute is_big_endian. Only needed to write files.
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 ],
},
]);
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;
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;

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

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

none


not known

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

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

Steffen Winkler

Copyright (c) 2011 - 2012, 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.