PHPCS: Strings should have translatable content

I am validating plugin to make it compatible with WPCS. a function which contains gettext placeholder returning error when i run phpcs command. Source code and screenshot of error is attached for reference. Maybe i am missing something or doing it in wrong way?

/**
 * Add Error.
 *
 * @package Mypackage
 * @since 0.1.0
 *
 * @param string $code Error Code.
 * @param string $message Error Message.
 * @return object WP_Error Returns Error Object.
 */
function mypackage_add_error( $code, $message ) {

    /* translators: %s: Error Message */
    return new WP_Error( $code, sprintf( esc_html__( '%s', 'mypackage' ), $message ) );
}

Screenshot of Error.
enter image description here

2 Answers
2

There are several things wrong with your code

  1. The is no need for translation there. The message should have been translated before it is being passed to that function. At that function there is actually no context at all to create any different translation than %s

  2. Escaping should happen at the output. You should escape the result of the sprintf and the translation.

And keep in mind that WPCS is a tool to help you, not the bible. At least at its current stage it lacks too many feature to just blindly follow it all the time. Some of the errors it emits right now are due to bad parsing of modern PHP or lacking features, so if you are 100% sure that in your specific case %s should be translatable go with it.

Leave a Comment