Locale::Utils::PluralForms - Utils to use plural forms
$Id: PluralForms.pm 382 2011-11-13 13:20:22Z steffenw $
0.001
use Locale::Utils::PluralForms;
my $obj = Locale::Utils::PluralForms->new;
Data downloaded from web
$obj = Locale::Utils::PluralForms->new( language => 'en_GB', # fallbacks from given en_GB to en );
Data of given data structure
$obj = Locale::Utils::PluralForms->new( all_plural_forms => { 'en' => { english_name => 'English', plural_forms => 'nplurals=2; plural=(n != 1)', }, # next languages }, );
Getter
my $language = $obj->language; my $all_plural_forms = $obj->all_plural_forms; my $plural_forms = $obj->plural_forms; my $nplurals = $obj->nplurals; my $plural_code = $obj->plural_code; my $plural = $obj->plural_code->($number);
This module helps to find the plural forms for all languages. It downloads the plural forms for all languages from web. Then it stores the extracted data into a data structure named "all_plural_forms".
It is possible to fill that data structure before method "language" is called first time or to cache after first method call "language".
In the header of a PO- or MO-File is an entry is called "Plural-Forms". How many plural forms the language has, is described there in "nplurals". The second Information in "Plural-Forms" describes as a formula, how to choose the "plural".
This module compiles plural forms to a code references in a secure way.
Set the language to switch to the plural forms of that language. "plural_forms" is set then and "nplurals" and "plural_code" will be calculated.
$obj->language('de_AT'); # A fallback finds plural forms of language de # because de_AT is not different.
Read the language back.
$obj->language eq 'de_AT';
Set the data structure.
$obj->all_plural_forms({ 'de' => { english_name => 'German', plural_forms => 'nplurals=2; plural=(n != 1)', }, # next languages });
Read the data structure back.
my $hash_ref = $obj->all_plural_forms;
Set "plural_forms" if no "language" is set. After that "nplurals" and "plural_code" will be calculated in a safe way.
$obj->plural_forms('nplurals=1; plural=0');
Or read it back.
my $plural_forms = $obj->plural_forms;
This method get back the calculated count of plurals.
If no "language" and no "plural_forms" is set, the defaults for "nplurals" is:
my $count = $obj->nplurals # returns: 1
There is no public setter for "nplurals" and it is not possible to set them in the constructor. Call method "language" or "plural_forms" or set attribute "language" or "plural_forms" in the constructor. After that "nplurals" will be calculated automaticly and safe.
This method get back the calculated code for the "plural" to choose the correct "plural".
If no "language" and no "plural_forms" is set, the defaults for plural_code is:
my $code_ref = $obj->plural_code # returns: sub { return 0 } my $plural = $obj->plural_code->($number); # returns 0
There is no public setter for "plural_code" and it is not possible to set them in the constructor. Call method "language" or "plural_forms" or set attribute "language" or "plural_forms" in the constructor. After that "plural_code" will be calculated automaticly and safe.
For the example plural forms 'nplurals=2; plural=(n != 1)'
:
$plural = $obj->plural_code->(0), # $plural is 1 $plural = $obj->plural_code->(1), # $plural is 0 $plural = $obj->plural_code->(2), # $plural is 1 $plural = $obj->plural_code->(3), # $plural is 1 ...
Inside of this distribution is a directory named example. Run the *.pl files.
none
none
not known
not known
http://en.wikipedia.org/wiki/Gettext
http://translate.sourceforge.net/wiki/l10n/pluralforms
Steffen Winkler
Copyright (c) 2011,
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.