0

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 ) ) );
  • 1
    What’s the result of __() function for this string? Could you var_dump it? – Krzysiek Dróżdż May 18 at 7:54
  • Good call, I’ll check on Monday! – MediaFormat May 18 at 12:28
  • @KrzysiekDróżdż thanks, I was using the wrong formatting function! – MediaFormat May 21 at 14:05
0

Because the translate function __() worked fine, I suspected printf() was the culprit.

Turns out sprintf() is what I needed! printf() arguments work differently

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.