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.40068';
4 143     143   138319 use HTML::FormHandler::I18N;
  143         10006  
  143         4848  
5 143     143   1179 use Moose::Role;
  143         389  
  143         1610  
6 143     143   836610 use Moose::Util::TypeConstraints;
  143         506  
  143         1511  
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 134     134   422 my ($self) = @_;
18              
19 134 50 33     1205 if (!$self->isa('HTML::FormHandler') && $self->has_form) {
20 0         0 return $self->form->language_handle();
21             }
22 134         348 my $lh;
23 134 100       636 if ( $ENV{LANGUAGE_HANDLE} ) {
24 24 50       232 if ( blessed $ENV{LANGUAGE_HANDLE} ) {
25 0         0 $lh = $ENV{LANGUAGE_HANDLE};
26             }
27             else {
28 24         457 $lh = HTML::FormHandler::I18N->get_handle( $ENV{LANGUAGE_HANDLE} );
29             }
30             }
31             else {
32 110         1590 $lh = HTML::FormHandler::I18N->get_handle;
33             }
34 134         18899 return $lh;
35             }
36              
37             sub _localize {
38 20     20   100 my ($self, @message) = @_;
39 20         590 my $message = $self->language_handle->maketext(@message);
40 20         74 return $message;
41             }
42              
43 143     143   363233 no Moose::Role;
  143         417  
  143         961  
44 143     143   39883 no Moose::Util::TypeConstraints;
  143         472  
  143         806  
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.40068
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) 2017 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