File Coverage

blib/lib/Mojolicious/Plugin/Localize/Locale.pm
Criterion Covered Total %
statement 28 28 100.0
branch 8 8 100.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 43 43 100.0


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Localize::Locale;
2 12     12   7279 use Mojo::Base 'Mojolicious::Plugin';
  12         25  
  12         100  
3 12     12   10075 use I18N::LangTags qw/implicate_supers/;
  12         34925  
  12         936  
4 12     12   5745 use I18N::LangTags::Detect;
  12         18191  
  12         469  
5 12     12   71 use List::MoreUtils 'uniq';
  12         20  
  12         100  
6              
7             # Register plugin
8             sub register {
9 13     13 1 629 my ($self, $mojo) = @_;
10              
11             # Establish helpers
12             $mojo->helper(
13             'localize.locale' => sub {
14 45     45   32302 my $c = shift;
15              
16             # Delete preference
17 45 100       131 if (@_ != 0) {
18 7         21 delete $c->stash->{'localize.preference'};
19             };
20              
21             # Already requested from stash
22 45 100       165 if ($c->stash('localize.locale')) {
23              
24             # Return cached values
25 30 100       273 return $c->stash('localize.locale') if @_ == 0;
26              
27             # Prepend override values
28             $c->stash('localize.locale' => my $lang = [
29 2         5 uniq(implicate_supers(map {lc} @_), @{ $c->stash('localize.locale')})
  2         30  
  2         256  
30             ]);
31              
32 2         93 return $lang;
33             };
34              
35             # Get languages from request headers
36 15         175 my @langs = implicate_supers(
37             I18N::LangTags::Detect->http_accept_langs(
38             $c->req->headers->accept_language
39             ));
40              
41             # Prepend override values
42 15 100       2830 unshift(@langs, implicate_supers(map {lc} @_)) if @_ > 0;
  5         16  
43              
44             # Return lang stash
45 15         428 $c->stash('localize.locale' => my $lang = [ uniq(@langs) ]);
46 15         269 return $lang;
47             }
48 13         122 );
49             };
50              
51              
52             1;
53              
54              
55             =pod
56              
57             =head1 NAME
58              
59             Mojolicious::Plugin::Localize::Locale - Localize Based on Requested Locales
60              
61              
62             =head1 SYNOPSIS
63              
64             # Register plugin with a dictionary in Mojolicious::Lite
65             plugin Localize => {
66             dict => {
67             welcome => {
68             '_' => sub { $_->locale },
69             -en => 'Welcome!',
70             de => 'Willkommen!',
71             fr => 'Bonjour!'
72             }
73             }
74             };
75              
76             # Optionally create language depending routes
77             under '/:lang' => { lang => '' } => sub {
78             my $c = shift;
79              
80             # Prefer the chosen language
81             $c->localize->locale($c->stash('lang')) if $c->stash('lang');
82             return 1;
83             };
84              
85             # Set language depending routes
86             get '/' => sub {
87             shift->render('<%= loc "welcome" %>');
88             };
89              
90              
91             =head1 DESCRIPTION
92              
93             L detects preferred languages
94             of a user agent's request to be used as preferred keys in dictionaries for
95             L.
96              
97              
98             =head1 METHODS
99              
100             L inherits all methods
101             from L and implements the following
102             new ones.
103              
104              
105             =head2 register
106              
107             # Mojolicious
108             $mojo->plugin('Localize::Locale');
109              
110             # Mojolicious::Lite
111             plugin 'Localize::Locale';
112              
113             Called when registering the plugin.
114             The plugin is registered by L by default.
115              
116              
117             =head1 HELPERS
118              
119             =head2 localize->locale
120              
121             # Return the requested languages
122             my $lang = $c->localize->locale;
123             # $lang = ['en-us', 'en']
124              
125             # Set a preferred language
126             $lang = $c->localize->locale('de-DE');
127             # $lang = ['de-de', 'de', 'en-us', 'en']
128              
129             Returns an array reference of locales the user preferred based on
130             the request headers. If language notations following
131             L
132             are passed, these will be preferred over detected languages
133             (e.g. based on the URL path, TLD, GeoIP, or user preferences coming from a database).
134              
135             All short names will be lower cased and specific languages will be followed
136             by the short name of their super languages.
137              
138              
139             =head1 AVAILABILITY
140              
141             https://github.com/Akron/Mojolicious-Plugin-Localize
142              
143              
144             =head1 COPYRIGHT AND LICENSE
145              
146             Copyright (C) 2014-2017, L.
147              
148             This program is free software, you can redistribute it
149             and/or modify it under the terms of the Artistic License version 2.0.
150              
151             =cut