__(): What if I have to pass in a variable?

In the docs for the translate function __( $text, $domain ), it states that you must put the string directly in place of $text, that you cannot do something clever such as __( $my_text, 'text-domain' );.

However, I am writing a method which takes in a string and needs to pass it to __() somehow. Is there a way, e.g. via printf or sprintf that I can work around this?

Something like…

function print_description( $text ) {
    echo '<p>' . __( printf( '%s', $text ), 'text-domain' ) . '</p>';
}

6 s
6

You can do it with printf().

E.g.

printf( __( 'We deleted %d spam messages.', 'my-text-domain' ), $count );

Leave a Comment