| 1 | package HTML::FormHandler::TraitFor::I18N; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: localization | ||||||||||||||||||||||||||||
| 3 | 146 | $HTML::FormHandler::TraitFor::I18N::VERSION = '0.410000'; | |||||||||||||||||||||||||||
| 4 | 146 | ▶ | use HTML::FormHandler::I18N; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 146 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 146 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 146 | 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 | 144 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_language_handle: called CC=5 SCAR=17.6
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 144 | ▶ | if (!$self->isa('HTML::FormHandler') && $self->has_form) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $self->isa("HTML::FormHandler") and $self->has_form
MC/DC0%0/2
not $self->isa("HTML::FormHandler") and $self->has_form
not $self->isa("HTML::FormHandler")
$self->has_form
Truth table33%1/3
not(A) and B
A= not $self->isa("HTML::FormHandler")B= $self->has_form
| |||||||||||||||||||||||||||||
| 20 | 0 | return $self->form->language_handle(); | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | 144 | my $lh; | |||||||||||||||||||||||||||
| 23 | 144 | ▶ | if ( $ENV{LANGUAGE_HANDLE} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 24 | ▶ | if ( blessed $ENV{LANGUAGE_HANDLE} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 0 | $lh = $ENV{LANGUAGE_HANDLE}; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | else { | ||||||||||||||||||||||||||||
| 28 | 24 | $lh = HTML::FormHandler::I18N->get_handle( $ENV{LANGUAGE_HANDLE} ); | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | else { | ||||||||||||||||||||||||||||
| 32 | 120 | $lh = HTML::FormHandler::I18N->get_handle; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 144 | return $lh; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | sub _localize { | ||||||||||||||||||||||||||||
| 38 | 20 | ▶ | my ($self, @message) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_localize: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 20 | my $message = $self->language_handle->maketext(@message); | |||||||||||||||||||||||||||
| 40 | 20 | return $message; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 146 | ▶ | no Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 146 | ▶ | no Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 146 | 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.410000 | ||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||