How to allow single quote with esc_html__() without sprintf()


Because of security reasons we are of course required to use esc_html__() for WP development. This is annoying because if you’d want to pass a single quote into your strings, you’d have to use sprintf() to make it work. Otherwise you just get ' printed out, instead of a '.

Without using sprintf():

esc_html__( 'Wasn\'t your favorite color red?', 'domain' ); // Output: Wasn't your favorite color red? esc_html__( 'Provided reason isn\'t selected', 'domain' ); // Output: Provided reason isn't selected 

With the use of sprintf() I can get single quotes to work (of course).

sprintf( esc_html__( 'Wasn%st your favorite color red?', 'domain' ), '\'' ); // Output: Wasn't your favorite color red? sprintf( esc_html__( 'Provided reason isn%st selected', 'domain' ), '\'' ); // Output: Provided reason isn't selected 

I’d like to know if there is a different way to achieve the same output. I am afraid there is none, but I thought why not give it a shot, who knows.