Catmandu/Fix/i18n.pm

filter: Statementstmt 100% 41 / 41 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 96.2% 51 / 53 CC 2 SCAR 1.8
CC
2
Cov
98%
emit_value
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
« Locale/Maketext/Lexicon/CatmanduConfig.pm ↑ Summary
1 package Catmandu::Fix::i18n;
2 1 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Catmandu::Util qw(:is);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Catmandu::Fix::Has;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Catmandu::I18N;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 has path => ( fix_arg => 1 );
9  
10 1 has config => ( fix_opt => 1, required => 1 );
11  
12 1 has lang => ( fix_opt => 1, required => 1 );
13  
14 1 has args => ( fix_opt => 1 );
15  
16 1 has i18n => ( is => "lazy" );
17  
18 1 with "Catmandu::Fix::SimpleGetValue";
19  
20 2 ▶sub _build_i18n {
Subroutine _build_i18n: called CC=1 SCAR=0.0
21 2 my $self = $_[0];
22  
23 2 Catmandu::I18N->new(
24 config => $self->config(),
25 on_failure => "undef",
26 fallback_languages => []
27 );
28 }
29  
30 10 ▶sub emit_value {
Subroutine emit_value: called CC=2 SCAR=6.9
Pod undocumented
31 10 my ($self, $var, $fixer) = @_;
32  
33 10 my $perl = "";
34  
35 10 my $args = $fixer->generate_var();
36 10 $perl .= "my ${args} = [];";
37  
38 10 ▶ if(is_string($self->args)){
Branch: 1 branch
BranchTrueFalseTotal
if (is_string($self->args)) 10 0 10
39 10 my $path_args = $fixer->split_path($self->args);
40 10 my $key_args = pop @$path_args;
41 10 ▶ $perl .= $fixer->emit_walk_path($fixer->var,$path_args,sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 10 my $var = shift;
43 10 ▶ $fixer->emit_get_key($var,$key_args,sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
44 10 my $var = shift;
45 10 my $p = <<EOF;
46 ${args} = is_array_ref(${var}) ? ${var} : [];
47 EOF
48 10 $p;
49 })
50 10 });
51 }
52  
53 10 my $i18n = $fixer->capture( $self->i18n() );
54 10 my $lang = $self->lang();
55  
56 10 $perl .= <<EOF;
57 ${var} = ${i18n}\->t("${lang}",${var},\@${args});
58 EOF
59  
60 10 $perl;
61 }
62  
63 =encoding utf8
64  
65 =head1 NAME
66  
67 Catmandu::Fix::i18n - lookup value in I18N
68  
69 =head1 SYNOPSIS
70  
71 use Catmandu::Sane;
72 use Catmandu;
73  
74 #In your catmandu config
75  
76 Catmandu->config->{i18n} = {
77 en => [
78 "Gettext",
79 "/path/to/en.po"
80 ],
81 nl => [
82 "Gettext",
83 "/path/to/nl.po"
84 ]
85 };
86  
87 #In your fix
88  
89 #simple lookup
90 i18n( "title", config => "i18n", lang => "en" )
91  
92 #lookup with arguments
93 add_field("args.$append","Nicolas")
94 i18n( "greeting", config => "i18n", lang => "en", args => "args" )
95  
96 =head1 CONSTRUCTOR ARGUMENTS
97  
98 =over
99  
100 =item path
101  
102 * path to i18n key
103  
104 * specified as fix argument
105  
106 * required
107  
108 =item config
109  
110 * path to i18n configuration in the Catmandu config
111  
112 * specified as fix option
113  
114 * required
115  
116 =item lang
117  
118 * language to use
119  
120 * specified as fix option
121  
122 * required
123  
124 =item args
125  
126 * path in current record where arguments are stored. Must be an array.
127  
128 * specified as fix option
129  
130 * optional
131  
132 =back
133  
134 =head1 AUTHORS
135  
136 Nicolas Franck C<< <nicolas.franck at ugent.be> >>
137  
138 =head1 SEE ALSO
139  
140 L<Catmandu::I18N>, L<Catmandu>, L<Locale::Maketext>
141  
142 =head1 LICENSE AND COPYRIGHT
143  
144 This program is free software; you can redistribute it and/or modify it
145 under the terms of either: the GNU General Public License as published
146 by the Free Software Foundation; or the Artistic License.
147  
148 See L<http://dev.perl.org/licenses/> for more information.
149  
150 =cut
151  
152 1 1;
« Locale/Maketext/Lexicon/CatmanduConfig.pm ↑ Summary