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
|
|
1373267
|
use strict; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
120
|
|
6
|
4
|
|
|
4
|
|
19
|
use warnings; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
141
|
|
7
|
4
|
|
|
4
|
|
2048
|
use Dancer2::Plugin; |
|
4
|
|
|
|
|
46833
|
|
|
4
|
|
|
|
|
23
|
|
8
|
4
|
|
|
4
|
|
10675
|
use Locale::Meta; |
|
4
|
|
|
|
|
3276
|
|
|
4
|
|
|
|
|
357
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.006'; |
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
|
|
110
|
); |
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
|
|
|
|
|
1161
|
); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
has 'lang_session' => ( |
88
|
|
|
|
|
|
|
is => 'ro', |
89
|
|
|
|
|
|
|
from_config => 1, |
90
|
4
|
|
|
|
|
1519
|
default => sub { 'lang' } |
91
|
4
|
|
|
|
|
1105
|
); |
92
|
|
|
|
|
|
|
|
93
|
4
|
|
|
|
|
1107
|
has 'locale_meta' => ( |
94
|
|
|
|
|
|
|
is => 'rw', |
95
|
|
|
|
|
|
|
); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub BUILD { |
101
|
4
|
|
|
4
|
0
|
7102
|
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
|
|
|
|
|
4227
|
$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
|
|
|
|
|
137
|
)); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
plugin_keywords ('loc','load_structure','locale_meta'); |
117
|
|
|
|
|
|
|
plugin_hooks ('charge'); |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub loc{ |
120
|
8
|
|
|
8
|
0
|
90486
|
my ($self, $str, $args, $force_lang) = @_; |
121
|
8
|
|
|
|
|
43
|
my $app = $self->app; |
122
|
8
|
|
33
|
|
|
145
|
my $lang = $force_lang || $app->session->read($self->lang_session) || $self->fallback; |
123
|
8
|
|
|
|
|
301
|
my $msg = $self->locale_meta->loc($str,$lang,@$args); |
124
|
|
|
|
|
|
|
#trying fallback |
125
|
8
|
100
|
|
|
|
112
|
if( $msg eq $str ){ |
126
|
1
|
|
|
|
|
6
|
$msg = $self->locale_meta->loc($str,$self->fallback,@$args); |
127
|
|
|
|
|
|
|
} |
128
|
8
|
|
|
|
|
696
|
return $msg; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub load_structure { |
132
|
6
|
|
|
6
|
0
|
199781
|
my ($self, $structure) = @_; |
133
|
6
|
|
|
|
|
51
|
return $self->locale_meta->charge($structure); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
1; |