CatalystX/I18N/Role/PosixLocale.pm

filter: Statementstmt 45.9% 17 / 37 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 57.1% 4 / 7 Podpod 100% 1 / 1 totaltotal 30.1% 22 / 73 CC 10 SCAR 30.0
CC
10
Cov
29%
__ANON__
42.0
i18n_posix_category
13.3
__ANON__
6.0
CRAP 20.1
« CatalystX/I18N/Role/GetLocale.pm ↑ Summary CatalystX/I18N/DataLocalize.pm »
1 # ============================================================================
2 package CatalystX::I18N::Role::PosixLocale;
3 # ============================================================================
4  
5 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 requires qw(set_locale);
8  
9 1 ▶use POSIX qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $ORIGINAL_LOCALE;
12  
13 0 ▶after 'set_locale' => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
14 0 my ($c,$locale) = @_;
15  
16 0 my $category = $c->i18n_posix_category;
17  
18 # Save original locale
19 0 ▶ $ORIGINAL_LOCALE ||= POSIX::setlocale($category);
Condition0%0/3
$CatalystX::I18N::Role::PosixLocale::ORIGINAL_LOCALE ||= POSIX::setlocale($category)
l!l&&r!l&&!r
000
MC/DC0%0/2
$CatalystX::I18N::Role::PosixLocale::ORIGINAL_LOCALE ||= POSIX::setlocale($category)
$CatalystX::I18N::Role::PosixLocale::ORIGINAL_LOCALE POSIX::setlocale($category)
Truth table0%0/3
A ||= B
A= $CatalystX::I18N::Role::PosixLocale::ORIGINAL_LOCALEB= POSIX::setlocale($category)
ABresult
1X1
011
000
20  
21 # Set locale
22 0 my $set_locale = $locale.'.UTF-8';
23  
24 0 my $set_locale_result = POSIX::setlocale( $category, $set_locale);
25 0 ▶ unless (defined $set_locale_result
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $set_locale_result and $set_locale eq $set_locale_result) 0 0 0
Condition0%0/3
defined $set_locale_result and $set_locale eq $set_locale_result
!ll&&!rl&&r
000
MC/DC0%0/2
defined $set_locale_result and $set_locale eq $set_locale_result
defined $set_locale_result $set_locale eq $set_locale_result
Truth table0%0/3
A and B
A= defined $set_locale_resultB= $set_locale eq $set_locale_result
ABresult
0X0
100
111
26 && $set_locale eq $set_locale_result) {
27 0 $set_locale_result = POSIX::setlocale( $category, $locale);
28 0 ▶ unless (defined $set_locale_result) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $set_locale_result) 0 0 0
29 0 ▶ $c->log->warn(sprintf("Could not setlocale '%s' or '%s' (do you have this locale installed?)",$set_locale,$locale))
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
30 if $c->debug;
31 }
32 }
33 1 };
34  
35 0 ▶sub i18n_posix_category {
Subroutine i18n_posix_category: not called CC=4 SCAR=25.8
Pod documented
36 0 my ($c,$category_string) = @_;
37  
38 0 ▶ $category_string ||= $c->config->{I18N}{posix_category};
Condition0%0/3
$category_string ||= $c->config->{"I18N"}{"posix_category"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$category_string ||= $c->config->{"I18N"}{"posix_category"}
$category_string $c->config->{"I18N"}{"posix_category"}
Truth table0%0/3
A ||= B
A= $category_stringB= $c->config->{"I18N"}{"posix_category"}
ABresult
1X1
011
000
39 0 ▶ $category_string ||= 'LC_ALL';
Condition0%0/2
$category_string ||= "LC_ALL"
l!l
00
MC/DC0%0/1
$category_string ||= "LC_ALL"
$category_string
Truth table0%0/2
A
A= $category_string
Aresult
11
00
40 0 $category_string = uc($category_string);
41  
42 return 0
43 0 ▶ unless grep { $category_string eq $_ } qw(LC_ALL LC_COLLATE LC_MONETARY LC_NUMERIC);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$category_string eq $_;} "LC_ALL", "LC_COLLATE", "LC_MONETARY", "LC_NUMERIC" 0 0 0
44  
45 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 0 return &{"POSIX::".uc($category_string)};
47 }
48  
49 0 ▶after finalize => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
50 0 my ($c) = @_;
51  
52 0 my $category = $c->i18n_posix_category;
53 # Restore original locale
54 0 ▶ POSIX::setlocale( $category, $ORIGINAL_LOCALE )
Branch: 1 branch
BranchTrueFalseTotal
if defined $CatalystX::I18N::Role::PosixLocale::ORIGINAL_LOCALE 0 0 0
55 if defined $ORIGINAL_LOCALE;
56 1 };
57  
58 1 1;
59  
60 =encoding utf8
61  
62 =head1 NAME
63  
64 CatalystX::I18N::Role::PosixLocale - Sets the POSIX locale
65  
66 =head1 SYNOPSIS
67  
68 # In your catalyst base class
69 package MyApp::Catalyst;
70
71 use Catalyst qw/MyPlugins
72 CatalystX::I18N::Role::Base
73 CatalystX::I18N::Role::PosixLocale/;
74
75 sub action : Local {
76 my ($self,$c) = @_;
77  
78 $c->locale('sk_SK')
79 # POSIX LC_COLLATE locale is 'sk_SK.UTF-8' now
80 }
81  
82 =head1 DESCRIPTION
83  
84 This role sets the POSIX locales for each request.
85  
86 =head1 METHODS
87  
88 =head3 i18n_posix_category
89  
90 Helper method that returns the value of the requested POSIX locale category
91 (LC_ALL, LC_COLLATE, LC_NUMERIC, LC_MONETARY). The POSIX category that should
92 be used can be set in the I18N config.
93  
94 __PACKAGE__->config(
95 I18N => {
96 posix_category => 'LC_COLLATE',
97 }
98 );
99  
100 Default is LC_ALL
101  
102 =head1 CAVEATS
103  
104 POSIX locale is set for the whole process, and might affect other modules
105 outside of Catalyst' scope. This role is also known to cause problems if
106 used in conjunction with the L<CatalystX::I18N::TraitFor::ViewTT> role
107  
108 =head1 SEE ALSO
109  
110 L<POSIX>, L<perllocale>
111  
112 =head1 AUTHOR
113  
114 Maroš Kollár
115 CPAN ID: MAROS
116 maros [at] k-1.com
117
118 L<http://www.k-1.com>
« CatalystX/I18N/Role/GetLocale.pm ↑ Summary CatalystX/I18N/DataLocalize.pm »