Achtung: Dieser Post ist ziemlich alt. Insbesondere die Vorschläge unter Punkt 2 und 3 sind nicht mehr zeitgemäß. Neuere Hinweise zur Datumsformatierung mit qTranslate gibt es hier.
Nach dem Update auf WordPress 2.5 hatte ich – wie offensichtlich viele andere auch – das Problem, daß Datum und Uhrzeit der Posts nicht mehr lokalisiert wurden. Wochentags- und Monatsnamen wurden nicht mehr übersetzt, und auch über die Anzahl der Kommentare wurde man nur noch mit einem englischen „x comments“ informiert. Die vielfach empfohlene setlocale()
-Funktion brachte keine Besserung – wieso sollte sie auch? Schließlich hatte ich sowohl WordPress als auch PHP bereits in den Konfigurationsdateien auf deutsch eingestellt.
Wieso, weshalb, warum, kann ich leider auch nicht sagen, aber ich habe nach einigem Ausprobieren Lösungen gefunden, die zumindest für mich funktionieren.
[:en]Note: This post is quite old. Especially the suggestions under items 2 and 3 are no longer appropriate. More recent tips for date formatting with qTranslate are available here.
After updating to WordPress 2.5, I have – like many others, obviously – experienced the problem that post dates and times were not being localised anymore. Weekday and months names were not translated, and the number of comments was only indicated by an English „x comments“. The frequently recommended setlocale()
function did not bring any improvement – and why should it? After all I had already set WordPress as well as PHP to German in the configuration files.
I cannot tell the whys and wherefores, but after some trial and error I found solutions that work at least for me.
[:][:de]1.) Lokalisierter Kommentar-Link
Mein Template verwendete die Funktion comments_popup_link()
, um den Link zu den Kommentaren zu erzeugen. Früher wurde dieser Link automatisch ins Deutsche übersetzt, also z. B. „Keine Kommentare“ anstatt „No comments“. Seit Version 2.5 jedoch klappt das nicht mehr. Also habe ich in meinem Template die Zeile
<?php comments_popup_link() ?>
ersetzt durch
<a href="<?php comments_link(); ?>"><?php comments_number(); ?>
Im Gegesatz zu comments_popup_link()
zeigt die Funktion comments_number()
die Anzahl der Kommentare – und eben auch die dazugehörigen Wörter wie „keine“, „Kommentar“ und „Kommentare“ – brav auf deutsch an. Da sie aber den Link zu den Kommentaren nicht selbst erzeugt, muß man diesen eben noch mit comments_link()
drum herum bauen.
2.) Lokalisierte Datumsanzeige bei Posts
Anstelle von the_date()
bzw. _e(the_date('l, d. F Y, H:i','','',FALSE))
setzte ich in meinem Template folgendes ein:
_e( htmlentities( strftime('%A, %e. %B %Y um %R Uhr', strtotime( the_date('Y/m/d H:i', '', '', FALSE) ) ) ) )
(Natürlich kann man das einfach hintereinander weg schreiben – ich habe es jetzt nur der Lesbarkeit halber so gemacht.)
Was macht diese Codezeile?
Grob gesagt sorgt sie dafür, daß das Datum nicht direkt mittels the_date()
ausgegeben wird, sondern zur weiteren Formatierung an strftime()
weitergegeben wird. Im Gegensatz zu the_date()
funktioniert nämlich bei strftime()
die Lokalisierung einwandfrei.
Wer es genau wissen will:
- Zuerst wird mithilfe von
the_date()
das Postdatum in die Form „Jahr/Monat/Tag Stunde:Minute“ gebracht, also z. B.2008/05/11 10:30
. - Dieses Datum wird dann an
strtotime()
weitergereicht, welches daraus einen Unix-Timestamp macht, z. B.1210494600
. - Und dieser Timestamp wird schließlich an die Funktion
strftime()
übergeben, mit der man das Datum entsprechend seiner Ländereinstellung beliebig formatieren kann. Juhu! - Drumherum habe ich noch ein
htmlentities()
gepackt, damit man anstelle von „März“ nicht ein „M?rz“ zu lesen bekommt.
Die verschiedenen Formatierungsparameter von strftime()
findet man bei php.net. Das oben gezeigte Beispiel '%A, %e. %B %Y um %R Uhr'
würde folgendes ausgeben:
Sonntag, 11. Mai 2008 um 10:30 Uhr
Mehrere lokalisierte Datumsformate mit qTranslate
Wer wie ich für die Mehrsprachigkeit seiner Seite das Plugin qTranslate verwendet, der kann mithilfe von Language-Tags ganz einfach verschiedene Datumsformate für die verschiedenen Sprachen angeben. So möchte ich z. B., dass das deutsche Datum im Format „Sonntag, 11. Mai 2008 um 10:30 Uhr“ ausgegeben wird, das englische dagegen als „Sunday, 11 May 2008 at 10:30 am“. Man schreibt also das Ganze einfach zweimal hin mit den entsprechenden Language-Tags und Datumsformaten. Das sieht dann folgendermaßen aus:
_e( '<!--:de-->'.htmlentities( strftime('%A, %e. %B %Y um %R Uhr', strtotime( the_date('Y/m/d H:i', '', '', FALSE) ) ) ) .'<!--:--><!--:en-->'.htmlentities( strftime('%A, %e %B %Y at %I:%M %P', strtotime( the_date('Y/m/d H:i', '', '', FALSE) ) ) ).'<!--:-->' )
Zwar stellt qTranslate eine eigene Funktionen zur Formatierung des Postdatums bereit, die das Ganze etwas abkürzen würde:
_e( '<!--:de-->'. qtrans_formatPostDateTime('%A, %e. %B %Y um %R Uhr') .'<!--:--><!--:en-->'. qtrans_formatPostDateTime('%A, %e %B %Y at %I:%M %P') .'<!--:-->' )
Doch leider hat diese bei mir nicht funktioniert – das Ergebnis sah immer so oder so ähnlich aus: %PM, %Europe/Berlin. %487 %2008 u05 %R 121050249512
.
Inzwischen habe ich herausgefunden, daß die qTranslate-Funktionen ebenfalls nur mit einem date()
-Format umgehen kann, obwohl der Autor hier ausdrücklich geschrieben hat, den Funktionen sei ein strftime()
-Format zu übergeben. Warum das so ist, weiß ich nicht – evtl. liegt es daran, daß ich WordPress 2.5 verwende, welches bisher noch nicht vollständig von qTranslate unterstützt wird. Jedenfalls erklärt das die merkwürdige Ausgabe, denn A
steht bei date()
für ein großgeschriebenes „AM“ bzw. „PM“, e
steht für die Zeitzone etc.
Beim Datum der Kommentare hingegen sah ich mich gezwungen, die von qTranslate beigesteuerte Datumsfunktion zu verwenden, da ich von WordPress selbst aus bisher keine finden konnte, die das Datum einfach nur zurückgibt, anstatt es sofort anzuzeigen. Da ich ja nun herausgefunden hatte, wie die qTranslate-Funktionen in Wirklichkeit ticken, habe ich das folgendermaßen gelöst:
_e( '<!--:de-->'.htmlentities( strftime('%A, %e. %B %Y um %R Uhr', strtotime( qtrans_formatCommentDateTime('Y/m/d H:i') ) ) ).'<!--:--><!--:en-->'.htmlentities( strftime('%A, %e %B %Y at %I:%M %P', strtotime( qtrans_formatCommentDateTime('Y/m/d H:i') ) ) ).'<!--:-->' )[:en]
1.) Localised comments link
My template used the function comments_popup_link()
to generate the link to the comments. In the past, this link was automatically translated into German, e. g. „Keine Kommentare“ instead of „No comments“. As of version 2.5, however, that doesn’t work anymore. So in my template I replaced the line
<?php comments_popup_link() ?>
by
<a href="<?php comments_link(); ?>"><?php comments_number(); ?>
In contrast to comments_popup_link()
, the function comments_number()
dutifully shows the number of comments – and the corresponding words like „keine“, „Kommentar“ and „Kommentare“ – in German. It does not generate the link to the comments though, so you have to enclose it in a link manually using comments_link()
.
2.) Localised date display for posts
Instead of the_date()
or _e(the_date('l, d F Y, H:i','','',FALSE))
, I inserted the following into my template:
_e( htmlentities( strftime('%A, %e %B %Y at %I:%M %p', strtotime( the_date('Y/m/d H:i', '', '', FALSE) ) ) ) )
(Of course, you can simply put this on a single line – i just did it like that for the sake of legibility.)
What does this line of code do?
Roughly spoken, it causes the date not to be output directly by the_date()
, but to be forwarded to strftime()
for further formatting. For in contrast to the_date()
, localisation works perfectly well when using strftime()
.
For those who want to know exactly:
- First the post date is being converted into the format „year/month/day hour:minute“ using
the_date()
, e. g.2008/05/11 10:30
. - This date is then being forwarded to
strtotime()
, which converts it into a Unix timestamp, e. g.1210494600
. - And that timestamp is finally delivered to
strftime()
, with which you can format the date according to your regional settings as you like. Woohoo! - Around it all I put a
htmlentities()
to prevent you from stumbling across a „M?rz“ instead of „März“.
The different formatting parameters for strftime()
can be found on php.net. The above example '%A, %e %B %Y at %I:%M %p'
would output the following:
Sunday, 11 May 2008 at 10:30 am
Different localised date formats with qTranslate
If you run a multilingual site using the plugin qTranslate, you can easily specify different date formats for the different languages by means of language tags. For example, I want the German date to be displayed in the format „Sonntag, 11. Mai 2008 um 10:30 Uhr“, while the English one should read „Sunday, 11 May 2008 at 10:30 am“. You simply have to write the whole thing twice with the corresponding language tags and date formats. That will look as follows:
_e( '<!--:de-->'.htmlentities( strftime('%A, %e. %B %Y um %R Uhr', strtotime( the_date('Y/m/d H:i', '', '', FALSE) ) ) ) .'<!--:--><!--:en-->'.htmlentities( strftime('%A, %e %B %Y at %I:%M %P', strtotime( the_date('Y/m/d H:i', '', '', FALSE) ) ) ).'<!--:-->' )
qTranslate does provide its own function for post date formatting, which would abbreviate the whole thing:
_e( '<!--:de-->'. qtrans_formatPostDateTime('%A, %e. %B %Y um %R Uhr') .'<!--:--><!--:en-->'. qtrans_formatPostDateTime('%A, %e %B %Y at %I:%M %P') .'<!--:-->' )
But unfortunately it didn’t work for me – the result always looked like this:
%PM, %Europe/Berlin. %487 %2008 u05 %R 121050249512
.
Meanwhile I found out that the qTranslate functions can also only deal with a date()
format, although the author stated explicitly that the functions would expect a strftime()
format. I don’t know why that is – maybe because I am using WordPress 2.5, which is not fully supported by qTranslate so far. Anyway, it does explain the strange output, because when using date()
, A
stands for a capitalised „AM“ or „PM“, e
stands for the time zone etc.
As for the comments dates anyhow, I was forced to use the date function provided by qTranslate, because I couldn’t find a native WordPress function, which simply returns the date instead of immediately displaying it. Since I had figured out now how the qTranslate functions really work, I solved this as follows:
_e( '<!--:de-->'.htmlentities( strftime('%A, %e. %B %Y um %R Uhr', strtotime( qtrans_formatCommentDateTime('Y/m/d H:i') ) ) ).'<!--:--><!--:en-->'.htmlentities( strftime('%A, %e %B %Y at %I:%M %P', strtotime( qtrans_formatCommentDateTime('Y/m/d H:i') ) ) ).'<!--:-->' )[:]
5 Antworten zu “[:de]Datumsformatierung in WordPress[:en]Date formatting in WordPress[:]”
Hallo Ginchen,
wie hast du denn den Blogtitel durch qtranslate übersetzt???
Mein header sieht so aus:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="" type="text/css" media="screen" />
Anmerkung: Also, erst mal habe ich den Blogtitel im Backend unter „Einstellungen“ → „Blogtitel“ mit Language-Tags eingetragen. Dort steht also:
Die Browser-Titelleiste im Format „Ginchens Blog » Name des Posts“ entsteht wie folgt:
Und die Überschrift ganz oben auf der Seite, die gleichzeitig ein Link zur Startseite ist, geht so:
Dadurch wird je nach Sprache „/en/“ ans Ende des Links angehängt oder eben nur ein Slash.
Hallo Ginchen,
du bist echt genial, hab es umgesetzt wenn du mal kucken willst:
http://www.divadlokufr.net/wordpress
Lieben Dank
Detlef
[…] fast einem Jahr beschrieb ich hier, wie ich es geschafft hatte, in meinem WordPress-Theme zwei unterschiedliche Datumsformate für […]
DANKE DANKE DANKE!
Hi, thanks very much for this, it helped me finish manipulating the post date in WordPress into Spanish.
Here’s the code that ended up working for me, in case it’s useful for anyone else:
„
This gives you the following format: “Jueves, 9 de junio, 2011″
Ginchen: Damn, my backtick code insertion plugin was disabled … Sorry. Maybe you can post your code again, it should be working now!