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   1425250 use strict;
  4         7  
  4         152  
6 4     4   22 use warnings;
  4         6  
  4         151  
7 4     4   1982 use Dancer2::Plugin;
  4         45231  
  4         26  
8 4     4   10654 use Locale::Meta;
  4         3362  
  4         357  
9              
10             our $VERSION = '0.005';
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             ## Getting the translation
29             get '/' => sub {
30             my $greeting = loc("hello");
31             template index.tt, { greeting => $greeting }
32             }
33            
34             ## Getting locale_meta attribute
35             my $locale_meta = locale_meta;
36              
37             # in your template
38              
39             <% l('greeting') %>
40              
41             # load custom structure on your app
42              
43              
44             my $structure = {
45             "en" => {
46             "goodbye" => {
47             "trans" => "bye",
48             }
49             },
50             "es" => {
51             "goodbye" => {
52             "trans" => "chao",
53             }
54             }
55             };
56              
57             In order to load the data use the keyword on your routes:
58              
59             load_structure($structure);
60              
61              
62             =head1 CONFIGURATION
63              
64             plugins:
65             Locale::Meta:
66             fallback: "en"
67             locale_path_directory: "i18n"
68             lang_session: "lang"
69             =cut
70              
71             BEGIN{
72             has 'fallback' => (
73             is => 'ro',
74             from_config => 1,
75             default => sub {}
76 4     4   113 );
77              
78             has 'locale_path_directory' => (
79             is => 'ro',
80             from_config => 1,
81             lazy => 1,
82             default => sub {
83 0         0 './i18n'
84             }
85 4         1240 );
86              
87             has 'lang_session' => (
88             is => 'ro',
89             from_config => 1,
90 4         1777 default => sub { 'lang' }
91 4         1112 );
92              
93 4         1087 has 'locale_meta' => (
94             is => 'rw',
95             );
96              
97             }
98              
99              
100             sub BUILD {
101 4     4 0 6637 my $plugin = shift;
102             #Initialize Locale::Meta module
103 4         13 my $lm = Locale::Meta->new( $plugin->locale_path_directory );
104             #Set the locale::meta module as a variable of the plugin.
105 4         3959 $plugin->locale_meta($lm);
106             $plugin->app->add_hook( Dancer2::Core::Hook->new(
107             name => 'before_template_render',
108             code => sub {
109 0     0   0 my $tokens = shift;
110 0         0 $tokens->{l} = sub { loc($plugin, @_); };
  0         0  
111             }
112 4         131 ));
113              
114             }
115              
116             plugin_keywords ('loc','load_structure','locale_meta');
117             plugin_hooks ('charge');
118              
119             sub loc{
120 8     8 0 107863 my ($self, $str, $args, $force_lang) = @_;
121 8         57 my $app = $self->app;
122 8   33     187 my $lang = $force_lang || $app->session->read($self->lang_session) || $self->fallback;
123 8         392 my $msg = $self->locale_meta->loc($str,$lang,@$args);
124             #trying fallback
125 8 100       149 if( $msg eq $str ){
126 1         9 $msg = $self->locale_meta->loc($str,$self->fallback,@$args);
127             }
128 8         838 return $msg;
129             }
130              
131             sub load_structure {
132 6     6 0 217242 my ($self, $structure) = @_;
133 6         73 return $self->locale_meta->charge($structure);
134             }
135              
136             1;