HTTP/ClientDetect/Language.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Interchange6/Plugin/Interchange5/Request.pm ↑ Summary HTTP/ClientDetect.pm »
1 package HTTP::ClientDetect::Language;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Locale::Language;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use Locale::Country;
10  
11 my @languages = all_language_codes();
12 my @countries = all_country_codes();
13  
14 my %langs = map { $_ => 1 } @languages;
15 my %countrs = map { $_ => 1 } @countries;
16  
17 =head1 NAME
18  
19 HTTP::ClientDetect::Language - Lookup the client's preferred language
20  
21 =head1 VERSION
22  
23 Version 0.01
24  
25 =cut
26  
27 our $VERSION = '0.01';
28  
29  
30 =head1 SYNOPSIS
31  
32 use HTTP::ClientDetect::Language;
33 my $lang_detect = HTTP::ClientDetect::Language->new(server_default => "en_US");
34 # inside a Dancer route
35 get '/detect' => sub {
36 my $req = request;
37 my $lang = $lang_detect->language($req);
38 }
39  
40  
41 =head1 ACCESSORS
42  
43 =head2 server_default
44  
45 The C<server_default> should be set in the constructor and defaults to
46 C<en_US>. This will be always returned if the lookup fails
47  
48 =cut
49  
50  
51 has server_default => (is => 'rw',
52 default => sub { return "en_US" },
53 isa => sub {
54 die "Bad language $_[0]\n"
55 unless __PACKAGE__->check_language_name($_[0]);
56 });
57  
58  
59 =head2 available_languages
60  
61 Accessor to an arrayref of languages available on the server side.
62 Please use the short version (C<de>, not C<de_DE>), otherwise the
63 check will be too restrictive.
64  
65 =cut
66  
67 has available_languages => (is => 'rw',
68 isa => sub {
69 my $aref = $_[0];
70 die "Not an arrayref" unless ref($aref) eq 'ARRAY';
71 foreach my $l (@$aref) {
72 die "Bad language $l\n"
73 unless __PACKAGE__->check_language_name($l);
74 }
75 },
76 default => sub { [] },
77 );
78  
79  
80 =head1 SUBROUTINES/METHODS
81  
82 =head2 language($request_obj)
83  
84 Return the preferred language of the request. The request object
85 should an object which has the methods C<accept_language> or C<header>
86  
87 From L<http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html>:
88  
89 The Accept-Language request-header field is similar to Accept, but
90 restricts the set of natural languages that are preferred as a
91 response to the request. Language tags are defined in section 3.10.
92  
93 Accept-Language = "Accept-Language" ":"
94 1#( language-range [ ";" "q" "=" qvalue ] )
95 language-range = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" )
96  
97 Each language-range MAY be given an associated quality value which
98 represents an estimate of the user's preference for the languages
99 specified by that range. The quality value defaults to "q=1". For
100 example,
101  
102 Accept-Language: da, en-gb;q=0.8, en;q=0.7
103  
104 would mean: "I prefer Danish, but will accept British English and
105 other types of English." A language-range matches a language-tag if
106 it exactly equals the tag, or if it exactly equals a prefix of the
107 tag such that the first tag character following the prefix is "-".
108 The special range "*", if present in the Accept-Language field,
109 matches every tag not matched by any other range present in the
110 Accept-Language field.
111  
112 Note: This use of a prefix matching rule does not imply that
113 language tags are assigned to languages in such a way that it is
114 always true that if a user understands a language with a certain
115 tag, then this user will also understand all languages with tags
116 for which this tag is a prefix. The prefix rule simply allows the
117 use of prefix tags if this is the case.
118  
119 The language quality factor assigned to a language-tag by the
120 Accept-Language field is the quality value of the longest language-
121 range in the field that matches the language-tag. If no language-
122 range in the field matches the tag, the language quality factor
123 assigned is 0. If no Accept-Language header is present in the
124 request, the server
125  
126 SHOULD assume that all languages are equally acceptable. If an
127 Accept-Language header is present, then all languages which are
128 assigned a quality factor greater than 0 are acceptable.
129  
130 It might be contrary to the privacy expectations of the user to send
131 an Accept-Language header with the complete linguistic preferences
132 of the user in every request
133  
134 =cut
135  
136 sub language {
137 my ($self, $obj) = @_;
138 my @browser_langs = $self->browser_languages($obj);
139 my @avail = @{$self->available_languages};
140 if (@avail) {
141 foreach my $ua_lang (@browser_langs) {
142 foreach my $avail_lang (@avail) {
143 if ($ua_lang =~ m/^\Q$avail_lang\E(_[A-Z]+)?$/) {
144 return $ua_lang;
145 }
146 }
147 }
148 # nothing? then return the server default
149 return $self->server_default;
150 }
151 else {
152 return $browser_langs[0];
153 }
154 }
155  
156 =head2 browser_languages($request)
157  
158 This method returns the parsed and sorted list of language preferences
159 set in the browser, when the first element has higher priority.
160  
161 =cut
162  
163 sub browser_languages {
164 my ($self, $obj) = @_;
165 return $self->server_default unless $obj;
166 my $accept_str;
167 if ($obj->can("accept_language")) {
168 $accept_str = $obj->accept_language;
169 }
170 # nothing? try with header, but don't count too much on this
171 if (!$accept_str and $obj->can("header")) {
172 $accept_str = $obj->header('Accept-Language');
173 }
174 return $self->server_default unless $accept_str;
175
176 # split the string at ,
177 my @langs = split(/\s*,\s*/, $accept_str);
178 my @to_order;
179 foreach my $lang_str (@langs) {
180 next unless $lang_str;
181 my ($q, $code);
182 if ($lang_str =~ m/([a-zA-Z]+([-_][a-zA-Z]+)?)\s*(;\s*q\s*=\s*([0-9\.]+))?/) {
183 $code = $self->check_language_name($1);
184 $q = $4 || 1;
185 }
186 next unless $code;
187 push @to_order, [ $code => $q ];
188 # sort by q
189 }
190 return $self->server_default unless @to_order;
191 my @ordered = sort { $b->[1] <=> $a->[1] } @to_order;
192 return map { $_->[0] } @ordered;
193 }
194  
195 =head3 language_short($request_obj)
196  
197 Return the short language version (i.e.), the language name without
198 the country part.
199  
200 =cut
201  
202 sub language_short {
203 my ($self, $obj) = @_;
204 my $lang = $self->language($obj);
205 # strip the second part
206 $lang =~ s/_.*$//;
207 return $lang;
208 }
209  
210  
211 =head3 check_language_name
212  
213 Returns a normalized version of the language name, lower case for the
214 language, upper case for the country. Undef it was not possible to
215 validate it.
216  
217 =cut
218  
219 sub check_language_name {
220 my ($self, $code) = @_;
221 my ($lang, $country);
222 return unless $code;
223 if ($code =~ m/([a-zA-Z]{2})([_-]([a-zA-Z]*))?/) {
224 $lang = $1;
225 $country = $3 || "";
226 }
227 else {
228 $lang = $code;
229 $country = $code; # eg. fr fr
230 }
231 # lowercase;
232 $lang = lc($lang);
233 $country = lc($country);
234 # check the lang;
235 return unless $langs{$lang};
236 # if the country doesn't validate, we fix the common scenario (en
237 # => US), and append the same
238 if ($countrs{$country}) {
239 return $lang . "_" . uc($country);
240 }
241 # then do some heuristics, if the country didn't match
242 if ($lang eq 'en') {
243 return $lang . "_US";
244 }
245 # then try the language as a country
246 if ($countrs{$lang}) {
247 return $lang . "_" . uc($lang);
248 }
249 # if we are still here, return the language, there are cases we
250 # can't catch, like ja_JP
251 return $lang;
252 }
253  
254  
255 =head1 AUTHOR
256  
257 Marco Pessotto, C<< <melmothx at gmail.com> >>
258  
259 =head1 BUGS
260  
261 Please report any bugs or feature requests to C<bug-interchange6-plugin-autodetect at rt.cpan.org>, or through
262 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTTP-ClientDetect>. I will be notified, and then you'll
263 automatically be notified of progress on your bug as I make changes.
264  
265  
266  
267  
268 =head1 SUPPORT
269  
270 You can find documentation for this module with the perldoc command.
271  
272 perldoc HTTP::ClientDetect::Language
273  
274  
275 You can also look for information at:
276  
277 =over 4
278  
279 =item * RT: CPAN's request tracker (report bugs here)
280  
281 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTTP-ClientDetect>
282  
283 =item * AnnoCPAN: Annotated CPAN documentation
284  
285 L<http://annocpan.org/dist/HTTP-ClientDetect>
286  
287 =item * CPAN Ratings
288  
289 L<http://cpanratings.perl.org/d/HTTP-ClientDetect>
290  
291 =item * Search CPAN
292  
293 L<http://search.cpan.org/dist/HTTP-ClientDetect/>
294  
295 =back
296  
297  
298 =head1 ACKNOWLEDGEMENTS
299  
300  
301 =head1 LICENSE AND COPYRIGHT
302  
303 Copyright 2013 Marco Pessotto.
304  
305 This program is free software; you can redistribute it and/or modify it
306 under the terms of either: the GNU General Public License as published
307 by the Free Software Foundation; or the Artistic License.
308  
309 See L<http://dev.perl.org/licenses/> for more information.
310  
311  
312 =cut
313  
314 1; # End of HTTP::ClientDetect::Language
« Interchange6/Plugin/Interchange5/Request.pm ↑ Summary HTTP/ClientDetect.pm »