CGI-FormMagick
view release on metacpan or search on metacpan
lib/CGI/FormMagick/L10N.pm view on Meta::CPAN
Form titles
=item *
Page titles and descriptions
=item *
Field labels and descriptions
=item *
Validation error messages
=back
If you wish to localise other textual information such as your HTML
Templates, you will have to explicitly call the l10n routines.
=head1 USER METHODS
=head2 localise($string)
Translates a string into the end-user's preferred language by checking
their HTTP_ACCEPT_LANG variable and looking up a lexicon hash for that
language (if it exists). If no translation can be found, returns the
original string untranslated.
Takes the text to translate as the first argument, and optionally, a hashref of
variables for substitution as the second argument.
WARNING WARNING WARNING: The internals of this routine will change
significantly in version 0.60, when we remove Locale::Maketext from
the equation. However, its output should still be the same. Just FYI.
=begin testing
BEGIN: {
use_ok('CGI::FormMagick');
use vars qw($fm);
use lib "lib/";
}
$ENV{HTTP_ACCEPT_LANGUAGE} = 'fr, en, de';
my $fm = CGI::FormMagick->new(type => 'file', source => "t/lexicon.xml");
$fm->parse_xml(); # suck in lexicon without display()ing
is($fm->localise("yes"), "oui", "Simple localisation");
is($fm->localise("Hello"), "Bonjour", "Simple localisation");
is($fm->localise("xyz"), "xyz", "Attempted localisation of untranslated string");
is($fm->localise(""), "", "Fail gracefully on localisation of empty string");
# Lexicon variable substitution tests
{
package MyFormMagick;
our @ISA = ('CGI::FormMagick');
sub new {
shift;
my $self = CGI::FormMagick->new(@_);
$self->{calling_package} = (caller)[0];
return bless $self;
}
sub getLexiconParams {
return (params_var => "'params' method variable");
}
}
my $mfm = MyFormMagick->new(type=> 'file', source => "t/lexicon-params.xml");
$mfm->parse_xml();
is($mfm->localise('This text contains a {$var}.', {var => "variable"}),
"A variable this text contains.",
"Lexicon variable substitution from hashref arg to localise()");
is($mfm->localise('This text contains a {$var}.'),
"A lexicon variable this text contains.",
"Lexicon variable substitution from lexicon entry");
is($mfm->localise('This text contains a {$params_var}.'),
"A 'params' method variable this text contains.",
"Lexicon variable substitution from 'params' subclass method");
=end testing
=cut
sub localise {
my ($fm, $string, $hashref) = @_;
$string = "" unless defined $string;
$hashref = {} unless keys %$hashref;
my $text;
my %params = $fm->{lexicon} ? %{$fm->{lexicon}} : ();
%params = (%params, $fm->_get_lexicon_params(), %$hashref);
if (my $trans = $fm->{lexicon}->{$string}) {
$text = fill_in_string($trans, HASH=>\%params);
} else {
$text = fill_in_string($string, HASH=>\%params);
}
return $text;
}
=pod
=head2 check_l10n()
print out lexicons to check whether they're what you think they are
this is mostly for debugging purposes. If you have DEBUG set to 1 in
your call to the new() method, you'll see a link at the bottom of each
page that says "Check L10N". This is the subroutine that's called when
you follow that link.
=cut
sub check_l10n {
my $self = shift;
print qq( <p>Your choice of language: $ENV{HTTP_ACCEPT_LANGUAGE}</p>);
my @langs = split(/, /, $ENV{HTTP_ACCEPT_LANGUAGE});
foreach my $lang (@langs) {
print qq(<h2>Language: $lang</h2>);
}
}
( run in 2.433 seconds using v1.01-cache-2.11-cpan-8dfa8b56332 )