1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
package HTML::FormFu::Localize;
use strict;
use HTML::FormFu::Util qw( require_class );
use List::MoreUtils qw( any );
use List::MoreUtils qw( pairwise );
use Scalar::Util qw( weaken isweak blessed );
use Exporter qw( import );
use Carp qw( croak );
our @EXPORT = qw(
localize
add_localize_object
add_localize_object_from_class
get_localize_object_from_class
get_localize_object_dies_on_missing_key
add_default_localize_object
get_localize_object
);
sub localize {
my ( $self, @original_strings ) = @_;
@original_strings = grep {defined} @original_strings;
if ( !$self->{has_default_localize_object} ) {
$self->add_default_localize_object;
}
my @localized_strings;
foreach my $localize_data ( @{ $self->{localize_data} } ) {
my $localize_object = $self->get_localize_object($localize_data);
eval {
@localized_strings = $localize_object->localize(@original_strings);
};
next if $@;
# NOTE:
# As FormFu uses L10N to return messages based on artificial message
# ids (instead of english language as message ids) the assumption
# that we just got a result from Locale::Maketext with AUTO = 1 seems
# to be safe when localize returns the same string as handed over.
if ( !$localize_data->{dies_on_missing_key}
&& scalar(@original_strings) == scalar(@localized_strings)
&& scalar( any { !$_ } pairwise { $a eq $b } @original_strings,
@localized_strings ) == 0
)
{
next;
}
last if @localized_strings;
}
if ( !@localized_strings ) {
@localized_strings = @original_strings;
}
return wantarray ? @localized_strings : $localized_strings[0];
}
sub add_localize_object {
my ( $self, @objects ) = @_;
croak 'no arguments given' if @_ < 2;
foreach my $localize_object (@objects) {
my $dies_on_missing_key = undef;
if ( blessed $localize_object) {
$dies_on_missing_key
= $self->get_localize_object_dies_on_missing_key(
$localize_object);
}
# add external localize object to the end of the list
push @{ $self->{localize_data} },
{
localize_object => $localize_object,
dies_on_missing_key => $dies_on_missing_key,
};
if ( !exists $self->{weaken_localize_object} || $self->{weaken_localize_object} != 0
&& !isweak @{ $self->{localize_data} }[-1]->{localize_object} )
{
weaken @{ $self->{localize_data} }[-1]->{localize_object};
} else {
delete $self->{weaken_localize_object};
}
}
return $self;
}
sub add_localize_object_from_class {
my ( $self, @class ) = @_;
$self->{weaken_localize_object} = 0;
return $self->add_localize_object(
map {
$self->get_localize_object_from_class( $_ )
} @class
);
}
sub get_localize_object_from_class {
my ( $self, $class ) = @_;
require_class($class);
my $languages = $self->languages;
return $class->get_handle(@$languages);
}
sub get_localize_object_dies_on_missing_key {
my ( $self, $localize_object ) = @_;
# NOTE:
# Findout how this class reacts on missing entries
# this is an issue with catalyst and po-style localization
# (in pm-style localization, you could set
# $Hello::I18N::en::Lexicon{_AUTO} = 0;
# to avoid autocreating missing keys)
# HINT:
# Never use underscores for the beginning of the testkey as they
# will lead Locale::Maketext to croak even if _AUTO is on (1) as
# Locale::Maketext useses underscores to identify text for
# processing via the AUTO-function (_compile).
my $testkey = 'html_formfu_missing_key_test';
eval { $localize_object->localize($testkey) };
my $dies_on_missing_key = $@ ? 1 : 0;
return $dies_on_missing_key;
}
sub add_default_localize_object {
my ($self) = @_;
my $localize_object
= $self->get_localize_object_from_class( $self->localize_class );
my $dies_on_missing_key = 1;
# put FormFu localize object in first place
unshift @{ $self->{localize_data} },
{
localize_object => $localize_object,
dies_on_missing_key => $dies_on_missing_key,
};
$self->{has_default_localize_object} = 1;
return $self;
}
sub get_localize_object {
my ( $self, $localize_data ) = @_;
if ( !blessed $localize_data->{localize_object} ) {
$localize_data->{localize_object}
= $self->get_localize_object_from_class( $self->localize_class );
$localize_data->{dies_on_missing_key}
= $self->get_localize_object_dies_on_missing_key(
$localize_data->{localize_object} );
}
return $localize_data->{localize_object};
}
1;
|