PHP code example of lajax / yii2-translate-manager
1. Go to this page and download the library: Download lajax/yii2-translate-manager 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/ */
'modules' => [
'translatemanager' => [
'class' => 'lajax\translatemanager\Module',
'root' => '@app', // The root directory of the project scan.
'scanRootParentDirectory' => true, // Whether scan the defined `root` parent directory, or the folder itself.
// IMPORTANT: for detailed instructions read the chapter about root configuration.
'layout' => 'language', // Name of the used layout. If using own layout use 'null'.
'allowedIPs' => ['127.0.0.1'], // IP addresses from which the translation interface is accessible.
'roles' => ['@'], // For setting access levels to the translating interface.
'tmpDir' => '@runtime', // Writable directory for the client-side temporary language files.
// IMPORTANT: must be identical for all applications (the AssetsManager serves the JavaScript files containing language elements from this directory).
'phpTranslators' => ['::t'], // list of the php function for translating messages.
'jsTranslators' => ['lajax.t'], // list of the js function for translating messages.
'patterns' => ['*.js', '*.php'],// list of file extensions that contain language elements.
'ignoredCategories' => ['yii'], // these categories won't be '\lajax\translatemanager\services\scanners\ScannerPhpArray',
'\lajax\translatemanager\services\scanners\ScannerJavaScriptFunction',
'\lajax\translatemanager\services\scanners\ScannerDatabase',
],
],
],
namespace common\controllers;
use lajax\translatemanager\helpers\Language;
// IMPORTANT: all Controllers must originate from this Controller!
class Controller extends \yii\web\Controller {
public function init() {
Language::registerAssets();
parent::init();
}
}
lajax.t('Apple');
lajax.t('Hello {name}!', {name:'World'});
lajax.t("Don't be so upset.");
Yii::t('category', 'Apple');
Yii::t('category', 'Hello {name}!', ['name' => 'World']);
Yii::t('category', "Don't be so upset.");
use lajax\translatemanager\helpers\Language as Lx;
Lx::t('category', 'Apple');
Lx::t('category', 'Hello {name}!', ['name' => 'World']);
Lx::t('category', "Don't be so upset.");
/**
* @translate
*/
private $_STATUSES = [
self::STATUS_INACTIVE => 'Inactive',
self::STATUS_ACTIVE => 'Active',
self::STATUS_DELETED => 'Deleted'
];
/**
* Returning the 'status' array on the site's own language.
* return array
*/
public function getStatuses() {
return \lajax\translatemanager\helpers\Language::a($this->_STATUSES);
}
/**
* @translate
*/
private $_GENDERS = ['Male', 'Female'];
/**
* Returning the 'genders' array in German
* return array
*/
public function getGenders() {
return \lajax\translatemanager\helpers\Language::a($this->_GENDERS, 'de-DE');
}
namespace common\models;
use lajax\translatemanager\helpers\Language;
/**
* This is the model class for table "category".
*
* @property string $category_id
* @property string $name
* @property string $description
*/
class Category extends \yii\db\ActiveRecord {
// afterFind & beforeSave:
/**
* @var Returning the 'name' attribute on the site's own language.
*/
public $name_t;
/**
* @var Returning the 'description' attribute on the site's own language.
*/
public $description_t;
/* ... */
public function afterFind() {
$this->name_t = Language::d($this->name);
$this->description_t = Language::d($this->descrioption);
// or If the category is the database table name.
// $this->name_t = Language::t(static::tableName(), $this->name);
// $this->description_t = Language::t(static::tableName(), $this->description);
parent::afterFind();
}
public function beforeSave($insert) {
if (parent::beforeSave($insert)) {
Language::saveMessage($this->name);
Language::saveMessage($this->description);
// or If the category is the database table name.
// Language::saveMessage($this->name, static::tableName());
// Language::saveMessage($this->description, static::tableName());
return true;
}
return false;
}
// or GETTERS:
/**
* @return string Returning the 'name' attribute on the site's own language.
*/
public function getName($params = [], $language = null) {
return Language::d($this->name, $params, $language);
// or If the category is the database table name.
// return Language::t(static::tableName(), $this->name, $params, $language);
}
/**
* @return string Returning the 'description' attribute on the site's own language.
*/
public function getDescription($params = [], $language = null) {
return Language::d($this->description, $params, $language);
// or If the category is the database table name.
// return Language::t(static::tableName(), $this->description, $params, $language);
}
}
namespace common\models;
/**
* This is the model class for table "category".
*
* @property string $category_id
* @property string $name
* @property string $description
*/
class Category extends \yii\db\ActiveRecord {
// TranslateBehavior
public function behaviors()
{
return [
[
'class' => \lajax\translatemanager\behaviors\TranslateBehavior::className(),
'translateAttributes' => ['name', 'description'],
],
// or If the category is the database table name.
// [
// 'class' => \lajax\translatemanager\behaviors\TranslateBehavior::className(),
// 'translateAttributes' => ['name', 'description'],
// 'category' => static::tableName(),
// ],
];
}
}
/translatemanager/language/list // List of languages and modifying their status
/translatemanager/language/create // Create languages
/translatemanager/language/scan // Scan the project for new multilingual elements
/translatemanager/language/optimizer // Optimise the database