Internationalizing a ClassicPress plugin: Don’t include HTML markup in localization

ClassicPress PluginsThis 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

Internationalizing a ClassicPress plugin
How does internationalization work?
What is a Text Domain and how is it specified?
Localization functions
Which localization functions to use?
Localizing a string
Don't paramaterize your text domain
Localizing a string containing a parameter
Localizing a string including plurals
Localizing a string including notes for the translator
Don't include HTML markup in localization
Don't localize URLs
Localizing a string including line breaks
Load plugin translations

What should we write about next?

Leave a Reply

Your email address will not be published. Required fields are marked *