blib/lib/Dancer/Plugin/Locale/Wolowitz.pm

filter: Statementstmt 77.0% 47 / 61 Branchbran 50.0% 7 / 14 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 78.5% 11 / 14 Podpod n/a totaltotal 69.0% 67 / 97 SCAR 25.4
CC
10
Cov
70%
_detect_lang_from_browser
12.0
_lang
5.4
_path_directory_locale
3.6
CRAP 12.7
↑ Summary Build »
1 #
2 # This file is part of Dancer-Plugin-Locale-Wolowitz
3 #
4 # This software is copyright (c) 2014 by Natal Ngétal.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 package Dancer::Plugin::Locale::Wolowitz;
10 {
11 2 $Dancer::Plugin::Locale::Wolowitz::VERSION = '0.140120';
12 }
13  
14 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶use Locale::Wolowitz;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 #ABSTRACT: Intenationalization for Dancer
26  
27 2 my $w;
28  
29 #Register exception
30 2 register_exception('DirectoryNotFound',
31 message_pattern => "Not found directory: %s"
32 );
33  
34  
35 add_hook(
36 before_template => sub {
37 0 ▶ my $tokens = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
38  
39 0 ▶ $tokens->{l} = sub { _loc(@_); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 }
41 2 );
42  
43 register loc => sub {
44 6 ▶ _loc(@_);
Subroutine __ANON__: called CC=1 SCAR=0.0
45 2 };
46  
47 sub _loc {
48 6 ▶ my ( $str, $args ) = @_;
Subroutine _loc: called CC=3 SCAR=11.0
49  
50 6 ▶ $w = Locale::Wolowitz->new(_path_directory_locale()) unless defined($w);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w 1 5 6
51 6 my $lang = _lang();
52  
53 6 ▶ !$args and return $w->loc($str, $lang);
Branch: 1 branch
BranchTrueFalseTotal
$args or return $w->loc($str, $lang) 4 2 6
54  
55 2 my $msg = $w->loc($str, $lang, map($w->loc($_, $lang), @{$args}));
56  
57 2 return $msg;
58 }
59  
60 sub _path_directory_locale {
61 1 ▶ my $settings = plugin_setting;
Subroutine _path_directory_locale: called CC=3 SCAR=12.7
62 my $path = $settings->{locale_path_directory}
63 1 ▶ // Dancer::FileUtils::path(setting('appdir'), 'i18n');
Condition33%1/3
$settings->{"locale_path_directory"} // Dancer::FileUtils::path(setting("appdir"), "i18n")
l!l&&r!l&&!r
010
MC/DC0%0/2
$settings->{"locale_path_directory"} // Dancer::FileUtils::path(setting("appdir"), "i18n")
$settings->{"locale_path_directory"} Dancer::FileUtils::path(setting("appdir"), "i18n")
Truth table33%1/3
A // B
A= $settings->{"locale_path_directory"}B= Dancer::FileUtils::path(setting("appdir"), "i18n")
ABresult
1X1
011
000
64  
65 1 ▶ if ( ! -d $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $path) 0 1 1
66 0 raise DirectoryNotFound => $path;
67 }
68  
69 1 return $path;
70 }
71  
72 sub _lang {
73 6 ▶ my $settings = plugin_setting;
Subroutine _lang: called CC=4 SCAR=16.9
74 6 ▶ my $lang_session = $settings->{lang_session} || 'lang';
Condition50%1/2
$settings->{'lang_session'} || 'lang'
l!l
06
MC/DC0%0/1
$settings->{'lang_session'} || 'lang'
$settings->{'lang_session'}
Truth table50%1/2
A
A= $settings->{'lang_session'}
Aresult
11
00
75 6 my $lang;
76  
77 # don't force the user to store lang in a session
78 6 ▶ if ( setting('session') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (setting("session")) 6 0 6
79 6 my $session_language = session $lang_session;
80  
81 6 ▶ if ( !$session_language ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $session_language) { } 0 6 6
82 0 $lang = _detect_lang_from_browser();
83  
84 0 session $lang_session => $lang;
85 0 return $lang;
86 }
87 else {
88 6 return $session_language;
89 }
90 }
91  
92 0 $lang = _detect_lang_from_browser();
93  
94 0 return $lang;
95 }
96  
97 sub _detect_lang_from_browser {
98 0 ▶ my $lang = request->accept_language;
Subroutine _detect_lang_from_browser: not called CC=3 SCAR=24.8
99  
100 0 ▶ return unless $lang;
Branch: 1 branch
BranchTrueFalseTotal
unless $lang 0 0 0
101  
102 0 $lang =~ s/-\w+//g;
103 0 ▶ $lang = (split(/,\s*/,$lang))[0] if $lang =~ /,/;
Branch: 1 branch
BranchTrueFalseTotal
if $lang =~ /,/ 0 0 0
104  
105 0 return $lang;
106 }
107  
108 2 register_plugin;
109  
110 2 1;
111  
112 __END__
↑ Summary Build »