Yii 1.1: stranslateablebehavior

Behavior that eases the translation of project model dynamic content.
14 followers

Intoduction

//Somewhere in controller:
$model = Page::model()->findPk(1);
echo $model->text; // Outputs Привет!
Yii::app()->language = 'en';
echo $model->text; // Outputs Hello!

Setup

  1. Download extension.
  2. Copy STranslateableBehavior folder to extensions directory.
  3. Copy TranslateCommand.php to commands directory.

Usage

1: Edit models, that you want to be able to translate. Enable STranslateableBehavior:

public function behaviors()
{
return array(     'translate'=>'application.extensions.STranslateableBehavior.STranslateableBehavior' );
}

2: Add translate method and define attributes to translate:

public function translate()
{
    // List of model attributes to translate
    return array('content', 'url'); //Example
}

Save model!

3: Edit config/main.php and add list of supported languages to app params array:

...
'params'=>array(
    'languages'=>array('ru', 'en'),
),
...

4: Create migration from command line run:

./yiic translate

Check migration php code!
Apply migration!

./yiic migrate

After migration you should see in your tables columns suffixed with language name.

Compatibility

Tested only on Yii v1.1.8r3324 with MySQL on PHP 5.3

Links

Github page
Forum topic

PS. Testers needed!

History

2011-29-07 - Initial release. Note - this is first release and this extension is a try to find a new way of model translation in Yii projects.

Total 1 comment

#14020 report it
Motin at 2013/07/12 06:32am
Improved version available

Thanks for creating this extension! After using the extension in a couple of projects, we created and released an improved version, available here: http://www.yiiframework.com/extension/i18n-columns/

Leave a comment

Please to leave your comment.

Create extension