Translation

Hi,

What is the best way of translating long strings with Yii2. If I follow following procedure message generator extract only the first line. If I merge all lines to single it works fine but readability is very poor. Do you guys have any good solution for this.


$message = Yii::t('app', 'Dear {tenantName}, ' .

	'<p>Your property request was rejected by {ownerName}</p>' .

	'<p>Property code is:{propertyCode}</p>' .

	'Thank you.', [

	'ownerName' => $ownerName,

	'tenantName' => $tenantName,

	'propertyCode' => $code

]);

Thank you.

Ideally, the problem should be solved in Yii’s “message” command, by supporting the concatenated literal strings. But, well, it looks to me a bit difficult to implement. And I agree with you that a very long line could be an obstacle to us, too.

In short, I don’t have any good idea other than dividing the string like the following:




$message = Yii::t('app', 'Dear {tenantName}, ', ['tenantName' => $tenantName]) .

    Yii::t('app', '<p>Your property request was rejected by {ownerName}</p>', ['ownerName' => $ownerName]) .

    Yii::t('app', '<p>Property code is:{propertyCode}</p>', ['propertyCode' => $code]) .

    Yii::t('app', 'Thank you.');



Or, creating language specific views would be a simple and clean solution if the message could be long and complicated.

Didn’t try it since never used multi language in yii2 till now but I think it should work.

Here is my 2 cents.

As main application language define something "invalid" like my_ln

Then write your messages as meta string like




$message = Yii::t('app', 'ERROR_01_PROPERTY_CODE', [

    	'ownerName' => $ownerName,

    	'tenantName' => $tenantName,

    	'propertyCode' => $code

]);



Then you manage the translations as always.

Using and invalid code language assure that the string ERROR_01_PROPERTY_CODE is never shown and instead it goes always on a translation.

If on one side you have a much more readable code, on the other side is going to be a little (but very little) more difficult to do the translation as you do not have the original text inside the language file you are translating, you need to open 2 files at same time.

In my opinion, it is wrong to attempt to translate a huge string like that, made up from several sentences.

A translation string should be as short as possible in order to make it easier to translate it into numerous languages.

The best approach is to find the smallest isolated sentences and, like Softark demonstrated, and translate each in order.

That would make it much easier for the translators, and for you. ;)

Hi Jocmoe,

Thanks for the response. I agree with you. But code segment mentioned is related to email message. Therefore it can contain several lines. I think best option is to go for language specific views for this kind of situation.

Thanks,

Aruna