Translating WP with __() and sprintf() Translating WP with __() and sprintf() wordpress wordpress

Translating WP with __() and sprintf()


echo sprintf(__("text %s", 'your__text_domain'), $data);


A.,

In your code:

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

the __() functions checks for a translation of the string '%s' - of which you probably have no translation - and then replaces '%s' with the result of get_color(). So the value for get_color() never passes the translation function.

I am not sure what the right solution is here, maybe just forget about Theme Check in this case.


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