Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

locale for date in Template Toolkit

by RenardBleu (Sexton)
on Feb 19, 2014 at 15:00 UTC ( #1075464=perlquestion: print w/replies, xml ) Need Help??
RenardBleu has asked for the wisdom of the Perl Monks concerning the following question:

Hi, i try to get the date displayed according to the client language. This code
[% USE date(format = '%A %d %B %Y %H:%M', locale = 'en_US') %] [% date.format %]
works fine for fr_FR and en_US but returns the fr_FR version for de_DE or other codes. (fr_FR is the system default) Do you guys know what i do wrong ?
Thanx in advance

Replies are listed 'Best First'.
Re: locale for date in Template Toolkit
by hippo (Monsignor) on Feb 19, 2014 at 15:11 UTC

    Are you sure the de_DE locale is installed on the system? The behaviour you report is what I would expect if it were not installed (ie. the system falls back to the default). At the shell try (LC_TIME=de_DE; date) and see what you get. If it's still French, then the de_DE locale is not accessible to the O/S and will need to be installed.

      That was the point, thanks dude :)
Re: locale for date in Template Toolkit
by sundialsvc4 (Abbot) on Feb 19, 2014 at 16:03 UTC

    Also, and for whatever this may be worth, “this sort of thing” is not what I like to see in the source-code of a template.   Instead, I prefer to see it either as a Perl-supplied input variable (which could of course be a closure ...), or as a custom function (defined in the Perl) that performs the task.   (Which is, in this case, “display this date according to ‘the client language’.”   IMHO, the template should neither contain the exact format-string, nor the logic for deducing what ‘the client language’ is.   Think ahead ... The Marketing Department™ will want to change this!   Endlessly.   Of course, after you have cut ’n pasted it a zillion-and-one times.   You want to be able to change it in one place, then walk away with a smug grin on your face as the Dumbfounded Marketroids praise you as a genius.

    Situations such as this one are where I would advocate that you should strive to “write what you mean,” then provide an implementation for that “meaning” in one, and only one place.   The code that you show in the OP is not “meaning,” but concrete implementation, ripe to be repeated.   This templating system makes all of this very easy to do.

      It was only a template toolkit test (the former coder used to code a lot of things in tt2 since it was annoying our boss...), the actual code is indeed done in Perl with Date::Calc and given to Template Toolkit.

      Also, and for whatever this may be worth, “this sort of thing” is not what I like to see in the source-code of a template...

      Nobody asked you

      Its perfectly reasonable to use a template as a config file to configure other templates without programming any perl

        Kind Monk, why did we build this Monastery, but to solicit the opinions and perhaps the counsel of our Peers? . . .   Peace.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1075464]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2017-05-25 17:21 GMT
Find Nodes?
    Voting Booth?