This is a bit of a diversion, but...
On Wed, 2008-10-29 at 22:24 -0500, Lee.M wrote:
> Locale::Maketext::Lexicon can do .po files, and it now has special TT
> and YAML formats although I've got to say I'm not a fan of the gettext
> style _(thingy)... too ambiguous but that's just me :)
Actually, TT is the one place I can't use the _('xxx') form, because the
parser is built to recognise _ as the concat operator.
One of the nice things about using the _('text') form in Perl code is
that the _ symbol is global.
This means that in your i18n module you can do:
our $Current_Lang;
*::_ = sub { $Current_Lang->maketext(@_) };
and then without any using / importing in any of your other modules, you
automatically have the _() sub available to you.
Actually, I do something else in addition to handle strings that are
defined at compile time, rather than run time.
In my i18n module, I have:
-------------------------------------------------------------------
{
no warnings 'redefine';
sub compile_mode {
*::_ = sub { MyApp::i18n::String->new(@_) };
}
sub run_mode {
*::_ = sub { $MyApp::i18n::Current_Lang->maketext(@_) };
}
}
BEGIN {
compile_mode();
}
-------------------------------------------------------------------
(You also need a line somewhere once your app has finished loading which
will switch you to run_mode, ie MyApp::i18n::run_mode)
So any strings that are defined at compile time can also be written as:
our @months = _('January'), _('February')....
and instead of getting immediately translated, they are blessed into the
MyApp::i18n::String class, which is pretty simple:
-------------------------------------------------------------------
package Burro::i18n::String;
use strict;
use warnings FATAL => 'all', NONFATAL => 'redefine';
use overload ( q{""} => \&translate );
#===================================
sub new {
#===================================
my $class = shift;
my $string = shift;
return bless( \$string, $class );
}
#===================================
sub translate {
#===================================
my $self = shift;
no warnings 'once';
return $MyApp::i18n::Current_Lang->maketext( ${$self} );
}
#===================================
sub original {
#===================================
my $self = shift;
return $$self;
}
1;
-------------------------------------------------------------------
If I need to require any futher modules at run time, instead of using
the standard require(), I use:
#===================================
sub i18n_require {
#===================================
my $original = my $path = shift;
MyApp::i18n::compile_mode();
$path=~s{::}{/}g;
my $error;
eval {require $path.'.pm'}
or $error = $@ || 'Unknown error';
MyApp::i18n::run_mode();
die( sprintf( "Error loading '%s' at %s line %d:\n%s\n",
$original, (caller)[ 1, 2 ], $error
)
) if $error;
return $INC{$path};
}
clint
_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates