PHP code example of mikehaertl / translatable

1. Go to this page and download the library: Download mikehaertl/translatable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mikehaertl / translatable example snippets




// Load a record in application default language ...
$car = New Car;
$car->manufacturer_id = 123;

// Set a description, e.g. in english if this is the app language
$car->description = 'English description';

// Save both: the new car record and a related translation record
$car->save();

// Change language
$car->language = 'de';
$car->description = 'German description';

// We could again call save() here, but we only want to save the translation record
$car->saveTranslation();


// Load a record in a specific language
$car = Car::model()->language('de')->findByPk(1);

// Output: 'German description'
echo $car->description;

$car->language = 'en';

// Output: 'English description'
echo $car->description;


public function behaviors()
{
    return array(
        // IMPORTANT: Always use 'Translatable' as key
        'Translatable' => array(
            'class'                 => 'ext.translatable.Translatable',
            'translationAttributes' => array('title','abstract'),

            // Optional configuration with their defaults
            'translationRelation'   => 'translation',
            'languageColumn'        => 'language',
        ),
    );
}


public function relations()
{
    return array(
        // IMPORTANT: Use the language column as `index`
        'translations' => array(self::HAS_MANY, 'BookTranslations', 'book_id', 'index'=>'language'),
    );
}


public function rules()
{
    return array(
        array('publisher_id,author_id,title,abstract', '