Use __($str) and _e($str) to translate strings with HTML

I’m currently developing a non-trivial theme right now, most of the time I’m not using a __($str) translate function when there’s an HTML tag therein the text. Is it good to always use the translate functions such __($str) and _e($str) whenever there’s a text that’ll be going to output? Any downside with it?

Can I still use it even to a very long text that have mixed with an HTML tag such like this?

__("This <em>text</em> have an <b>HTML</b> tag. Another sentence...")

Or it is good to use like this? (*Looks messy..)

__('This ').'<em>'.__('text').'</em>'.' have an '.'<b>'.__('HTML').'</b>'__(' tag.')

How to properly use such kind of a function?

2 s
2

The sweet and short of it all is, never try to translate HTML tags. Use placeholders and either printf or sprintf to translate strings with HTML tags

Something like this will do

printf()

printf( __( 'This %s text %s have an %s HTML %s tag. Another sentence...' ), '<em>', '</em>', '<b>', '</b>' );

sprintf()

sprintf( __( 'This %s text %s have an %s HTML %s tag. Another sentence...' ), '<em>', '</em>', '<b>', '</b>' );

Leave a Comment