Php – Translating longer texts (view and email templates) with gettext

gettextinternationalizationPHPtemplates

I'm developing a multilingual PHP web application, and I've got long(-ish) texts that I need to translate with gettext. These are email templates (usually short, but still several lines) and parts of view templates (longer descriptive blocks of text). These texts would include some simple HTML (things like bold/italic for emphasis, probably a link here or there). The templates are PHP scripts whose output is captured.

The problem is that gettext seems very clumsy for handling longer texts. Longer texts would generally have more changes over time than short texts — I can either change the msgid and make sure to update it in all translations (could be lots of work and very error-prone when the msgid is long), or I can keep the msgid unchanged and modify only the translations (which would leave misleading outdated texts in the templates). Also, I've seen advice against including HTML in gettext strings, but avoiding it would break a single natural piece of text into lots of chunks, which will be an even bigger nightmare to translate and reassemble, and I've also seen advice against unnecessary splitting of gettext strings into separate msgids.

The other approach I see is to ignore gettext altogether for these longer texts, and to separate those blocks in external subtemplates for each locale, and just include the one for the current locale. The disadvantage is that I'm separating the translation effort between gettext .po files and separate templates located in a completely different location.

Since this application will be used as a starting point for other applications in the future, I'm trying to come up with the best approach for the long term. I need some advice for best practices in such scenarios. How have you implemented similar cases? What turned out to work and what turned out a bad idea?

Best Answer

I stumbled upon a similar problem not long ago (see https://stackoverflow.com/questions/8288050/can-i-automatically-update-msgids-in-gettexts-po-files-for-trivial-text-change ).

Basically, there are two options:

  1. Do it the way gettext is meant to be used: Use the original texts as msgid, HTML and all. Then every change to the original text will invalidate all translations. msgmerge's fuzzy matching will, however, usually still match the right original text to the old translation, so usually the old translation will be available to the translator.
  2. Insted of using the original text as msgid, use some synthetic identifier (like EMAIL_TEMPLATE_NEWSLETTER_START). That way, the IDs never change. The original (probably English) texts will then simply be another translation to gettext. Main disadvantages: 1) You must track separately which translations are up to date; 2) Some gettext tools may not handle this well (translators may need special tools).
Related Topic