File Coverage

blib/lib/Locale/TextDomain/IfEnv.pm
Criterion Covered Total %
statement 33 36 91.6
branch 6 10 60.0
condition n/a
subroutine 15 15 100.0
pod 0 4 0.0
total 54 65 83.0


line stmt bran cond sub pod time code
1             package Locale::TextDomain::IfEnv;
2              
3             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4             our $DATE = '2019-12-26'; # DATE
5             our $DIST = 'Locale-TextDomain-IfEnv'; # DIST
6             our $VERSION = '0.001'; # VERSION
7              
8             #use strict 'subs', 'vars';
9             #use warnings;
10              
11             sub __expand($@) {
12 8     8   23 my ($translation, %args) = @_;
13 8         19 my $re = join '|', map { quotemeta $_ } keys %args;
  8         32  
14 8 50       74 $translation =~ s/\{($re)\}/defined $args{$1} ? $args{$1} : "{$1}"/ge;
  8         47  
15 8         44 $translation;
16             }
17              
18             # plain string
19             sub __($) {
20 1     1   99 $_[0];
21             }
22              
23             # interpolation
24             sub __x($@) {
25 2     2   7 goto &__expand;
26             }
27              
28             # plural
29             sub __n($$$) {
30 4     4   9 my ($msgid, $msgid_plural, $count) = @_;
31 4 100       19 $count > 1 ? $msgid_plural : $msgid;
32             }
33              
34             # plural + interpolation
35             sub __nx($$$@) {
36 6     6   21 my ($msgid, $msgid_plural, $count, %args) = @_;
37 6 100       22 __expand($count > 1 ? $msgid_plural : $msgid, %args);
38             }
39              
40             # alias for __nx
41             sub __xn($$$@) {
42 2     2   6 goto &__nx;
43             }
44              
45             # context
46             sub __p($$) {
47 1     1   4 $_[1];
48             }
49              
50             # context + interpolation
51             sub __px($$@) {
52 1     1   7 my $context = shift;
53 1         3 goto &__x;
54             }
55              
56             # context + plural
57             sub __np($$$$) {
58 2     2   6 my $context = shift;
59 2         6 goto &__n;
60             }
61              
62             # context + plural + interpolation
63             sub __npx($$$$@) {
64 2     2   4 my $context = shift;
65 2         7 goto &__nx;
66             }
67              
68             # Dummy functions for string marking.
69             sub N__($) {
70 1     1 0 5 return shift;
71             }
72              
73             sub N__n($$$) {
74 1     1 0 5 return @_;
75             }
76              
77             sub N__p($$) {
78 1     1 0 5 return @_;
79             }
80              
81             sub N__np($$$$) {
82 1     1 0 6 return @_;
83             }
84              
85             sub import {
86 1     1   8 my $class = shift;
87 1         2 local $Locale::TextDomain::IfEnv::textdomain = shift;
88 1         2 local @Locale::TextDomain::IfEnv::search_dirs = @_;
89              
90 1         2 my $caller = caller;
91              
92 1 50       4 if ($ENV{PERL_LOCALE_TEXTDOMAIN_IFENV}) {
93 0         0 require Locale::TextDomain;
94 0         0 eval "package $caller; use Locale::TextDomain \$Locale::TextDomain::IfEnv::textdomain, \@Locale::TextDomain::IfEnv::search_dirs;";
95 0 0       0 die if $@;
96             } else {
97 1         3 for (qw(__ __x __n __nx __xn __p __px __np __npx
98             N__ N__n N__p N__np)) {
99 13         19 *{"$caller\::$_"} = \&{$_};
  13         1439  
  13         23  
100             }
101             }
102             }
103              
104             1;
105             # ABSTRACT: Enable translation only when environment variable flag is true
106              
107             __END__