0

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?

Karl Adler
  • 15,780
  • 10
  • 70
  • 88
  • On some languages you can't say "DB_NAME on DB_HOST" but for example "DB_HOST'xx DB_NAME" or something else. I prefer translating the whole string even if there will be a duplication (including DB_NAME and DB_HOST as variables). – enenen Jun 04 '13 at 11:49
  • the words in capital letters are php constants, they couldn't be translated – Karl Adler Jun 04 '13 at 12:20
  • Of course. But they could be used in different positions in the translation. You can't translate `CONSTANT1 translated_word CONSTANT2` into `CONSTANT1'translated_word some_other_word CONSTANT2`. So, be sure whether you want only translation or translation and interntionalization. :) – enenen Jun 04 '13 at 12:26

2 Answers2

1

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.

Václav Slavík
  • 6,445
  • 2
  • 28
  • 25
0

If you keep them in translation than all translations will duplicate them. This means all this spaces, colons and etc will be duplicated for each language. What for? I'm standing for translating just the meaning parts of the strings (second variant).

Tsar Ioann
  • 444
  • 4
  • 9