xili-language and the time

Setting time, on the fly, in current language of the current post is not very easy in WordPress. After some research in core’s source and in php functions, several pathways are available here :

Without changing or adding specific template tags, the_time(), the_modified_time() and other functions are hookable by xili-language.

The « hooking » functions are placed inside the xilidev-libraries (see this post).

function xiliml_comment_date($comment_time,$d = '') {
  /* params no used because php format */
  /* the first param is to be as msgid in .po example: %B %d, %Y - %H:%M or %B %d, %Y at %H:%M   */	
  return the_xili_local_time('%B %d, %Y', strtotime(get_comment_time ('m/d/Y H:i'))); 
  /* impossible to use get_comment_date as it is itself filtered*/
}
add_filter('get_comment_date','xiliml_comment_date',10,2);
 
 
function xiliml_date($thetime,$theformat) {
  /* param no used because php format */	
  return the_xili_local_time('%B %d, %Y - %H:%M',strtotime(get_the_time('m/d/Y H:i')));
}
add_filter('the_time','xiliml_date',10,2);
 
function xiliml_modified_date($thetime,$theformat) {
  /* param no used because php format */	
  return the_xili_local_time('%B %d, %Y - %H:%M',strtotime(get_the_modified_time('m/d/Y H:i')));
}
add_filter('the_modified_time','xiliml_modified_date',10,2);

Basically WordPress uses php date() format – http://fr2.php.net/manual/en/function.date.php – and and internal static translation tools.

In the hooking function and in date format translation, the format depending of setlocale is used. See – http://fr2.php.net/manual/en/function.strftime.php

With this way and if the server is well configured, the live translation of date / time works well.

For the .po file, the msgid format is ‘%B %d, %Y – %H:%M‘ and his translation in french can be ‘%d %B %Y – %Hh%M

FAQ

* Why are these hooks inside the xilidev library and not inside the plugin itself ?

Because the webmaster can modify it and the modifications will be not erased when the plugin is updated. (Don’t forget to check (and activate) this library in admin plugin’s UI)

xilidev or gold functions

xilidev or gold functions


* Why the parameters of the_time() are not used ?

Because they need big translation and lot of lines. So it was better to bypass it and insert more flexible properties.

* In the_time() how to suppress the time ?

Just modify the string ‘%B %d, %Y – %H:%M’ in ‘%B %d, %Y’ .

* What do the function the_xili_local_time() ?

This function inside the plugin translates a generic numeric date according current language of the post or the theme’s live language.

* On my website, I use different patterns inside the_time() depending category or single or… What do you recommend ?
Inside the filter function, add php ‘switch case‘ lines analyzing the $theformat passed (php format) and deliver – according these – the first param of the_xili_local_time() a setlocale format with %. It is indeed easy to choose a result with or without hour:min. After you have defined the list of format, don’t forget to translate them in .po (and .mo) of each language.

 
 

This posts belong to the series: xili-language for experts
  1. xili-language plugin : release 2.1
  2. Q&A about xili-language plugin, release 1.1
  3. xili-language : hooks, filters, class and so on...
  4. From a theme with localization to a multilingual theme with xili-language : technical and general notes.
  5. xili-language - 1.4.1 - new libraries
  6. Xili-language toolbox in one table
  7. xili-language and the time
  8. xili-language : Affichage des dates et heures
 
 

Ce contenu a été publié dans Experts corner, xili-language, avec comme mot(s)-clé(s) , , , , . Vous pouvez le mettre en favoris avec ce permalien.

Laisser un commentaire