5

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?

3
  • it depends of the sentence. to choose you have to think if it's clear for the translator. in the 2nd case, he will have to translate " have an" and "This " then in this case it would be clearer to translate the whole sentence in one time. when a sentence is to complicated, you can also use sprintf to cut the sentence – mmm Aug 2 '15 at 19:11
  • @mmm So you mean translating the whole paragraph that had innerHTML tags works well? Could you provide me a link to an article that proves your saying? – 5ervant Aug 4 '15 at 7:43
  • 1
    A good information source is the WordPress codex article »I18n for WordPress Developers«. – Nicolai Aug 5 '15 at 12:43
3

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>' );
2
  • 1
    @ialocin Thanks for the edit, noddy badge for me for forgetting those brackets :-) – Pieter Goosen Aug 5 '15 at 12:44
  • 1
    It happens and this is after all a community effort :) – Nicolai Aug 5 '15 at 12:47
3

The second option will not only (as @mmm already said) confuse translators, but also the order of words in a sentence can be significantly different from one language to another.

With the first option a translator can adjust and translate properly, which is not possible with the second one.

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.