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”
Prima, eine Seite mit Tipps für qTranslate. Gerade hab ich das richtige Datum selbst mit Trial&Error im Template und qTranslate für die neue Website hingebügelt. Mir kommen die Parameter schon zu den Ohren heraus….!
Hi
Qtranslate is working fine in my local system but in live site it is not working fine
i had upload all .mo files from my local to live
Qtranslate plugin is also active
http://www.ma-nany.com/baby-sitter.php?reg=baby&action=register〈=en
this is url of live site
only “Username” is translateble
Please help it’s urgent
I don’t see any problem? I click on English, the page is in English, click on French, the page is in French, click German, the page is German.
So you mean the stuff like “Welcome to Baby Sitter Registration” or “Enter E-mail and Username”? That is not inside the default .mo files, of course. You can either enter that in your template manually like
_e('[:en]English text[:de]German text[:fr]French text');
or add the sentences to your .mo files.i’ve got the same problems and for a reason local it is still working, but not on my webserver. so i had to find another solution to get my dates multilingual.
here is what worked for me and what is used by wordpress itself:
You can also realize different formats, by checking what get_locale() gives you back.
hi qtranslate plugin does not work in my theme in local wamp server on my system and how we can add hindi language in this plugin
pleaase help me .
thisplugin change backend content in different language but pronf end it is not work properly.
Well, you have to create a new language in your WordPress backend under “Languages”, for example:
Also, you have to download a Hindi .mo file and put it inside your
wp-content/languages
folder.You can get one here.
Hi, thanks for the tips! I’ve got my site running WordPress 3.2.1 and qtranslate 2.5.24. The dates show fine in both English and German using just:
. June will display as Juni in the German version, and October as Oktober. Perfect. BUT… come to March, and it shows as M?rch… as if it cannot deal with characters with an umlaut, like ä.
Any ideas? Thanks in advance.
I should add that characters with umlaut’s are working perfectly fine in every other area of the website.
Hmmm, it’s a problem with the encoding. It shouldn’t be happening, if everything (server, database, WordPress) is set to UTF-8. So maybe one of them isn’t?
Anyway, you could try to wrap it in
utf8_encode()
, like this:<?php utf8_encode(the_time('j F Y')) ?>
Maybe that will help. (The corresponding command for decoding is utf8_decode(), but I think you need ENcode.) However, no guarantees that it will work. ;)
Dominik’s solution worked for me! Thanks dude!