Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translating WP with __() and sprintf()

I'm trying to translate WP theme. I have this code:

$translation = __( get_color(), 'textdomain' );

It works, I get color dynamically from get_color() function, and it translates well. But when I use "Theme Check" plugin I get error for this code.

I need to use this instead:

$translation = sprintf( __( '%s', 'textdomain' ), get_color() );

But in that case my placeholder %s doesn't translates, and I get original color name (not translated).

What I'm doing wrong? Thank you.

like image 223
D. A. Avatar asked Apr 30 '16 15:04

D. A.


2 Answers

echo sprintf(__("text %s", 'your__text_domain'), $data);
like image 109
Mahdi Bashirpour Avatar answered Oct 03 '22 07:10

Mahdi Bashirpour


I'm surprised no one mentioned the "translators" comment, that tells the translator what each variable in the sprintf is. Examples:

sprintf(
    /* translators: %s: Name of a city */
    __( 'Your city is %s.', 'my-plugin' ),
    $city
);

sprintf(
     /* translators: 1: Name of a city 2: ZIP code */
    __( 'Your city is %1$s, and your zip code is %2$s.', 'my-plugin' ),
    $city,
    $zipcode
);

See: https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#variables

like image 36
Lucas Bustamante Avatar answered Oct 03 '22 08:10

Lucas Bustamante