1

I have a WP homepage with 2 languages and use the Polylang plugin to do that; so far so good.

Now I have an US theme installed which provides the blog articles only with US date format (like "28 September, 2017"). Unfortunately, Polylang only translates the month names correctly but does not change the date format as such.

The theme's blog php file uses:

get_the_time('d F, Y')

If I change that format manually into the local date format it will apply it for the English version too which of course does not make sense. Instead, I want to hook into the functions.php file and get Polylang to change the date format generally according the language of the page.

I found this snippet for WPML (another WP multilanguage plugin) which should do the trick:

add_filter( 'option_date_format', function( $format ) {
do_action( 'wpml_register_single_string', 'Date formats', 'Date format', $format );
return apply_filters( 'wpml_translate_single_string', $format, 'Date Formats', 'Date format' );
} );

add_filter( 'option_time_format', function( $format ) {
do_action( 'wpml_register_single_string', 'Date formats', 'Time format', $format );
return apply_filters( 'wpml_translate_single_string', $format, 'Date Formats', 'Time format' );
} );

Unfortunately, I do not manage to do it for Polylang. I know the function to use is:

pll_register_string( $name, $string, $group, $multiline );

See: https://polylang.pro/doc/function-reference/#pll_register_string

Does somebody know how to do this?

Super Geroy
  • 123
  • 1
  • 10

1 Answers1

4

In WordPress default settings you can set your own format for displaying date Settings -> General. Then you can translate this format in Polylang string translations functionality Language -> String translations.

If your theme use custom date format, which is bad practise, you should change it or contact author to do it.

Instead of get_the_date('d F, Y') there should be get_the_date() which will return date in default format from settings.

If for some reason you do not use date format from WordPress settings you should register date format with pll_register_string and translate it in Language -> String translations.

/**
 * Register polylang strings to translate
 * 
 * @return void
 */
function se_50718114_register_translatable_strings() {

    $date_format = 'd F, Y';

    pll_register_string( $date_format, $date_format );
}

add_action( 'init', 'se_50718114_register_translatable_strings' );

/**
 * Somewhere in your template where you display date
 */
echo get_the_date( pll__( 'd F, Y' ) );

Solution which you provide for WPML will not work in this case because it translate date format from settings and your theme do not use it.

kierzniak
  • 606
  • 5
  • 15
  • Where should I insert this? And what does the 'echo' line do? – Super Geroy Jun 06 '18 at 10:47
  • I updated my answer. The best option for me is to change get_the_time('d F, Y') to get_the_time() everywhere and translate date using string translations page. – kierzniak Jun 06 '18 at 10:55
  • Of course, I already contacted the theme authors and they wrote they cannot do this within their support. I also consider this to be bad coding but my php skills are limited. – Super Geroy Jun 06 '18 at 10:55
  • Are you saying that I can globally substitute get_the_time('d F, Y') by get_the_time() and it works? – Super Geroy Jun 06 '18 at 10:57
  • This should work. Change date format to `d F, Y` in your WordPress settings. – kierzniak Jun 06 '18 at 10:58
  • I tried it on a local installation, set system language to English and date format to `d F, Y`and your suggestion showed the time only. I then subsituted with `get_the_date` and it seems to work with that. Why would the theme authors not code it like that in the first place? – Super Geroy Jun 06 '18 at 11:57
  • Glad I could help! I do not know by I assume laziness or lack of knowledge. I will change my answer to replace `get_the_time` with `get_the_date`. Can you also do it with your question? – kierzniak Jun 06 '18 at 12:00