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


In reply to Re: Translation modules and the importation of global language variables by BrowserUk
in thread Translation modules and the importation of global language variables by traxlog

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.