Datumsformatierung mit qTranslate


Vor fast einem Jahr beschrieb ich hier, wie ich es geschafft hatte, in meinem WordPress-Theme zwei unterschiedliche Datumsformate für deutsch und englisch einzurichten. Vor allem Benutzer des Plugins „qTranslate“ verschlug es deshalb des öfteren hierher, denn ich hatte eine – zwar eher verworrene, aber eben funktionierende – Lösung gefunden, das Datum für jede Sprache in einem eigenen Format anzuzeigen, da die vom Autor empfohlene Methode damals bei mir nicht funktioniert hatte. Elf Monate später aber hat sich das Plugin natürlich ganz schön weiterentwickelt und bietet gleich vier Varianten für die Formatierung des Datums an.

Ich will mal zeigen, wie ich für diese Seite hier vorgegangen bin. Zuerst habe ich in den erweiterten qTranslate-Optionen den Haken bei „Benutze emulierte date Funktion“ gesetzt. Das bedeutet, daß man in seinem Template nun wie gewohnt the_date() benutzen kann, qTranslate aber automatisch so Dinge wie Wochentage oder Monatsnamen auf die jeweilige Sprache umstellt. Man braucht also nicht wie früher die qTranslate-Funktion qtrans_formatPostDateTime() zu benutzen! Diese Einstellung empfinde ich von allen als die komfortabelste.

Eine ganz einfache Datumsanzeige ohne Zusatztext (etwa „Sonntag, 19. April 2009“) könnte dann z. B. so aussehen:

the_date('l, j. F Y');

Für diese Seite wollte ich gerne eine Anzeige haben wie „19. Apr 2009“. Das geht wie folgt:

the_date('j. M Y');

Dazwischen habe ich anschließend noch jeweils einen Zeilenumbruch eingefügt:

the_date('j.MY');

Das „r“ muß man in diesem Fall mit einem Backslash schützen, da es von der date()-Funktion bereits belegt ist. Überhaupt muß man bei allem zusätzlichen Text, den man in sein Datum einbauen möchte, aufpassen, ob er Buchstaben enthält, die von der Funktion als Formatparameter erkannt werden. Diese muß man dann alle escapen. Beispiel:

the_date('l, j. F Y \u\m G:i \U\h\r');

In einem solchen Fall muß man sämtliche Buchstaben der Wörter „um“ und „Uhr“ escapen, weil sie sonst alle von der date()-Funktion interpretiert werden und irgendein Murks herauskommt:

Sonntag, 2. November 2008 00000011 7:04 122560586907Sun, 02 Nov 2008 07:04:29 +0100

Wenn man nun gerne auf deutsch die Worte „um“ und „Uhr“ in seinem Datum haben möchte, auf englisch jedoch „at“ und „am“/„pm“, dann kann man dank der (relativ) neuen qTranslate-Quicktags jetzt einfach zwei verschiedene Formate angeben:

the_date('[:de]l, j. F Y \u\m G:i \U\h\r[:en]l, jS F Y \a\t g:i a');

Dieses Beispiel würde auf deutsch so etwas ausgeben wie „Sonntag, 19. April 2009 um 4:18 Uhr“ und auf englisch entsprechend „Sunday, 19th April 2009 at 4:18 am“.

Eine Auflistung aller Parameter für die date()-Funktion findet man auf php.net.

P.S.: Die alte qTranslate-Funktion qtrans_formatCommentDateTime() benötigt man dementsprechend jetzt auch nicht mehr. Stattdessen nimmt man die normale WordPress-Funktion comment_date(), z. B. so:

comment_date('[:de]j. F Y[:en]jS F, Y');

40 Antworten zu “Datumsformatierung mit qTranslate”

  1. Hmm, only in IE 7 and nowhere else? – You have to know that IE 7 is one hell of a mess of a browser. It’s a bunch of crap, absolutely unreliable and totally buggy. So I shouldn’t wonder if it was just some kind of IE 7 bug…

    Do you have an „&“ in the query string? I had a problem with that once. I had to encode the „&“, so the link would look like this: ?a=b&lang=de (instead of ?a=b&lang=de)
    Apart from that, I have no ideas, sorry… :(
    However, once more: If it only happens in IE 7, I would blame it on that crappy piece of software!

  2. I think, your problem with the „Page not found“ thing is because you append the „?lang=…“ directly to http://www.ma-nany.com/ (without index.php). It seems that qTranslate cannot „find“ the page title then. Maybe you can use index.php as start page by default. That could be easily done with a .htaccess file.

  3. Hi Ginchen

    You are doing your language translate using Qtranslate. Am i right?

    if yes then when you have need to add any content in coding then u have to must add that content in all .mo language file.

    then how you are update your .mo files?

    are you using poedit for that or something else?

  4. No, I don’t edit the .mo files. Since I only have two languages (German and English), and I only need very few multilingual content in my template, I simply do it like this (in the template):
    <?php _e(‚[:de]Deutscher Text[:en]English Text‘); ?>
    But in your case, where you have three languages and supposedly need many many more translations inside your template files, I’d rather use the .mo files, too. I use Poedit for this. As soon as you save your .po file, it will automatically create a .mo file, too.

  5. Hi,

    Thanks for feedback.i am updating my .mo files each time when i have to add new content in my template.but your follow code works like magic for me.

    <?php _e(‘[:de]Deutscher Text[:en]English Text‘); ?>

    you are mind blowing in wordpress! how long time r u worked on wordpress?please give me any useful material for wordpress learning.i am just beginner of it.

    so which one i use:follow your upper code or update .mo files each time?

  6. Hehe, I’ve been working with WordPress since February 2008 now (since I started this blog). However, I didn’t use any tutorials – whenever I had a problem, I just asked Google or http://codex.wordpress.org/ which is full of information. ;)

    In your case, I think I would stick with editing the .mo files. The example with _e(‚…‘) is good if you have only 1-2 things to translate, but I think in your case it’s much more, so I’d say the .mo files are the better way.

  7. Hi Ginchen,

    i am using date-time function in my custom script,i have to print current month and year in this format (Jul 2009)..

    so when i am using below code i am not getting same result as u get in your above blog

    $month_name=date(„[:en]M [:fr]M [:de]M Y“,mktime(1,1,1,07,1,2009));

    any idea?

  8. This cannot be done with qTranslate language tags, because qTranslate cannot influence the output of the native PHP function date(), but only of the WordPress function the_date().

    However, qTranslate does set the locale in your environment variables to the currently chosen language, so you can simply use strftime(). Try like this:

    <?php echo strftime(‚%b %Y‘); ?>

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert