Commit c1fac6eb by Luciano Baraglia

More changes in GII I18N

parent 311bd469
...@@ -47,6 +47,15 @@ abstract class Generator extends Model ...@@ -47,6 +47,15 @@ abstract class Generator extends Model
* The value of this property is internally managed by this class. * The value of this property is internally managed by this class.
*/ */
public $template; public $template;
/**
* @var boolean whether the strings will be generated using `Yii::t()` or normal strings.
*/
public $enableI18N = false;
/**
* @var string the message category used by `Yii::t()` when `$enableI18N` is `true`.
* Defaults to `app`.
*/
public $messageCategory = 'app';
/** /**
* @return string name of the code generator * @return string name of the code generator
...@@ -76,6 +85,17 @@ abstract class Generator extends Model ...@@ -76,6 +85,17 @@ abstract class Generator extends Model
} }
/** /**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'enableI18N' => 'Enable I18N',
'messageCategory' => 'Message Category',
];
}
/**
* Returns a list of code template files that are required. * Returns a list of code template files that are required.
* Derived classes usually should override this method if they require the existence of * Derived classes usually should override this method if they require the existence of
* certain template files. * certain template files.
...@@ -95,7 +115,7 @@ abstract class Generator extends Model ...@@ -95,7 +115,7 @@ abstract class Generator extends Model
*/ */
public function stickyAttributes() public function stickyAttributes()
{ {
return ['template']; return ['template', 'enableI18N', 'messageCategory'];
} }
/** /**
...@@ -106,7 +126,11 @@ abstract class Generator extends Model ...@@ -106,7 +126,11 @@ abstract class Generator extends Model
*/ */
public function hints() public function hints()
{ {
return []; return [
'enableI18N' => 'This indicates whether the generator should generate strings using <code>Yii::t()</code> method.
Set this to <code>true</code> if you are planning to make your application translatable.',
'messageCategory' => 'This is the category used by <code>Yii::t()</code> in case you enable I18N.',
];
} }
/** /**
...@@ -359,6 +383,16 @@ abstract class Generator extends Model ...@@ -359,6 +383,16 @@ abstract class Generator extends Model
} }
/** /**
* Checks if message category is not empty when I18N is enabled.
*/
public function validateMessageCategory()
{
if ($this->enableI18N && empty($this->messageCategory)) {
$this->addError('messageCategory', "Message Category cannot be blank.");
}
}
/**
* @param string $value the attribute to be validated * @param string $value the attribute to be validated
* @return boolean whether the value is a reserved PHP keyword. * @return boolean whether the value is a reserved PHP keyword.
*/ */
...@@ -454,7 +488,7 @@ abstract class Generator extends Model ...@@ -454,7 +488,7 @@ abstract class Generator extends Model
* @param string $string the text be generated * @param string $string the text be generated
* @param array $placeholders the placeholders to use by `Yii::t()` * @param array $placeholders the placeholders to use by `Yii::t()`
*/ */
public function generateString($string = '', $placeholders = []){ public function generateString($string = '', $placeholders = []) {
if ($this->enableI18N) { if ($this->enableI18N) {
// If there are placeholders, use them // If there are placeholders, use them
if (!empty($placeholders)) { if (!empty($placeholders)) {
...@@ -464,7 +498,7 @@ abstract class Generator extends Model ...@@ -464,7 +498,7 @@ abstract class Generator extends Model
} else { } else {
$ph = ''; $ph = '';
} }
$str = "Yii::t('" . $this->translationCategory . "', '" . $string . "'" . $ph . ")"; $str = "Yii::t('" . $this->messageCategory . "', '" . $string . "'" . $ph . ")";
} else { } else {
// No I18N, replace placeholders by real words, if any // No I18N, replace placeholders by real words, if any
if (!empty($placeholders)) { if (!empty($placeholders)) {
......
...@@ -115,9 +115,9 @@ yii.gii = (function ($) { ...@@ -115,9 +115,9 @@ yii.gii = (function ($) {
$('#model-generator .field-generator-modelclass').toggle($(this).val().indexOf('*') == -1); $('#model-generator .field-generator-modelclass').toggle($(this).val().indexOf('*') == -1);
}).change(); }).change();
// CRUD generator: hide translationCategory when I18N is disabled // CRUD generator: hide messageCategory when I18N is disabled
$('#crud-generator #generator-enablei18n').change(function () { $('#crud-generator #generator-enablei18n').change(function () {
$('#crud-generator .field-generator-translationcategory').toggle($(this).is(':checked')); $('#crud-generator .field-generator-messagecategory').toggle($(this).is(':checked'));
}).change(); }).change();
// hide Generate button if any input is changed // hide Generate button if any input is changed
......
...@@ -36,8 +36,6 @@ class Generator extends \yii\gii\Generator ...@@ -36,8 +36,6 @@ class Generator extends \yii\gii\Generator
public $baseControllerClass = 'yii\web\Controller'; public $baseControllerClass = 'yii\web\Controller';
public $indexWidgetType = 'grid'; public $indexWidgetType = 'grid';
public $searchModelClass; public $searchModelClass;
public $enableI18N = false;
public $translationCategory = 'app';
/** /**
* @inheritdoc * @inheritdoc
...@@ -74,7 +72,7 @@ class Generator extends \yii\gii\Generator ...@@ -74,7 +72,7 @@ class Generator extends \yii\gii\Generator
[['modelClass'], 'validateModelClass'], [['modelClass'], 'validateModelClass'],
[['moduleID'], 'validateModuleID'], [['moduleID'], 'validateModuleID'],
[['enableI18N'], 'boolean'], [['enableI18N'], 'boolean'],
[['translationCategory'], 'validateTranslationCategory', 'skipOnEmpty' => false], [['messageCategory'], 'validateMessageCategory', 'skipOnEmpty' => false],
]); ]);
} }
...@@ -90,8 +88,6 @@ class Generator extends \yii\gii\Generator ...@@ -90,8 +88,6 @@ class Generator extends \yii\gii\Generator
'baseControllerClass' => 'Base Controller Class', 'baseControllerClass' => 'Base Controller Class',
'indexWidgetType' => 'Widget Used in Index Page', 'indexWidgetType' => 'Widget Used in Index Page',
'searchModelClass' => 'Search Model Class', 'searchModelClass' => 'Search Model Class',
'enableI18N' => 'Enable I18N',
'translationCategory' => 'Translation Category',
]); ]);
} }
...@@ -100,7 +96,7 @@ class Generator extends \yii\gii\Generator ...@@ -100,7 +96,7 @@ class Generator extends \yii\gii\Generator
*/ */
public function hints() public function hints()
{ {
return [ return array_merge(parent::hints(), [
'modelClass' => 'This is the ActiveRecord class associated with the table that CRUD will be built upon. 'modelClass' => 'This is the ActiveRecord class associated with the table that CRUD will be built upon.
You should provide a fully qualified class name, e.g., <code>app\models\Post</code>.', You should provide a fully qualified class name, e.g., <code>app\models\Post</code>.',
'controllerClass' => 'This is the name of the controller class to be generated. You should 'controllerClass' => 'This is the name of the controller class to be generated. You should
...@@ -113,10 +109,7 @@ class Generator extends \yii\gii\Generator ...@@ -113,10 +109,7 @@ class Generator extends \yii\gii\Generator
You may choose either <code>GridView</code> or <code>ListView</code>', You may choose either <code>GridView</code> or <code>ListView</code>',
'searchModelClass' => 'This is the name of the search model class to be generated. You should provide a fully 'searchModelClass' => 'This is the name of the search model class to be generated. You should provide a fully
qualified namespaced class name, e.g., <code>app\models\search\PostSearch</code>.', qualified namespaced class name, e.g., <code>app\models\search\PostSearch</code>.',
'enableI18N' => 'This indicates whether the generator should generate strings using <code>Yii::t()</code> method. ]);
Set this to <code>true</code> if you are planning to make your application translatable.',
'translationCategory' => 'This is the category used by <code>Yii::t()</code> in case you enable I18N.',
];
} }
/** /**
...@@ -132,7 +125,7 @@ class Generator extends \yii\gii\Generator ...@@ -132,7 +125,7 @@ class Generator extends \yii\gii\Generator
*/ */
public function stickyAttributes() public function stickyAttributes()
{ {
return ['baseControllerClass', 'moduleID', 'indexWidgetType', 'enableI18N', 'translationCategory']; return array_merge(parent::stickyAttributes(), ['baseControllerClass', 'moduleID', 'indexWidgetType']);
} }
/** /**
...@@ -162,16 +155,6 @@ class Generator extends \yii\gii\Generator ...@@ -162,16 +155,6 @@ class Generator extends \yii\gii\Generator
} }
/** /**
* Checks if translation category is not empty when I18N is enabled
*/
public function validateTranslationCategory()
{
if ($this->enableI18N && empty($this->translationCategory)) {
$this->addError('translationCategory', "Translation Category cannot be blank.");
}
}
/**
* @inheritdoc * @inheritdoc
*/ */
public function generate() public function generate()
......
...@@ -49,7 +49,7 @@ class <?= $searchModelClass ?> extends Model ...@@ -49,7 +49,7 @@ class <?= $searchModelClass ?> extends Model
{ {
return [ return [
<?php foreach ($labels as $name => $label): ?> <?php foreach ($labels as $name => $label): ?>
<?= "'$name' => " . addslashes($generator->generateString($label)) . ",\n" ?> <?= "'$name' => " . $generator->generateString(addslashes($label)) . ",\n" ?>
<?php endforeach; ?> <?php endforeach; ?>
]; ];
} }
......
...@@ -15,4 +15,4 @@ echo $form->field($generator, 'indexWidgetType')->dropDownList([ ...@@ -15,4 +15,4 @@ echo $form->field($generator, 'indexWidgetType')->dropDownList([
'list' => 'ListView', 'list' => 'ListView',
]); ]);
echo $form->field($generator, 'enableI18N')->checkbox(); echo $form->field($generator, 'enableI18N')->checkbox();
echo $form->field($generator, 'translationCategory'); echo $form->field($generator, 'messageCategory');
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment