Date formatting with qTranslate


Almost a year ago I described here how I had managed to set up two different date formats for German and English in my WordPress theme. That’s why especially users of the plugin “qTranslate” ended up here frequently, because I had found a – rather muddled, but working – solution to show each language in its own format, since the method recommended by the author didn’t work for me then. Eleven months later however, the plugin has developed quite a bit and offers four options for date formatting.

I will show you how I proceeded for this site. First I checked the box “Use emulated date function” in the advanced qTranslate settings. That means that you can now use the_date() in your templates as usual, but qTranslate will automatically convert things like weekdays or month names to the current language. You do not have to use the qTranslate function qtrans_formatPostDateTime() as it used to be! This setting seems the most comfortable to me.

A plain date display without additional text (like “Sunday, 19th April 2009”) could look like this:

the_date('l, jS F Y');

For this site, I wanted it to be displayed like “19. Apr 2009”. That is done as follows:

the_date('j. M Y');

After that, I added a line break between those three:

the_date('j.MY');

The “r” must be protected by a backslash, because it is already reserved by the date() function. You generally have to mind if the additional text in your date format contains letters that are being recognized by the function as formatting parameters. They all have to be escaped. Example:

the_date('l, jS F Y \a\t g:i a');

In such a case you have to escape all the letters of the word “at”, because otherwise they would be interpreted by the date() function and produce a mess:

Sunday, 2nd November 2008 am 30 7:04 am

Now, if you would like to have the words “at” and “am”/“pm” in your English date, but “um” and “Uhr” in the German one, you can simply specify two different formats thanks to the (relatively) new qTranslate quicktags:

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

This example would output something like “Sunday, 19th April 2009 at 4:18 am” in English and “Sonntag, 19. April 2009 um 4:18 Uhr” in German.

A list of all parameters of the date() function can be found on php.net.

P.S.: Accordingly, the old qTranslate function qtrans_formatCommentDateTime() isn’t needed anymore either. You can use the standard WordPress function comment_date() instead, e. g.:

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

40 responses to “Date formatting with qTranslate”

  1. Hi

    I am using Qtranslate Plugin for language translate.i set French language as default.we are using three language in our site mainly (EN,FR,DE)
    when i am selecting English language then qtranslate translate it successfully.but when i am click on any other link on my site it take french language as defualt.

    so do u know How to store selected language in session or cookie in qtranslate?

  2. Did you try to do it like I described above? For example, put something like this into your template:

    the_date('[:de]l, j. F Y, G:i[:en]l, jS F Y, g:i a[:fr]l, jS F Y, G:i');

    Usually that should be working.

    If you want to get the current language and store it somewhere, try get_locale(). This should return ‘de’, ‘en’ or ‘fr’.

  3. Hi,

    When i write “?lang=en” in url of your site,then your site trnaslate in english

    am i right?

    then link for “About us” is like that “http://blog.ginchen.de/en/ueber-mich/”

    How do u do that?How do u maintain “/en/” in whole site?

  4. Ermm, you can set that somewhere in the options under Settings → Languages. I just can’t look it up exactly right now, because I’m running WordPress 2.8, and qTranslate isn’t 2.8 ready yet, so I can’t open the options page…

    After that, you never need to write “?lang=en” anymore, but instead you can call ALL pages with “/en/blahblah/”.

    [Edit] It must be somewhere unter “Advanced Settings” → “URL Modification Mode”.

  5. Hi

    option specified by u is their in admin section.i set that,but when i write url “?lang=de”,then when i am check again in admin section then above option is changed.

    where r u put your .mo files?

    i am putting in “wp-include/languages” folder.

    i am writing like this
    so i have to specified more things to make as same as u…

  6. My .mo files are in wp-content/languages, but that shouldn’t make any difference. According to the WordPress docs, both locations are fine.

    I have no idea why your settings would be reset when you add “?lang=de” to the URL… What WordPress version are you using? qTranslate is not yet ready for WordPress 2.8.

  7. (Sorry, I really have to find out why people can’t post code snippets here…)

    Okay, when you make the links yourself, like <a href=”…”><?php echo __(“About us”); ?></a>, then qTranslate cannot recognize them. It will only work with links that are generated automatically by WordPress, for example with wp_list_pages().

    P.S.: You should now be able to post code snippets here. Just put them between `backticks`. :)

  8. Hi,

    one simple question i have to ask u that

    In admin side when we change language,it store that language in cookie(As i found code in qtranslate_core.php on line no 68).then why wordpress is not store language in cookie on frontside?.

    is it possiable on frontside?

  9. Hi,

    I Store my current language in cookie same as admin side.it’s working fine….

    Now New Problem arise
    when i am writing “lang” querystring in ie 7 then it give me 404 error

    do u have any idea about that?

Leave a Reply

Your email address will not be published. Required fields are marked *