http://www.perlmonks.org?node_id=1075464

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 (Bishop) 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.