So I am trying to make these strings translatable and in proper PHP/WP format. This works:
'Du ' . date_i18n( 'j M', strtotime( $date_debut->format('j M Y') ) ) . ' au ' . date_i18n( get_option( 'date_format' ), strtotime( $date_fin->format('j M Y') ) );
The equivalent using printf
should be something like this:
printf(
__( 'Du %1$s au %2$s', 'my-plugin' ),
date_i18n( 'j M', strtotime( $date_debut->format('j M Y') ) ),
date_i18n( get_option( 'date_format' ), strtotime( $date_fin->format('j M Y') ) )
);
But for some reason the Year comes out as either 201914
or 201920
.
Why are there extra characters on the dates?
EDIT
The results are fine and as expected when using without the printf()
function
__('Du ' . date_i18n( 'j M', strtotime( $date_de_debut ) ) . ' au ' . date_i18n( get_option( 'date_format' ), strtotime( $date_de_fin ) ) );
So I am trying to make these strings translatable and in proper PHP/WP format. This works:
'Du ' . date_i18n( 'j M', strtotime( $date_debut->format('j M Y') ) ) . ' au ' . date_i18n( get_option( 'date_format' ), strtotime( $date_fin->format('j M Y') ) );
The equivalent using printf
should be something like this:
printf(
__( 'Du %1$s au %2$s', 'my-plugin' ),
date_i18n( 'j M', strtotime( $date_debut->format('j M Y') ) ),
date_i18n( get_option( 'date_format' ), strtotime( $date_fin->format('j M Y') ) )
);
But for some reason the Year comes out as either 201914
or 201920
.
Why are there extra characters on the dates?
EDIT
The results are fine and as expected when using without the printf()
function
__('Du ' . date_i18n( 'j M', strtotime( $date_de_debut ) ) . ' au ' . date_i18n( get_option( 'date_format' ), strtotime( $date_de_fin ) ) );
Share
Improve this question
edited May 21, 2019 at 14:25
MediaFormat
asked May 17, 2019 at 19:46
MediaFormatMediaFormat
2831 silver badge11 bronze badges
3
- 1 What’s the result of __() function for this string? Could you var_dump it? – Krzysiek Dróżdż Commented May 18, 2019 at 7:54
- Good call, I’ll check on Monday! – MediaFormat Commented May 18, 2019 at 12:28
- @KrzysiekDróżdż thanks, I was using the wrong formatting function! – MediaFormat Commented May 21, 2019 at 14:05
1 Answer
Reset to default 0Because the translate function __()
worked fine, I suspected printf()
was the culprit.
Turns out sprintf()
is what I needed! printf()
arguments work differently