| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use Test::Exception; |
| 5 |
use HTML::FormHandler::Field::Text; |
| 6 |
|
| 7 |
use lib ('t/lib'); |
| 8 |
|
| 9 |
# ensure $ENV is properly set up |
| 10 |
my @LH_VARS = ('LANGUAGE_HANDLE', 'HTTP_ACCEPT_LANGUAGE', 'LANG', 'LANGUAGE' ); |
| 11 |
my %LOC_ENV; |
| 12 |
$LOC_ENV{$_} = delete $ENV{$_} for @LH_VARS; |
| 13 |
|
| 14 |
# a primitive translation package |
| 15 |
{ |
| 16 |
package HTML::FormHandler::I18N::xx_xx; |
| 17 |
use base 'HTML::FormHandler::I18N'; |
| 18 |
|
| 19 |
# Auto define lexicon |
| 20 |
our %Lexicon = ( |
| 21 |
'_AUTO' => 1, |
| 22 |
'You lost, insert coin' => 'Not won, coin needed', |
| 23 |
'Test field' => 'Grfg svryq', |
| 24 |
); |
| 25 |
} |
| 26 |
|
| 27 |
# a simple demo form |
| 28 |
{ |
| 29 |
package Test::Form; |
| 30 |
use HTML::FormHandler::Moose; |
| 31 |
extends 'HTML::FormHandler'; |
| 32 |
|
| 33 |
has_field 'test_field'; |
| 34 |
} |
| 35 |
|
| 36 |
my $form; |
| 37 |
|
| 38 |
|
| 39 |
################ Locale -none- |
| 40 |
|
| 41 |
$ENV{LANGUAGE} = 'en-US'; |
| 42 |
|
| 43 |
# create form w/o locale must work |
| 44 |
lives_ok { $form = Test::Form->new } 'create form w/o locale lives'; |
| 45 |
ok($form, 'create form w/o locale'); |
| 46 |
is(ref($form->language_handle), 'HTML::FormHandler::I18N::en_us', 'locale en_us'); |
| 47 |
|
| 48 |
# ensure we know / don't know the translations |
| 49 |
$HTML::FormHandler::I18N::en_us::Lexicon{'You lost, insert coin'} = 'XX Dummy 42'; |
| 50 |
$HTML::FormHandler::I18N::en_us::Lexicon{'Must insert a [_1] coin'} = 'Want a [_1] coin'; |
| 51 |
delete $HTML::FormHandler::I18N::en_us::Lexicon{'Test field'}; |
| 52 |
delete $HTML::FormHandler::I18N::en_us::Lexicon{'You won'}; |
| 53 |
|
| 54 |
# translating a known error works |
| 55 |
$form->field('test_field')->clear_errors; |
| 56 |
$form->field('test_field')->add_error('You lost, insert coin'); |
| 57 |
is_deeply($form->field('test_field')->errors, ['XX Dummy 42'], 'error is translated into en_us'); |
| 58 |
|
| 59 |
# translating a known label |
| 60 |
is($form->field('test_field')->label, 'Test field', 'Label w/o translation = ok'); |
| 61 |
|
| 62 |
# translating a known error with a positional parameter |
| 63 |
$form->field('test_field')->clear_errors; |
| 64 |
$form->field('test_field')->add_error('Must insert a [_1] coin', 'cleaned'); |
| 65 |
is_deeply($form->field('test_field')->errors, ['Want a cleaned coin'], 'error w/parameter is translated into en_us'); |
| 66 |
|
| 67 |
# translating an unknown error also works |
| 68 |
$form->field('test_field')->clear_errors; |
| 69 |
$form->field('test_field')->add_error('You won'); |
| 70 |
is_deeply($form->field('test_field')->errors, ['You won'], 'error is translated into en_us'); |
| 71 |
|
| 72 |
# translating an error with bracket issues |
| 73 |
$form->field('test_field')->clear_errors; |
| 74 |
dies_ok( sub { $form->field('test_field')->add_error('You are not authorized for this archive. See: [<a href="/help/auth">more information</a>], [<a href="/need_auth">request authorization</a>]') }, 'dies on maketext error' ); |
| 75 |
lives_ok( sub { $form->field('test_field')->add_error('An error with ~[escaped brackets~]'); }, |
| 76 |
'does not die when brackets are escaped with tilde' ); |
| 77 |
|
| 78 |
################ Locale xx_xx set via ENV{LANGUAGE_HANDLE} |
| 79 |
$ENV{LANGUAGE_HANDLE} = 'xx_xx'; |
| 80 |
|
| 81 |
# create form w/ locale must work |
| 82 |
undef $form; |
| 83 |
lives_ok { $form = Test::Form->new } 'create form w/ locale lives'; |
| 84 |
ok($form, 'create form w/ locale'); |
| 85 |
is(ref($form->language_handle), 'HTML::FormHandler::I18N::xx_xx', 'locale xx_xx'); |
| 86 |
|
| 87 |
# translating a known error works |
| 88 |
$form->field('test_field')->clear_errors; |
| 89 |
$form->field('test_field')->add_error('You lost, insert coin'); |
| 90 |
is_deeply($form->field('test_field')->errors, ['Not won, coin needed'], 'error is translated into xx_xx'); |
| 91 |
|
| 92 |
# translating an unknown error also works |
| 93 |
$form->field('test_field')->clear_errors; |
| 94 |
$form->field('test_field')->add_error('You won'); |
| 95 |
is_deeply($form->field('test_field')->errors, ['You won'], 'error is translated into xx_xx'); |
| 96 |
|
| 97 |
# translating a known label |
| 98 |
is($form->field('test_field')->loc_label, 'Grfg svryq', 'label rot13 to xx_xx'); |
| 99 |
|
| 100 |
# remove from environment variable, so we can use builder |
| 101 |
delete $ENV{LANGUAGE_HANDLE}; |
| 102 |
{ |
| 103 |
package MyApp::Test::Form; |
| 104 |
use HTML::FormHandler::Moose; |
| 105 |
extends 'HTML::FormHandler'; |
| 106 |
use MyApp::I18N::abc_de; |
| 107 |
|
| 108 |
sub _build_language_handle { MyApp::I18N::abc_de->new } |
| 109 |
has_field 'foo'; |
| 110 |
has_field 'bar'; |
| 111 |
sub validate_foo { |
| 112 |
my ( $self, $field ) = @_; |
| 113 |
$field->add_error('You lost, insert coin'); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
$form = MyApp::Test::Form->new; |
| 118 |
|
| 119 |
ok( $form, 'form built' ); |
| 120 |
$form->process( params => { foo => 'test' } ); |
| 121 |
is( $form->field('foo')->errors->[0], 'Loser! coin needed', 'right message' ); |
| 122 |
is( ref $form->language_handle, 'MyApp::I18N::abc_de', 'using right lh'); |
| 123 |
is( ref $form->field('foo')->language_handle, 'MyApp::I18N::abc_de', 'field uses form language handle' ); |
| 124 |
|
| 125 |
$form->process( language_handle => HTML::FormHandler::I18N->get_handle('en_us'), params => { foo => 'byebye' } ); |
| 126 |
my $field_lh = $form->field('bar')->language_handle; |
| 127 |
is( ref $field_lh, 'HTML::FormHandler::I18N::en_us', 'field uses form language handle' ); |
| 128 |
|
| 129 |
$ENV{$_} = 'en-US' for @LH_VARS; |
| 130 |
|
| 131 |
done_testing; |