Yii 1.1: db-missing-translations

Add missing translations to the DB automatically
8 followers

Basically this was not designed to be an extension but rather a tool for development stage where you do not want to manually add every single language string into the database messages table for each language you need the application to be translated to.

So what this does is basically a class that runs when the onMissingTranslation event raises and it checks if the message being translated exists in the source table, If not it adds it to the source messages table if it is then it skips this stage, The next stage is to check if this message exists in the messages table with the currently used language if it is then nothing is done if not then the message is added to the messages table for translation.

Note: It is highly recommended NOT to use this on a production environment as it run two queries for each message to check if it exists or not, Since i didn't plan this to be optimized and made this as a personal tool i have no plans updating this currently and is only released to help who ever needs such tool.

Resources

Documentation

Requirements

  • Yii 1.0 or above

Installation

  • Extract the 'MissingMessages.php' file in a directory you have imported.
  • Extract the 'SourceMessage.php' & 'Message.php' Model files under your models directory or any other directory your application imports it's contents.

Usage

Just add the following line to the 'messages' component array

'onMissingTranslation' => array('MissingMessages', 'load')

So it looks like this:

'messages' => array(
                        'onMissingTranslation' => array('MissingMessages', 'load')
         ),

Change Log

May 2, 2010

  • Initial release.

Total 3 comments

#9738 report it
Clem at 2012/09/07 02:12pm
it occured... nothing...

Does it work for you? I have any message stored in the database, not error message... when few time ago was working. Can be Yii issue?

#8537 report it
seb7 at 2012/06/11 07:00am
Infinite loop

I had trouble because my Message and SourceMessage Classes had Yii::t() calls so i get infinite loops calling onMissingTranslation callback.

Solution : Deactivate the event handling inside missingTranslation function, then reactive it at the end of the function.

public static function missingTranslation($event)
{
   // detachement du composant pour éviter boucles infinies,réattaché la fin
   Yii::app()->messages->detachEventHandler('onMissingTranslation', array('MissingMessages', 'missingTranslation'));
 
...
 
    Yii::app()->messages->attachEventHandler('onMissingTranslation', array('MissingMessages', 'missingTranslation'));
 
}

Thanks for sharing, it was a good base for my needs :)

#2597 report it
cass at 2011/01/21 05:09pm
Works fine

Thanks - very useful for development. Well done.

Leave a comment

Please to leave your comment.

Create extension
  • Yii Version: 1.1
  • License: New BSD License
  • Developed by: Vince.
  • Category: Others
  • Votes: +5 / -1
  • Downloaded: 1,058 times
  • Created on: May 2, 2010
  • Last updated: May 2, 2010