File Coverage

blib/lib/Locale/Maketext/ManyPluralForms.pm
Criterion Covered Total %
statement 31 33 93.9
branch 7 10 70.0
condition n/a
subroutine 6 7 85.7
pod 1 1 100.0
total 45 51 88.2


line stmt bran cond sub pod time code
1             package Locale::Maketext::ManyPluralForms;
2             # ABSTRACT: internationalisation with many plural forms
3 1     1   26573 use strict;
  1         2  
  1         28  
4 1     1   5 use warnings;
  1         1  
  1         52  
5              
6             our $VERSION = '0.04';
7              
8             require Locale::Maketext::Lexicon;
9              
10 1     1   562 use parent 'Locale::Maketext';
  1         295  
  1         5  
11              
12             sub import {
13 1     1   140 shift;
14 1         6 return Locale::Maketext::Lexicon->import(@_);
15             }
16              
17             sub plural {
18 4     4 1 3235 my ($self, $num, @strings) = @_;
19 4 100       10 unless (defined $num) {
20 1         16 warn 'Use of uninitialized value $num in ' . ref($self) . " with params: '" . join(";", @strings) . "'";
21 1         50 $num = 0;
22             }
23 4 100       8 unless ($self->{_plural}) {
24 1         3 my $class = ref $self;
25 1     1   12360 no strict 'refs'; ## no critic
  1         2  
  1         302  
26 1         1 my $header = ${"${class}::Lexicon"}{"__Plural-Forms"};
  1         4  
27 1 50       2 if ($header) {
28 1         7 $header =~ s/^.*plural\s*=\s*([^;]+);.*$/$1/;
29 1         11 $header =~ s/\[_([0-9]+)\]/%$1/g;
30 1 50       16 die "Invalid expression for plural: $header" if $header =~ /\$|n\s*\(|[A-Za-mo-z]|nn/;
31 1         5 $header =~ s/n/\$_[0]/g;
32 1         99 eval "\$self->{_plural} = sub { return $header }"; ## no critic (ProhibitStringyEval RequireCheckingReturnValueOfEval)
33             } else {
34 0     0   0 $self->{_plural} = sub { return $_[0] != 1 };
  0         0  
35             }
36             }
37 4         84 my $pos = $self->{_plural}($num);
38 4 50       9 $pos = $#strings if $pos > $#strings;
39 4         24 return sprintf $strings[$pos], $num;
40             }
41              
42             1;
43              
44             __END__
45              
46             =head1 NAME
47              
48             Locale::Maketext::ManyPluralForms
49              
50             =head1 SYNOPSIS
51              
52             use Locale::Maketext::ManyPluralForms {'*' => ['Gettext' => 'i18n/*.po']};
53             my $lh = Locale::Maketext::ManyPluralForms->get_handle('en');
54             $lh->maketext("Hello");
55              
56             =head1 DESCRIPTION
57              
58             The implementation supporting internationalisation with many plural forms
59             using Plural-Forms header from .po file to add plural method to Locale::Maketext based class.
60             As described there L<http://www.perlmonks.org/index.pl?node_id=898687>.
61              
62             =head1 METHODS
63              
64             =cut
65              
66             =head2 Locale::Maketext::ManyPluralForms->import({'*' => ['Gettext' => 'i18n/*.po']})
67              
68             This method to specify languages.
69              
70             =cut
71              
72             =head2 $self->plural($num, @strings)
73              
74             This method handles plural forms. You can invoke it using Locale::Maketext's
75             bracket notation, like "[plural,_1,string1,string2,...]". Depending on value of
76             I<$num> and language function returns one of the strings. If string contain %d
77             it will be replaced with I<$num> value.
78              
79             =cut
80              
81             =head1 SEE ALSO
82              
83             L<Locale::Maketext>,
84             L<Locale::Maketext::Lexicon>
85              
86             =head1 COPYRIGHT AND LICENSE
87              
88             Copyright (C) 2016 binary.com
89              
90             =cut