in reply to Translation modules and the importation of global language variables

Would this meet your needs?

#! perl -slw use strict; use vars qw[ $LANG ]; BEGIN{ $LANG ||= 'ENGLISH'; } use Language $LANG; print RED, GREEN, BLUE; print "The RED ballon floated over the GREEN fields in the BLUE sky"; { no Language; print RED, GREEN, BLUE; print "The RED ballon floated over the GREEN fields in the BLUE sk +y"; } print RED, GREEN, BLUE; print "The RED ballon floated over the GREEN fields in the BLUE sky"; __END__ P:\test>junk redgreenblue The red ballon floated over the green fields in the blue sky redgreenblue The RED ballon floated over the GREEN fields in the BLUE sky redgreenblue The red ballon floated over the green fields in the blue sky P:\test>junk -LANG=DUTCH roodgroenblauw The rood ballon floated over the groen fields in the blauw sky roodgroenblauw The RED ballon floated over the GREEN fields in the BLUE sky roodgroenblauw The rood ballon floated over the groen fields in the blauw sky P:\test>junk -LANG=FRENCH rougevertbleu The rouge ballon floated over the vert fields in the bleu sky rougevertbleu The RED ballon floated over the GREEN fields in the BLUE sky rougevertbleu The rouge ballon floated over the vert fields in the bleu sky

The language is choosen by a command line switch -LANG=DUTCH. This could be converted to use one of the GetOpt::* modules easily, if they're your thing.

The module (Langauge) exports constants for the words which will also be interpolated into string constants. This could be extended to interpolation into regexes as well if that was a requirement.

Caveats

You can also change languages on the fly, though currently this will produce a warning for each constant that is redefined -- I haven't found a way to suppress this yet, which is why I haven't included it in the demonstration.

Also, changing langauges is not lexically constrained. Once it is changed, it remains changed at all scopes, though you can always change it back. You can however, disable the interpolation lexically.

The module

package Language; use Inline::Files; use strict; use overload; my %words; sub import { no strict 'refs'; my $caller = caller; my( $class, $lang ) = @_; $lang = uc $lang; my $fh = \*{$lang}; while( <$fh> ) { my( $english, $trans ) = m[(\w+)\s*=\s*(\w+)]; $words{ $english } = $trans; eval qq[ # line 1 "Language::import" *${caller}::$english = bless sub() { '$trans' }, '$class'; ]; } overload::constant ( q => \&interp ); } sub unimport { overload::remove_constant ( q => \&interp ); } sub interp{ my( $orig, $perl, $use) = @_; $orig =~ s[\b([A-Z]+)\b]{ exists $words{ $1 } ? $words{ $1 } : $1 }ge; $orig; } 1; __ENGLISH__ RED = red BLUE = blue GREEN = green __FRENCH__ RED = rouge BLUE = bleu GREEN = vert __DUTCH__ RED = rood BLUE = blauw GREEN = groen

It uses Inline::Files to incorporate all the languages into the module file for compactness, but this could easily be changed to use separate files.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail