R – Where to translate message strings – in the view or in the model

gettextinternationalizationlanguage-agnosticxgettext

We have a multilingual (PHP) application and use gettext for i18n. There are a few classes in the backend/model that return messages or message formats for printf().

We use xgettext to extract the strings that we want to translate.

We apply the gettext function T_() in the frontend/view – this seems to be where it belongs. So far we kept the backend clean from T_() calls, this way we can also unit-test messages.

So in the frontend we have something like

echo T_($mymodel->getMessage());

or

printf(T_($mymodel->getMessageFormat()), $mymodel->getValue());

This makes it impossible to apply xgettext to extract the strings, unless we put some dummy T_("my message %s to translate") call in the MyModel class.

So this leads to the more general question:

Do you apply translation in the backend classes, resp. where do you apply translation and how do you keep track of the strings which you have to translate?

(I am aware of Question: poedit workaround for dynamic gettext.)

Best Answer

My backend classes usually output english strings with parameters left out. Example

["Good job %s you have %i points", "Paul", 10]

Then the key for the translation is the English string (since I don't really like message codes).

Related Topic