Search code examples
phplocalizationpoedit

translating php strings - include formatting characters or not?


I'm using po files to translate my application using the gettext function. I have a lot of strings using formatting characters like spaces, colons, question marks, etc....

What's best practice here?

E.g.:

_('operating database: '). DB_NAME. _(' on ').DB_HOST;
_('Your name:');

or

_('operating database').': '. DB_NAME.' '._('on').' '.DB_HOST;
_('Your name').':';

Should I keep them in translation or is it better to let them hardcoded? What are the pros and cons?


Solution

  • Neither of your examples is good.

    The best practice is to have one string per one self-contained displayed unit of text. If you're showing a message box, for example, then all of its content should be one translatable string, even if it has more than one sentence. A label: one string; a message: one string.

    Never, unless you absolutely cannot avoid it, break a displayed piece of text into multiple strings concatenated in code, as the above examples do. Instead, use string formatting:

    sprintf(_('operating database: %s on %s'), $DB_NAME, $DB_HOST);
    

    The reason is that a) some translations may need to put the arguments in different order and b) it gives the translator some context to work with. For example, "on" alone can be translated quite differently in different sentences, even in different uses in your code, so letting the translator translate just that word would inevitably lead to poor, hard to understand, broken translations.

    The GNU gettext manual has a chapter on this as well.