PHP code example of zaszczyk / translatable

1. Go to this page and download the library: Download zaszczyk/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/ */

    

zaszczyk / translatable example snippets


use \Ovide\Lib\Translate as Translate;

/**
 * Your translatable model
 * @property string $description
 */
class MyModel extends Translate\Model
{
    public $id;
    public $value;
    public $timestamp;
    protected $_translatable = ['description'];
}

/**
 * This is a default basic abstract model, but you can add yours
 */
class Translation extends Translate\Adapter\Model\AbstractModel{}


$di = new \Phalcon\DI\FactoryDefault();
$di->setShared('db', function () {
    return new \Phalcon\Db\Adapter\Pdo\Mysql([/* my config */]);
});

/**
 *             HERE WE SET THE TRANSLATOR
 */
$di->setShared('translator', function() {
    $service = new Translate\Service();
    //All translatable models from 'db'
    //will use 'Translation' to manage the translations
    $service->attachAdapter(
        'db',
        Translate\Adapter\Model\Manager::class,
        ['backendModel' => 'Translation']
    );
    //You can use a default language resolver
    Translate\Model::setLanguageResolver(function() use($di){
        //You can put anything here
        if (isset($_COOKIE['lang'])) return $_COOKIE['lang'];
        return 'en';
    });
});

$model = new MyModel();
$model->value = 'foo';
//Will set the text using the default language
$model->description = 'my description';
//You can change the current language
$model->setCurrentLang('es');
$model->description = 'mi descripción';
//Or use setter/getter
$model->setTranslation('description', 'la meva descipció', 'ca');
$model->save();

interface TranslationInterface
{
    public static function retrieve(Model $model, $pk, array $options = null);
    public function get($field, $language);
    public function set($field, $language, $value);
    public function persist(array $records = null);
    public function remove();
}