File Coverage

blib/lib/Dancer2/Plugin/Locale/Wolowitz.pm
Criterion Covered Total %
statement 42 43 97.6
branch 4 6 66.6
condition 5 9 55.5
subroutine 11 11 100.0
pod n/a
total 62 69 89.8


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::Locale::Wolowitz;
2              
3 2     2   492663 use 5.010;
  2         8  
4 2     2   12 use strict;
  2         4  
  2         43  
5 2     2   21 use warnings;
  2         7  
  2         57  
6              
7 2     2   792 use Dancer2::FileUtils;
  2         862  
  2         85  
8 2     2   1532 use Dancer2::Plugin;
  2         164418  
  2         13  
9 2     2   4277 use I18N::AcceptLanguage;
  2         2434  
  2         63  
10 2     2   1536 use Locale::Wolowitz;
  2         89339  
  2         1484  
11              
12             our $VERSION = '0.04';
13              
14             my $wolowitz;
15              
16             =head1 NAME
17              
18             Dancer2::Plugin::Locale::Wolowitz - Dancer2's plugin for Locale::Wolowitz
19              
20             =head1 DESCRIPTION
21              
22             This plugin give you the L support. It's a blatant copy of
23             L and should be a drop in replacement
24             for Dancer2 projects.
25              
26             =head1 SYNOPSIS
27              
28             use Dancer2;
29             use Dancer2::Plugin::Locale::Wolowitz;
30              
31             # in your templates
32             get '/' => sub {
33             template 'index';
34             }
35              
36             # or directly in code
37             get '/logout' => sub {
38             template 'logout', {
39             bye => loc('Bye');
40             }
41             }
42              
43             ... meanwhile, in a nearby template file called index.tt
44              
45             <% l('Welcome') %>
46              
47             =head1 CONFIGURATION
48              
49             plugins:
50             Locale::Wolowitz:
51             fallback: "en"
52             locale_path_directory: "i18n"
53             lang_session: "lang"
54             lang_available:
55             - de
56             - en
57             - id
58             - nl
59              
60             =cut
61              
62              
63             on_plugin_import {
64             my $dsl = shift;
65              
66             $dsl->app->add_hook(
67             Dancer2::Core::Hook->new(
68             name => 'before_template_render',
69             code => sub {
70             my $tokens = shift;
71             $tokens->{l} = sub { _loc($dsl, @_); };
72             }
73             )
74             );
75             };
76              
77             =head1 KEYWORDS
78              
79             =head2 loc
80              
81             The C keyword can be used in code to look up the correct translation. In
82             templates you can use the C function
83              
84             =cut
85              
86             register loc => \&_loc;
87              
88             sub _loc {
89 14     14   152538 my ($dsl, $str, $args) = @_;
90              
91 14   66     60 $wolowitz ||= Locale::Wolowitz->new(_path_directory_locale($dsl));
92 14         397 my $lang = _lang($dsl);
93              
94 14         391 return $wolowitz->loc($str, $lang, @$args);
95             };
96              
97             sub _path_directory_locale {
98 1     1   3 my $dsl = shift;
99              
100 1         5 my $conf = $dsl->{app}{config}{plugins}{'Locale::Wolowitz'};
101              
102 1   50     10 my $dir = $conf->{locale_path_directory} // 'i18n';
103 1 50       51 unless (-d $dir) {
104 1         14 $dir = Dancer2::FileUtils::path($dsl->app->setting('appdir'), $dir);
105             }
106 1         218 return $dir;
107             }
108              
109             sub _lang {
110 14     14   26 my $dsl = shift;
111              
112 14         44 my $conf = $dsl->{app}{config}{plugins}{'Locale::Wolowitz'};
113 14   50     83 my $lang_session = $conf->{lang_session} || 'lang';
114              
115 14 100       68 if( $dsl->app->has_session ) {
116 12         3096 my $session_language = $dsl->app->session->read( $lang_session );
117              
118 12 50       2553 if( !$session_language ) {
119 0         0 $session_language = _detect_lang_from_browser($dsl);
120             }
121              
122 12         38 return $session_language;
123             } else {
124 2         83 return _detect_lang_from_browser($dsl);
125             }
126             }
127              
128             sub _detect_lang_from_browser {
129 2     2   4 my $dsl = shift;
130              
131 2         5 my $conf = $dsl->{app}{config}{plugins}{'Locale::Wolowitz'};
132 2   50     22 my $acceptor = I18N::AcceptLanguage->new(defaultLanguage => $conf->{fallback} // "");
133 2         110 return $acceptor->accepts($dsl->app->request->accept_language, $conf->{lang_available});
134             }
135              
136             =head1 AUTHOR
137              
138             Menno Blom, C<< >>
139              
140             =head1 BUGS / CONTRIBUTING
141              
142             This module is developed on Github at:
143             L
144              
145             =head1 ACKNOWLEDGEMENTS
146              
147             Many thanks go out to L for
148             writing the Dancer 1 version of this plugin (L).
149              
150             And obviously thanks to L for
151             creating the main code we're using in this plugin! (L).
152              
153             =head1 COPYRIGHT
154              
155             Copyright 2014- Menno Blom
156              
157             =head1 LICENSE
158              
159             This library is free software; you can redistribute it and/or modify
160             it under the same terms as Perl itself.
161              
162             =cut
163              
164             register_plugin;
165              
166             1; # End of Dancer2::Plugin::Locale::Wolowitz