Internationalization - return a string using onMissingTranslation?

I’m struggling with an internationalization problem. I have set up an array in /messages/en/lang.php in which I fill with the english translation.

Now I have a huge database and it’s quite a struggle to get it all translated so I’d like the words not defined in the array of /messages/en/lang.php to run through the Bing translate api I have set up.

I read in the docs about the possibility of using the onMissingTranslation method, but it seems to be unable to return a value to be used instead.

I have set up the config to use the onMissingTranslation:




'messages' => array(

	'onMissingTranslation' => array('MissingTranslation', 'bingTranslate'),

)



This is not working but it shows what I would like to do. I would like to return the translated sting.




class MissingTranslation

{

    public function bingTranslate($event)

    {

        // ... translate by bing

        

        return $translatedText;

    }

}



Is there any way to get this to work or is there an alternative?

Thanks!

CPhpMessageSource is raising an event when the message isn’t found, which you should be able to intercept in order to perform the translation. I haven’t used events much, but if you do intercept it, I believe you can set the $event->message property to specify the translated string.

Thanks! I realise this is is mainly for logging this missing words, yet I can’t seem to find a better alternative to do this. Any ideas maybe?

This seems to work though. Thank you.

I don’t see any reason not to do it this way. It would probably be a good idea to log the missing translation as well though. If you weren’t intended to be able to provide a message using this feature, it wouldn’t be made available as a property of the event.