Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Language or string templates?

by Seumas (Curate)
on Oct 02, 2004 at 03:35 UTC ( #395815=perlquestion: print w/ replies, xml ) Need Help??
Seumas has asked for the wisdom of the Perl Monks concerning the following question:

In mucking around with some PHP scripts, I noticed authors frequently set aside a file for language/text values. That is, rather than editing templates directly, they have a single file that contains entries like the following:
$GLOBALS['strCliDeactivated'] = "Currently not active"; $GLOBALS['strNotActive'] = "date has not yet been reached"; $GLOBALS['strExpired'] = "date has been reached";
Then they refer to the $GLOBALS that they want to use at any particular point in a template.

I like the idea of doing this rather than having to manually edit dozens of template files individually. However, I'm not sure what ways there are to do this in perl. One obvious way is to just use something like Config::ApacheFormat and create a config object just for strings or language. That way I just edit the one file.

But what other ways are suggested? Or are there other modules that will handle this that I am unaware of? Or maybe this is a bad idea to begin with and I shouldn't try going down this road?

I use HTML::Template, though I would not be averse to eventually switching to another system at some point.

Comment on Language or string templates?
Select or Download Code
Re: Language or string templates?
by ikegami (Pope) on Oct 02, 2004 at 03:50 UTC
    There are a number of modules on CPAN to read configuration files. Config::IniFiles is an example. You could have a section for each language. It even provides a interface to tie to a global:

    use Config::IniFiles; tie %GLOBALS, 'Config::IniFiles', ( -file => "/path/configfile.ini" ); print "We have $GLOBALS{$lang}{Parameter}." if $GLOBALS{$lang}{Parameter};

    or

    use Config::IniFiles; my %ini; tie %ini, 'Config::IniFiles', ( -file => "/path/configfile.ini" ); %GLOBALS = %{$ini{$lang}}; print "We have $GLOBALS{Parameter}." if $GLOBALS{Parameter};

    or you can simply do "filename" to execute its content as if it was in the current file. File filename could contain something like:

    $GLOBALS{'strCliDeactivated'} = "Currently not active"; $GLOBALS{'strNotActive'} = "date has not yet been reached"; $GLOBALS{'strExpired'} = "date has been reached";
Re: Language or string templates?
by BUU (Prior) on Oct 02, 2004 at 03:53 UTC
    In this case, $GLOBALS is an example of a Phrasebook (pattern or paradigm or whatever). Theres a large amount of various material on the web and more specifically on cpan discussing phrasebooks and implementing them. Theres also several nodes on Perlmonks.

    Generally people tend to apply Phrasebooks to SQL dialects, but theres no real reason that you couldn't use them for language strings. I would suspect language-specific Phrasebooks would only suffice for the must simplistic systems, where you have a very tiny amount of text to actually modify. I would think that in a larger system you would be better off just having multiple templates, one for each language, or perhaps a language template and a style template.
Re: Language or string templates?
by Prior Nacre V (Hermit) on Oct 02, 2004 at 04:12 UTC

    Here's a fairly straightforward solution framework that may be suitable for your application.

    Create a text/language file with this format:

    . . . strCliDeactivated => 'Currently not active', strNotActive => 'date has not yet been reached', strExpired => 'date has been reached', . . .

    Then in your main code, slurp in the whole file and create a hash which can then be used when required.

    . . . my @global_data = <TEXT_LANG>; my %globals; eval "\%globals = ( @global_data )"; . . . print STDERR $globals{strExpired} if $expired; . . .

    Update: Sorry, I had two solutions in my head - I posted half of each. I've added two lines to the main code replacing:

    my %globals = ( @global_data );

    Apologies for any confusion.

    Regards,

    PN5

Re: Language or string templates?
by borisz (Canon) on Oct 02, 2004 at 10:22 UTC
    Take a look at the various gettext modules.
    Boris
Re: Language or string templates?
by pingo (Hermit) on Oct 03, 2004 at 08:50 UTC
    I have used the language text files approach, but I've stopped doing that since it turned out to be a bit messy. The people editing the language text files tended to stuff html into the strings ("This is an error message, so it should be red."). Also, when they wanted to add some text to the page they just put it in the template file, making the language file semi-obsolete.

    Of course, if you are the only one who is going to be doing the editing, I guess you are not likely to run into the above problems, so go right ahead. :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://395815]
Approved by ikegami
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-10-01 01:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (386 votes), past polls