最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

printf, translation function and date_i18n adds extra characters

programmeradmin0浏览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 ) ) );

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
Add a comment  | 

1 Answer 1

Reset to default 0

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

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

发布评论

评论列表(0)

  1. 暂无评论