File Coverage

blib/lib/Dancer2/Plugin/Locale/Meta.pm
Criterion Covered Total %
statement 30 34 88.2
branch 2 2 100.0
condition 1 3 33.3
subroutine 8 9 88.8
pod 0 3 0.0
total 41 51 80.3


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::Locale::Meta;
2              
3             # ABSTRACT: Interface to support multilanguage using Locale::Meta package.
4              
5 4     4   1304560 use strict;
  4         9  
  4         119  
6 4     4   19 use warnings;
  4         6  
  4         128  
7 4     4   1949 use Dancer2::Plugin;
  4         43984  
  4         23  
8 4     4   10180 use Locale::Meta;
  4         3110  
  4         352  
9              
10             our $VERSION = '0.004';
11              
12             =head1 NAME
13              
14             Dancer2::Pluging::Locale::Meta
15              
16             =head1 DESCRIPTION
17              
18             This plugin allow Dancer2 developers to use L package. This Plugin is
19             based on L plugin.
20              
21             =head1 SYNOPSIS
22              
23             use Dancer2;
24             use Dancer2::Plugin::Locale::Meta;
25              
26             # in your routes
27              
28             get '/' => sub {
29             my $greeting = loc("hello");
30             template index.tt, { greeting => $greeting }
31             }
32              
33             # in your template
34              
35             <% l('greeting') %>
36              
37             # load custom structure on your app
38              
39              
40             my $structure = {
41             "en" => {
42             "goodbye" => {
43             "trans" => "bye",
44             }
45             },
46             "es" => {
47             "goodbye" => {
48             "trans" => "chao",
49             }
50             }
51             };
52              
53             In order to load the data use the keyword on your routes:
54              
55             load_structure($structure);
56              
57              
58             =head1 CONFIGURATION
59              
60             plugins:
61             Locale::Meta:
62             fallback: "en"
63             locale_path_directory: "i18n"
64             lang_session: "lang"
65             =cut
66              
67             BEGIN{
68             has 'fallback' => (
69             is => 'ro',
70             from_config => 1,
71             default => sub {}
72 4     4   107 );
73              
74             has 'locale_path_directory' => (
75             is => 'ro',
76             from_config => 1,
77             lazy => 1,
78             default => sub {
79 0         0 './i18n'
80             }
81 4         1245 );
82              
83             has 'lang_session' => (
84             is => 'ro',
85             from_config => 1,
86 4         1458 default => sub { 'lang' }
87 4         1055 );
88              
89 4         1002 has 'locale_meta' => (
90             is => 'rw',
91             );
92              
93             }
94              
95              
96             sub BUILD {
97 4     4 0 6261 my $plugin = shift;
98             #Initialize Locale::Meta module
99 4         13 my $lm = Locale::Meta->new( $plugin->locale_path_directory );
100             #Set the locale::meta module as a variable of the plugin.
101 4         3883 $plugin->locale_meta($lm);
102             $plugin->app->add_hook( Dancer2::Core::Hook->new(
103             name => 'before_template_render',
104             code => sub {
105 0     0   0 my $tokens = shift;
106 0         0 $tokens->{l} = sub { loc($plugin, @_); };
  0         0  
107             }
108 4         129 ));
109              
110             }
111              
112             plugin_keywords ('loc','load_structure');
113             plugin_hooks ('charge');
114              
115             sub loc{
116 8     8 0 86867 my ($self, $str, $args, $force_lang) = @_;
117 8         49 my $app = $self->app;
118 8   33     147 my $lang = $force_lang || $app->session->read($self->lang_session) || $self->fallback;
119 8         300 my $msg = $self->locale_meta->loc($str,$lang,@$args);
120             #trying fallback
121 8 100       121 if( $msg eq $str ){
122 1         7 $msg = $self->locale_meta->loc($str,$self->fallback,@$args);
123             }
124 8         508 return $msg;
125             }
126              
127             sub load_structure {
128 6     6 0 194989 my ($self, $structure) = @_;
129 6         52 return $self->locale_meta->charge($structure);
130             }
131              
132             1;