File Coverage

blib/lib/Locale/Simple.pm
Criterion Covered Total %
statement 69 93 74.1
branch 19 26 73.0
condition 9 9 100.0
subroutine 19 33 57.5
pod 0 17 0.0
total 116 178 65.1


line stmt bran cond sub pod time code
1             package Locale::Simple;
2             BEGIN {
3 6     6   151703 $Locale::Simple::AUTHORITY = 'cpan:GETTY';
4             }
5             # ABSTRACT: Functions for translate text based on gettext data, also in JavaScript
6             $Locale::Simple::VERSION = '0.019';
7 6     6   55 use strict;
  6         14  
  6         215  
8 6     6   32 use warnings;
  6         12  
  6         194  
9              
10 6     6   51 use Exporter 'import';
  6         12  
  6         199  
11 6     6   6067 use Locale::TextDomain 1.23 (); # to ensure we get the right version of gettext_dumb
  6         202958  
  6         404  
12 6     6   6218 use Locale::gettext_dumb qw(:locale_h :libintl_h);
  6         210942  
  6         2016  
13 6     6   71 use POSIX qw' setlocale ';
  6         13  
  6         54  
14 6     6   13891 use IO::All 0.41 -utf8;
  6         87287  
  6         83  
15              
16             our @EXPORT = qw(
17            
18             l_dir
19             l_lang
20             l_dry
21             l_nolocales
22              
23             l
24             ln
25             lp
26             lnp
27             ld
28             ldn
29             ldp
30             ldnp
31            
32             ltd
33              
34             );
35              
36             my $dry;
37             my $nowrite;
38             my $nolocales;
39              
40             my %tds;
41             my $dir;
42              
43 1     1 0 596 sub l_nolocales { $nolocales = shift }
44 1 50   1 0 582 sub l_dry { $dry = shift; $nowrite = shift; $nolocales = 1 if $dry }
  1         2  
  1         5  
45              
46             sub gettext_escape {
47 44     44 0 54 my ( $content ) = @_;
48 44         69 $content =~ s/\\/\\\\/g;
49 44         50 $content =~ s/\n/\\n/g;
50 44         52 $content =~ s/"/\\"/g;
51 44         367 return $content;
52             }
53              
54             sub sprintf_compare {
55 0     0 0 0 my ( $first, $second ) = @_;
56 0         0 my $re = qr/(?:%%|%(?:[0-9]+\$)?[+-]?(?:[ 0]|\'.)?-?[0-9]*(?:\.[0-9]+)?[bcdeufFosxX])/;
57 0         0 my @placeholder_first = sort { $a cmp $b } map { my @chars = split(//,$_); pop @chars; } ($first =~ m/$re/g);
  0         0  
  0         0  
  0         0  
58 0         0 my @placeholder_second = sort { $a cmp $b } map { my @chars = split(//,$_); pop @chars; } ($second =~ m/$re/g);
  0         0  
  0         0  
  0         0  
59 0         0 return join("",@placeholder_first) eq join("",@placeholder_second);
60             }
61              
62             sub coderef_hash {{
63 0     0   0 l => sub { l(@_) },
64 0     0   0 ln => sub { ln(@_) },
65 0     0   0 lp => sub { lp(@_) },
66 0     0   0 lnp => sub { lnp(@_) },
67 0     0   0 ld => sub { ld(@_) },
68 0     0   0 ldn => sub { ldn(@_) },
69 0     0   0 ldp => sub { ldp(@_) },
70 0     0   0 ldnp => sub { ldnp(@_) },
71 0     0 0 0 }}
72              
73 3     3 0 30 sub l_dir { $dir = shift }
74              
75             sub l_lang {
76 2     2 0 14 my $primary = shift;
77 2         18 $ENV{LANGUAGE} = $primary;
78 2         9 $ENV{LANG} = $primary; # unsure if these ENV assignments are still needed with the setlocale below
79 2         10 $ENV{LC_ALL} = $primary;
80 2         7 $ENV{LC_MESSAGES} = $primary; # set locale for messages if the system supports it
81 2 50       4 setlocale( LC_MESSAGES, $primary ) if eval { LC_MESSAGES }; # set locale for messages if the system supports it
  2         58  
82             }
83              
84             # write dry
85 5 50   5 0 25 sub wd { io($dry)->append(join("\n",@_)."\n\n") if !$nowrite }
86              
87             # l(msgid,...)
88 9     9 0 1380 sub l { return ldnp('',undef,shift,undef,undef,@_) }
89             # ln(msgid,msgid_plural,count,...)
90 12     12 0 43 sub ln { return ldnp('',undef,@_) }
91             # lp(msgctxt,msgid,...)
92 1     1 0 3 sub lp { return ldnp('',shift,shift,undef,undef,@_) }
93             # lnp(msgctxt,msgid,msgid_plural,count,...)
94 0     0 0 0 sub lnp { return ldnp('',shift,shift,shift,shift,@_) }
95             # ld(domain,msgid,...)
96 0     0 0 0 sub ld { return ldnp(shift,undef,shift,undef,undef,@_) }
97             # ldn(domain,msgid,msgid_plural,count,...)
98 0     0 0 0 sub ldn { return ldnp(shift,undef,shift,shift,shift,@_) }
99             # ldp(domain,msgctxt,msgid,...)
100 0     0 0 0 sub ldp { return ldnp(shift,shift,shift,undef,undef,@_) }
101             # ldnp(domain,msgctxt,msgid,msgid_plural,count,...)
102             sub ldnp {
103 22 100 100 22 0 95 die "please set a locale directory with l_dir() before using other translate functions" unless $dir || $nolocales;
104 21         51 my ($td, $ctxt, $id, $idp, $n) = (shift,shift,shift,shift,shift);
105 21         39 my @args = @_;
106 21 100       52 unshift @args, $n if $idp;
107 21         25 my $return;
108 21 100       63 if ($dry) {
109 5 50       11 if (!$nowrite) {
110 5         8 my @save;
111 5 50       11 push @save, '# domain: '.$td if $td;
112 5 50       9 push @save, 'msgctxt "'.gettext_escape($ctxt).'"' if $ctxt;
113 5         11 push @save, 'msgid "'.gettext_escape($id).'"';
114 5 100       16 push @save, 'msgid_plural "'.gettext_escape($idp).'"' if $idp;
115 5         10 wd(@save);
116             }
117 5 100 100     12730 $return = sprintf($idp && $n != 1 ? $idp : $id, @args);
118             } else {
119             # L::TD handles msg ids as bytes internally
120 16         32 utf8::encode($id);
121 16         51 my $gt = dnpgettext($td, $ctxt, $id, $idp, $n);
122             # Fixing bad utf8 handling
123 16         4923 utf8::decode($gt);
124 16         55 $return = sprintf($gt,@args);
125             }
126 21         737 return $return;
127             }
128              
129             sub ltd {
130 6 100 100 6 0 402 die "please set a locale directory with l_dir() before using other translate functions" unless $dir || $nolocales;
131 5         13 my $td = shift;
132 5 50       23 unless (defined $tds{$td}) {
133 5         29 bindtextdomain($td,$dir);
134 5         79 bind_textdomain_codeset($td,'utf-8');
135 5         43 $tds{$td} = 1;
136             }
137 5         22 textdomain($td);
138             }
139              
140             1;
141              
142             __END__