This post is part of the sub-series on Internationalizing a ClassicPress plugin which is part of the Internationalizing a ClassicPress plugin series.
When internationalizing a plugin, you should, as far as possible, avoid including HTML markup in the localization strings.
Annoyance the First: Thou shalt not put unnecessary HTML markup into the translated string. For example, a heading should not be included in the translatable string like this:
$str = esc_html__('<h3>Section 1</h3>', 'plugin-text-domain');
Instead, it should be included outside the translatable string like this:
$str = '<h3>'.esc_html__('Section 1', 'plugin-text-domain').'</h3>';
This isn’t a rule as such, but more of a guideline. It is sometimes necessary to include HTML markup when emphasis is being added to a specific word where the emphasis in another language may be different. However, bear in mind, that even when the emphasis may be on a single word, you still don’t necessarily need to include it in the translatable string:
$str = sprintf(esc_html__('There are %d lights.', 'plugin-text-domain'), '<em>'.$number.'</em>' );
Another approach to get the same result is to include the markup as the text to replace the placeholders; for example:
$str = sprintf(esc_html__('The cave is %svery%s deep.', 'plugin-text-domain'), '<em>', '</em>' );
Translating a ClassicPress plugin
What should we write about next?
If there is a topic which fits the typical ones of this site, which you would like to see me write about, please use the form, below, to submit your idea.