Textpattern quick tip: Dates in local language

Thursday, February 23, 2006

Currently building a danish website with Textpattern, I ran into an issue with the date format. Even though I set the language to danish, <txp:posted /> still uses english for weekdays and months. By googling around a bit, I’ve found this FAQ: How do I display time and date formats in my local language, which suggests that instead of <txp:posted />, I’d use the following:

global $locale;
setlocale(LC_ALL, 'nl_NL.UTF-8');
echo posted(array('format' => '%A %B %e %Y, %H:%M:%S'));
setlocale(LC_ALL, $locale);

but doing that in all the article forms felt like a somewhat bad idea, at least for readability. Instead, I stripped out most of it and added the following line to the start of the page:

<txp:php>setlocale(LC_ALL, 'da_DK.UTF-8');</txp:php>

Of course, da_DK should be replaced with whatever language you wish to use. Another advantage, besides this being shorter, is that it lets you keep using the date format settings in TXP. And thus lets you keep using <txp:posted /> in all its glory. Still, knowing a bit of strftime conversion specifiers won’t hurt you, it might come in handy sometime (impress your mom! set a date for a date! whatever!)

Header and footer include with <txp:output_form />

Another tip that really helps when building sites with many page templates (I guess most people using TXP does this already, so this is mostly for beginners) is to make “include files” for the header and footer, by using TXP forms. I use www- as a prefix for site-related snippets so that they come last in the form listing, and thus every TXP page starts with:

<txp:output_form form="www-header" />

and ends with:

<txp:output_form form="www-footer" />

All in all, Textpattern is a lovely application. If it would just let me use my text editor instead of the browser for editing templates, we’d be best pals.

More info:

If you have any additions to good references, or know for a fact that what I’m doing with that setlocale() call in the header is plain stupid, please let me know. Thanks!

Comments are currently closed.

2 comments. Comments are closed for this entry.

I’m just setting up my first textpattern site and your tips on using custom forms for headers and footers was great – I definitely need one for my navigation sidebar!

#1 Lorna 18 Mar 06

I’d love to see what you’ve done with the SwfObj plugin. Showing a poetsr image by default sounds like a fantastic idea. Please shoot me an email if you get a chance, I’m definitely interested in folding your hacks into the next plugin release.Thanks.

#2 Bobby 11 Nov 15