NAME

Locale::PO::Utils - Utils to build/extract the PO header and anything else

$Id: Utils.pm 520 2010-07-31 06:17:39Z steffenw $

$HeadURL: https://dbd-po.svn.sourceforge.net/svnroot/dbd-po/Locale-PO-Utils/trunk/lib/Locale/PO/Utils.pm $

VERSION

0.05

SYNOPSIS

use Locale::PO::Utils;

DESCRIPTION

Utils to build/extract the PO header and anything else.

SUBROUTINES/METHODS

method new

my $obj = Locale::PO::Utils->new();

read/write header

method get_all_header_keys

This sub returns all header keys you can set or get.

$array_ref = $obj->get_all_header_keys();

or as class method

$array_ref => Locale::PO::Utils->get_all_header_keys();

The $array_ref is:

[ qw(
    Content-Transfer-Encoding
    Content-Type
    Language-Team-Mail
    Language-Team-Name
    Last-Translator-Mail
    Last-Translator-Name
    MIME-Version
    PO-Revision-Date
    POT-Creation-Date
    Plural-Forms
    Project-Id-Version
    Report-Msgid-Bugs-To-Mail
    Report-Msgid-Bugs-To-Name
    charset
    extended
) ]

method build_header_msgstr

There are more ways to do this.

minimal header

$obj->build_header_msgstr();

The result is:

MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

maximal header

$obj->build_header_msgstr({
    'Project-Id-Version'        => 'Testproject',
    'Report-Msgid-Bugs-To-Name' => 'Bug Reporter',
    'Report-Msgid-Bugs-To-Mail' => 'bug@example.org',
    'POT-Creation-Date'         => 'no POT creation date',
    'PO-Revision-Date'          => 'no PO revision date',
    'Last-Translator-Name'      => 'Steffen Winkler',
    'Last-Translator-Mail'      => 'steffenw@example.org',
    'Language-Team-Name'        => 'MyTeam',
    'Language-Team-Mail'        => 'cpan@example.org',
    'MIME-Version'              => '1.0',
    'Content-Type'              => 'text/plain',
    'charset'                   => 'utf-8',
    'Content-Transfer-Encoding' => '8bit',
    'extended'                  => [
        'X-Poedit-Language'      => 'German',
        'X-Poedit-Country'       => 'GERMANY',
        'X-Poedit-SourceCharset' => 'utf-8',
    ],
});

The result is:

Project-Id-Version: Testproject
Report-Msgid-Bugs-To: Bug Reporter <bug@example.org>
POT-Creation-Date: no POT creation date
PO-Revision-Date: no PO revision date
Last-Translator: Steffen Winkler <steffenw@example.org>
Language-Team: MyTeam <cpan@example.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
X-Poedit-Language: German
X-Poedit-Country: GERMANY
X-Poedit-SourceCharset: utf-8

method split_header_msgstr (for internal use only)

This method is internal used at method get_header_msgstr_data.

$array_ref = $obj->split_header_msgstr($msgstr);

method get_header_msgstr_data

This method extracts the values using the given keys.

single mode

$string = $obj->get_header_msgstr_data($msgstr, 'Project-Id-Version');

$string is now "Testproject".

multiple mode

$data = $obj->get_header_msgstr_data(
    $msgstr,
    [qw(Project-Id-Version Report-Msgid-Bugs-To-Mail extended)],
),

$data is now:

[
    'Testproject',
    'bug@example.org',
    [
        'X-Poedit-Language',
        'German',
        'X-Poedit-Country',
        'GERMANY',
        'X-Poedit-SourceCharset',
        'utf-8',
    ],
]

method set_plural_forms

Plural forms are defined like that for English

$obj->set_plural_forms('nplurals=2; plural=n != 1');

method calculate_plural_forms

This method sets the nplural count and the plural code in a safe way.

$obj->calculate_plural_forms();

method get_nplurals

This method get back the calculated count of plural forms. The default value before any calculation is 1.

$nplurals = $obj->get_nplurals();

method get_plural_code

This method get back the calculated code for the calculaded plural form to select the right plural. The default value before any calculation sub {return 0}.

For the example 'nplurals=2; plural=n != 1':

$plural = $obj->get_plural_code()->(0), # $plural is 1
$plural = $obj->get_plural_code()->(1), # $plural is 0
$plural = $obj->get_plural_code()->(2), # $plural is 1
$plural = $obj->get_plural_code()->(3), # $plural is 1
...

different writing of placeholders

method maketext_to_gettext

Maps maketext strings with [_1] or [quant,_1,singular,plural,zero] or [*,_1,singular,plural,zero] inside to %1 or %quant(%1,singluar,plural,zero] or %*(%1,singluar,plural,zero] inside.

$gettext_string = $obj->maketext_to_gettext($maketext_string);

or

@gettext_strings = $obj->maketext_to_gettext(@maketext_strings);

This method can called as class method too.

$gettext_string = Locale::PO::Utils->maketext_to_gettext($maketext_string);

or

@gettext_strings = Locale::PO::Utils->maketext_to_gettext(@maketext_strings);

expand placeholders

method expand_maketext

Expands strings containing maketext placeholders. If the fist parameter is true, gettext style is used instead of maketext style.

maketext style:

[_1]
[quant,_1,singular,plural,zero]
[*,_1,singular,plural,zero]

gettext style:

%1
%quant(%1,singular,plural,zero)
%*(%1,singular,plural,zero)

   $obj->set_is_gettext_style(0);
   $expanded = $obj->expand_maketext($maketext_text, @args);

   $obj->set_is_gettext_style(1);
   $expanded = $obj->expand_maketext($gettext_text, @args);

method expand_gettext

Expands strings containing gettext placeholders like {name}.

$expanded = $obj->expand_gettext($text, %args);

This method can called as class method too.

$expanded = Locale::PO::Utils->expand_gettext($text, %args);

EXAMPLE

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

DIAGNOSTICS

none

CONFIGURATION AND ENVIRONMENT

none

DEPENDENCIES

Moose

MooseX::StrictConstructor

MooseX::FollowPBP

English

Carp

Clone

Params::Validate

Safe

INCOMPATIBILITIES

not known

BUGS AND LIMITATIONS

not known

SEE ALSO

http://en.wikipedia.org/wiki/Gettext

http://translate.sourceforge.net/wiki/l10n/pluralforms

AUTHOR

Steffen Winkler

LICENSE AND COPYRIGHT

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