Tuesday, July 3, 2012

how to translate strings to different languages using Yii

In Yii, you can translate strings to different languages
using Yii::t. As we all love perfect projects all language translations should be up to date.
If they are not, we would like to receive an e-mail about it.
Events come in handy again here. In particular, the CMessageSource::onMissingTransl
ation event that is called when the translation for a string passed to Yii::t is missing.
This time we will use the application configuration file protected/config/main.php to
attach an event handler as follows:

'components' => array(

// messages component class is CPhpMessageSource by default
'messages' => array(
// using static class method as event handler
'onMissingTranslation' => array('MyEventHandler',
'handleMissingTranslation'),
),

)


Now, we should implement our handler. Create protected/components/
MyEventHandler.php as follows:
class MyEventHandler
{
static function handleMissingTranslation($event)
{
// event class for this event is CMissingTranslationEvent
// so we can get some info about the message
$text = implode("\n", array(
'Language: '.$event->language,
'Category:'.$event->category,
'Message:'.$event->message
));
// sending email
mail('admin@example.com', 'Missing translation', $text);
}
}


Find more on http://Learnyii.ebharat.org

1 comment:

  1. If you're interested in an online localization alternative, try https://poeditor.com/

    ReplyDelete