File Coverage

blib/lib/HTML/FormHandler/TraitFor/I18N.pm
Criterion Covered Total %
statement 26 28 92.8
branch 4 6 66.6
condition 1 3 33.3
subroutine 7 7 100.0
pod n/a
total 38 44 86.3


line stmt bran cond sub pod time code
1             package HTML::FormHandler::TraitFor::I18N;
2             # ABSTRACT: localization
3             $HTML::FormHandler::TraitFor::I18N::VERSION = '0.40067';
4 141     141   111609 use HTML::FormHandler::I18N;
  141         744  
  141         12811  
5 141     141   3508 use Moose::Role;
  141         1107  
  141         1217  
6 141     141   522553 use Moose::Util::TypeConstraints;
  141         223  
  141         1162  
7              
8              
9             has 'language_handle' => (
10             isa => duck_type( [ qw(maketext) ] ),
11             is => 'rw',
12             lazy_build => 1,
13             required => 1,
14             );
15              
16             sub _build_language_handle {
17 132     132   233 my ($self) = @_;
18              
19 132 50 33     882 if (!$self->isa('HTML::FormHandler') && $self->has_form) {
20 0         0 return $self->form->language_handle();
21             }
22 132         196 my $lh;
23 132 100       501 if ( $ENV{LANGUAGE_HANDLE} ) {
24 24 50       102 if ( blessed $ENV{LANGUAGE_HANDLE} ) {
25 0         0 $lh = $ENV{LANGUAGE_HANDLE};
26             }
27             else {
28 24         496 $lh = HTML::FormHandler::I18N->get_handle( $ENV{LANGUAGE_HANDLE} );
29             }
30             }
31             else {
32 108         1452 $lh = HTML::FormHandler::I18N->get_handle;
33             }
34 132         13300 return $lh;
35             }
36              
37             sub _localize {
38 19     19   65 my ($self, @message) = @_;
39 19         536 my $message = $self->language_handle->maketext(@message);
40 19         51 return $message;
41             }
42              
43 141     141   197224 no Moose::Role;
  141         230  
  141         641  
44 141     141   22097 no Moose::Util::TypeConstraints;
  141         206  
  141         811  
45              
46             1;
47              
48             __END__
49              
50             =pod
51              
52             =encoding UTF-8
53              
54             =head1 NAME
55              
56             HTML::FormHandler::TraitFor::I18N - localization
57              
58             =head1 VERSION
59              
60             version 0.40067
61              
62             =head3 language_handle, _build_language_handle
63              
64             Holds a Locale::Maketext (or other duck_type class with a 'maketext'
65             method) language handle. The language handle is used to localize the
66             error messages in the field's 'add_error' method. It's also used
67             in various places in rendering to localize labels and button values,
68             etc.
69              
70             The builder for this attribute gets the Locale::Maketext language
71             handle from the environment variable $ENV{LANGUAGE_HANDLE}:
72              
73             $ENV{LANGUAGE_HANDLE} = 'en_en';
74              
75             ...or creates a default language handler using L<HTML::FormHandler::I18N>.
76             (Note that earlier versions required an actual object reference in ENV,
77             which is a bad practice and no longer supported.)
78             You can pass in an existing L<Locale::MakeText> subclass instance
79             or create one in a builder.
80              
81             In a form class:
82              
83             sub _build_language_handle { MyApp::I18N::abc_de->new }
84              
85             Passed into new or process:
86              
87             my $lh = MyApp::I18N::abc_de->new;
88             my $form = MyApp::Form->new( language_handle => $lh );
89              
90             If you do not set the language_handle, then L<Locale::Maketext> and/or
91             L<I18N::LangTags> may guess, with unexpected results.
92              
93             You can use non-Locale::Maketext language handles, such as L<Data::Localize>.
94             There's an example of building a L<Data::Localize> language handle
95             in t/xt/locale_data_localize.t in the distribution.
96              
97             If you don't want a particular error message to go through localization,
98             you can use 'push_errors' and 'push_form_errors' instead of 'add_error' and
99             'add_form_errors'.
100              
101             Example of getting the language handle from the Catalyst context (where the Catalyst
102             context is passed in with 'ctx'):
103              
104             has '+language_handle' => ( builder => 'get_language_handle_from_ctx' );
105             sub get_language_handle_from_ctx {
106             my $self = shift;
107             return MyApp::I18N->get_handle(
108             @{ $self->ctx->languages } );
109             }
110              
111             =head1 AUTHOR
112              
113             FormHandler Contributors - see HTML::FormHandler
114              
115             =head1 COPYRIGHT AND LICENSE
116              
117             This software is copyright (c) 2016 by Gerda Shank.
118              
119             This is free software; you can redistribute it and/or modify it under
120             the same terms as the Perl 5 programming language system itself.
121              
122             =cut