| 1 |
package HTML::FormHandler::TraitFor::I18N; |
| 2 |
# ABSTRACT: localization |
| 3 |
$HTML::FormHandler::TraitFor::I18N::VERSION = '0.410002'; |
| 4 |
use HTML::FormHandler::I18N; |
| 5 |
use Moose::Role; |
| 6 |
use Moose::Util::TypeConstraints; |
| 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 |
my ($self) = @_; |
| 18 |
|
| 19 |
if (!$self->isa('HTML::FormHandler') && $self->has_form) { |
| 20 |
return $self->form->language_handle(); |
| 21 |
} |
| 22 |
my $lh; |
| 23 |
if ( $ENV{LANGUAGE_HANDLE} ) { |
| 24 |
if ( blessed $ENV{LANGUAGE_HANDLE} ) { |
| 25 |
$lh = $ENV{LANGUAGE_HANDLE}; |
| 26 |
} |
| 27 |
else { |
| 28 |
$lh = HTML::FormHandler::I18N->get_handle( $ENV{LANGUAGE_HANDLE} ); |
| 29 |
} |
| 30 |
} |
| 31 |
else { |
| 32 |
$lh = HTML::FormHandler::I18N->get_handle; |
| 33 |
} |
| 34 |
return $lh; |
| 35 |
} |
| 36 |
|
| 37 |
sub _localize { |
| 38 |
my ($self, @message) = @_; |
| 39 |
my $message = $self->language_handle->maketext(@message); |
| 40 |
return $message; |
| 41 |
} |
| 42 |
|
| 43 |
no Moose::Role; |
| 44 |
no Moose::Util::TypeConstraints; |
| 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.410002 |
| 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 |