NAME
Locale::Utils::PluralForms - Utils to use plural forms
VERSION
0.003
SYNOPSIS
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);
DESCRIPTION
Find plural forms for the language
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".
"plural" as subroutine
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.
SUBROUTINES/METHODS
Find plural forms for the language
method language
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';
method all_plural_forms
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;
executable plural forms
method 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;
method nplurals
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.
method plural_code
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
...
EXAMPLE
Inside of this distribution is a directory named example. Run the *.pl files.
DIAGNOSTICS
none
CONFIGURATION AND ENVIRONMENT
none
DEPENDENCIES
INCOMPATIBILITIES
not known
BUGS AND LIMITATIONS
not known
SEE ALSO
http://en.wikipedia.org/wiki/Gettext
http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html
AUTHOR
Steffen Winkler
LICENSE AND COPYRIGHT
Copyright (c) 2011 - 2022, 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.