I can make a simple translation string like this:

<?php _e('Hello world','text-domain'); ?>

But how to make a translation string if there is a markup in it? For example this:

<?php _e('<span>Hello</span> world','text-domain'); ?>

Is the above correct?


Localizing a string with html markup inside the string can be a very opinionated story as you can see from one of my questions when I started out.

I would not say you are right, neither would I say that you are wrong, but I would say that your method is not prefered.

If you have a look at I18n for WordPress Developers, you'll see that you should use printf() and placeholders when you have variables inside a string, well, the same applies for markup. My emphasis, you should never translate html tags, always use html tags outside of your string.

An example, with what I've just covered, should render a string like this

printf(__( '%s Hello %s world', 'textdomain' ), '<span>', '</span>' );

It works.

 echo '<span>' . esc_html__( 'Hello world', 'textdomain' ) . '</span>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.