File Coverage

blib/lib/Dancer/Plugin/Locale/Wolowitz.pm
Criterion Covered Total %
statement 34 47 72.3
branch 7 16 43.7
condition 4 8 50.0
subroutine 11 12 91.6
pod n/a
total 56 83 67.4


line stmt bran cond sub pod time code
1             #
2             # This file is part of Dancer-Plugin-Locale-Wolowitz
3             #
4             # This software is copyright (c) 2016 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             $Dancer::Plugin::Locale::Wolowitz::VERSION = '0.160190';
11 2     2   321630 use strict;
  2         4  
  2         53  
12 2     2   10 use warnings;
  2         4  
  2         48  
13              
14 2     2   107 use 5.010;
  2         10  
15              
16 2     2   1152 use Dancer ':syntax';
  2         286642  
  2         11  
17 2     2   2415 use Dancer::Plugin;
  2         2755  
  2         159  
18 2     2   12 use Dancer::Exception qw(:all);
  2         4  
  2         229  
19              
20 2     2   1502 use Locale::Wolowitz;
  2         32342  
  2         1072  
21              
22             #ABSTRACT: Internationalization for Dancer
23              
24             my $w;
25              
26             #Register exception
27             register_exception('DirectoryNotFound',
28             message_pattern => "Wolowitz internationalization directory not found: %s"
29             );
30              
31              
32             add_hook(
33             before_template => sub {
34             $_[0]->{l} = sub { _loc(@_); };
35             }
36             );
37              
38             register loc => sub {
39 9     9   22758 _loc(@_);
40             };
41              
42             sub _loc {
43             # my ( $str, $args, $force_lang ) = @_;
44              
45 9 100   9   29 $w = Locale::Wolowitz->new(_path_directory_locale()) unless defined($w);
46 9   66     283 my $lang = $_[2] || _lang();
47              
48             # return early if no args array-ref given
49 9 100       37 !$_[1] and return $w->loc($_[0], $lang); # was: !$args and return $w->loc($str, $lang);
50              
51 5         10 return $w->loc($_[0], $lang, map($w->loc($_, $lang), @{$_[1]}));
  5         21  
52             }
53              
54             sub _path_directory_locale {
55             my $path = plugin_setting()->{locale_path_directory}
56 1   33 1   5 // Dancer::FileUtils::path(setting('appdir'), 'i18n');
57              
58 1 50       150 if ( ! -d $path ) {
59 0         0 raise DirectoryNotFound => $path;
60             }
61              
62 1         10 return $path;
63             }
64              
65             sub _lang {
66 6     6   8 my $lang;
67             # don't force the user to store lang in a session
68 6 50       18 if ( setting('session') ) {
69 6   50     163 my $lang_session = plugin_setting()->{lang_session} || 'lang';
70 6         114 my $session_language = session $lang_session;
71 6 50       1067 return $session_language if $session_language;
72              
73 0           $lang = _detect_lang_from_browser();
74 0           session $lang_session => $lang;
75 0           return $lang;
76             }
77              
78 0           $lang = _detect_lang_from_browser();
79              
80 0           return $lang;
81             }
82              
83             sub _detect_lang_from_browser {
84             # a rude shortcut, for no-session contexts, so multiple loc() calls within the
85             # same request don't trigger regex matching/string munging over and over
86 0 0   0     return request->{__dancer_plugin_locale_wolowitz_detected_language} if request->{__dancer_plugin_locale_wolowitz_detected_language};
87              
88 0           my $lang = request->accept_language;
89 0 0         return unless $lang;
90              
91 0           $lang =~ s/-\w+//g;
92 0 0         $lang = (split(/,\s*/,$lang))[0] if index($lang,',');
93              
94 0           request->{__dancer_plugin_locale_wolowitz_detected_language} = $lang; # this is a bit rude, but it saves us from detecting lang over and over (with regex and all) within the same request
95 0           return $lang;
96             }
97              
98             register_plugin;
99              
100             1;
101              
102             __END__