Commit 8c8f3d0c by Luciano Baraglia

Proposal: GII generate strings for I18N

parent 772f56f0
...@@ -448,4 +448,36 @@ abstract class Generator extends Model ...@@ -448,4 +448,36 @@ abstract class Generator extends Model
return in_array(strtolower($value), $keywords, true); return in_array(strtolower($value), $keywords, true);
} }
/**
* Generates a string depending on translatable property
* @param string $string the text be generated
* @param array $placeholders the placeholders to use by `Yii::t()`
*/
public function generateString($string = '', $placeholders = []){
if ($this->translatable) {
// If there are placeholders, use them
if (count($placeholders) > 0) {
$search = ['array (', ')'];
$replace = ['[', ']'];
$ph = ', ' . str_replace($search, $replace, var_export($placeholders, true));
} else {
$ph = '';
}
$str = "Yii::t('app', '" . $string . "'" . $ph . ")";
} else {
// No translatable, replace placeholders by real words, if any
if (count($placeholders) > 0) {
$phKeys = array_map(function($word) {
return '{' . $word . '}';
}, array_keys($placeholders));
$phValues = array_values($placeholders);
$string = "'" . str_replace($phKeys, $phValues, $string) . "'";
} else {
// No placeholders, just the given string
$string = "'" . $string . "'";
}
}
return $string;
}
} }
...@@ -36,6 +36,7 @@ class Generator extends \yii\gii\Generator ...@@ -36,6 +36,7 @@ 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 $translatable = false;
/** /**
* @inheritdoc * @inheritdoc
...@@ -71,6 +72,7 @@ class Generator extends \yii\gii\Generator ...@@ -71,6 +72,7 @@ class Generator extends \yii\gii\Generator
[['indexWidgetType'], 'in', 'range' => ['grid', 'list']], [['indexWidgetType'], 'in', 'range' => ['grid', 'list']],
[['modelClass'], 'validateModelClass'], [['modelClass'], 'validateModelClass'],
[['moduleID'], 'validateModuleID'], [['moduleID'], 'validateModuleID'],
[['translatable'], 'boolean'],
]); ]);
} }
...@@ -86,6 +88,7 @@ class Generator extends \yii\gii\Generator ...@@ -86,6 +88,7 @@ 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',
'translatable' => 'Generate Translatable Strings',
]); ]);
} }
...@@ -107,6 +110,8 @@ class Generator extends \yii\gii\Generator ...@@ -107,6 +110,8 @@ 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>.',
'translatable' => '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.',
]; ];
} }
...@@ -123,7 +128,7 @@ class Generator extends \yii\gii\Generator ...@@ -123,7 +128,7 @@ class Generator extends \yii\gii\Generator
*/ */
public function stickyAttributes() public function stickyAttributes()
{ {
return ['baseControllerClass', 'moduleID', 'indexWidgetType']; return ['baseControllerClass', 'moduleID', 'indexWidgetType', 'translatable'];
} }
/** /**
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
use yii\helpers\StringHelper; use yii\helpers\StringHelper;
/** /**
* This is the template for generating a CRUD controller class file. * This is the template for generating CRUD search class of the specified model.
* *
* @var yii\web\View $this * @var yii\web\View $this
* @var yii\gii\generators\crud\Generator $generator * @var yii\gii\generators\crud\Generator $generator
...@@ -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($label) . "',\n" ?> <?= "'$name' => " . $generator->generateString(addslashes($label)) . ",\n" ?>
<?php endforeach; ?> <?php endforeach; ?>
]; ];
} }
......
...@@ -36,7 +36,7 @@ use yii\widgets\ActiveForm; ...@@ -36,7 +36,7 @@ use yii\widgets\ActiveForm;
echo " <?= " . $generator->generateActiveField($attribute) . " ?>\n\n"; echo " <?= " . $generator->generateActiveField($attribute) . " ?>\n\n";
} ?> } ?>
<div class="form-group"> <div class="form-group">
<?= "<?= " ?>Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> <?= "<?= " ?>Html::submitButton($model->isNewRecord ? <?= $generator->generateString('Create') ?> : <?= $generator->generateString('Update') ?>, ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div> </div>
<?= "<?php " ?>ActiveForm::end(); ?> <?= "<?php " ?>ActiveForm::end(); ?>
......
...@@ -39,8 +39,8 @@ foreach ($generator->getColumnNames() as $attribute) { ...@@ -39,8 +39,8 @@ foreach ($generator->getColumnNames() as $attribute) {
} }
?> ?>
<div class="form-group"> <div class="form-group">
<?= "<?= " ?>Html::submitButton('Search', ['class' => 'btn btn-primary']) ?> <?= "<?= " ?>Html::submitButton(<?= $generator->generateString('Search') ?>, ['class' => 'btn btn-primary']) ?>
<?= "<?= " ?>Html::resetButton('Reset', ['class' => 'btn btn-default']) ?> <?= "<?= " ?>Html::resetButton(<?= $generator->generateString('Reset') ?>, ['class' => 'btn btn-default']) ?>
</div> </div>
<?= "<?php " ?>ActiveForm::end(); ?> <?= "<?php " ?>ActiveForm::end(); ?>
......
...@@ -18,8 +18,8 @@ use yii\helpers\Html; ...@@ -18,8 +18,8 @@ use yii\helpers\Html;
* @var <?= ltrim($generator->modelClass, '\\') ?> $model * @var <?= ltrim($generator->modelClass, '\\') ?> $model
*/ */
$this->title = 'Create <?= Inflector::camel2words(StringHelper::basename($generator->modelClass)) ?>'; $this->title = <?= $generator->generateString('Create {modelClass}', ['modelClass' => Inflector::camel2words(StringHelper::basename($generator->modelClass))]) ?>;
$this->params['breadcrumbs'][] = ['label' => '<?= Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass))) ?>', 'url' => ['index']]; $this->params['breadcrumbs'][] = ['label' => <?= $generator->generateString(Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass)))) ?>, 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title; $this->params['breadcrumbs'][] = $this->title;
?> ?>
<div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-create"> <div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-create">
......
...@@ -23,7 +23,7 @@ use <?= $generator->indexWidgetType === 'grid' ? "yii\\grid\\GridView" : "yii\\w ...@@ -23,7 +23,7 @@ use <?= $generator->indexWidgetType === 'grid' ? "yii\\grid\\GridView" : "yii\\w
* @var <?= ltrim($generator->searchModelClass, '\\') ?> $searchModel * @var <?= ltrim($generator->searchModelClass, '\\') ?> $searchModel
*/ */
$this->title = '<?= Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass))) ?>'; $this->title = <?= $generator->generateString(Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass)))) ?>;
$this->params['breadcrumbs'][] = $this->title; $this->params['breadcrumbs'][] = $this->title;
?> ?>
<div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-index"> <div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-index">
...@@ -33,7 +33,7 @@ $this->params['breadcrumbs'][] = $this->title; ...@@ -33,7 +33,7 @@ $this->params['breadcrumbs'][] = $this->title;
<?= "<?php " . ($generator->indexWidgetType === 'grid' ? "// " : "") ?>echo $this->render('_search', ['model' => $searchModel]); ?> <?= "<?php " . ($generator->indexWidgetType === 'grid' ? "// " : "") ?>echo $this->render('_search', ['model' => $searchModel]); ?>
<p> <p>
<?= "<?= " ?>Html::a('Create <?= Inflector::camel2words(StringHelper::basename($generator->modelClass)) ?>', ['create'], ['class' => 'btn btn-success']) ?> <?= "<?= " ?>Html::a(<?= $generator->generateString('Create {modelClass}', ['modelClass' => Inflector::camel2words(StringHelper::basename($generator->modelClass))]) ?>, ['create'], ['class' => 'btn btn-success']) ?>
</p> </p>
<?php if ($generator->indexWidgetType === 'grid'): ?> <?php if ($generator->indexWidgetType === 'grid'): ?>
......
...@@ -20,10 +20,10 @@ use yii\helpers\Html; ...@@ -20,10 +20,10 @@ use yii\helpers\Html;
* @var <?= ltrim($generator->modelClass, '\\') ?> $model * @var <?= ltrim($generator->modelClass, '\\') ?> $model
*/ */
$this->title = 'Update <?= Inflector::camel2words(StringHelper::basename($generator->modelClass)) ?>: ' . $model-><?= $generator->getNameAttribute() ?>; $this->title = <?= $generator->generateString('Update {modelClass}: ', ['modelClass' => Inflector::camel2words(StringHelper::basename($generator->modelClass))]) ?> . $model-><?= $generator->getNameAttribute() ?>;
$this->params['breadcrumbs'][] = ['label' => '<?= Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass))) ?>', 'url' => ['index']]; $this->params['breadcrumbs'][] = ['label' => <?= $generator->generateString(Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass)))) ?>, 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model-><?= $generator->getNameAttribute() ?>, 'url' => ['view', <?= $urlParams ?>]]; $this->params['breadcrumbs'][] = ['label' => $model-><?= $generator->getNameAttribute() ?>, 'url' => ['view', <?= $urlParams ?>]];
$this->params['breadcrumbs'][] = 'Update'; $this->params['breadcrumbs'][] = <?= $generator->generateString('Update') ?>;
?> ?>
<div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-update"> <div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-update">
......
...@@ -22,7 +22,7 @@ use yii\widgets\DetailView; ...@@ -22,7 +22,7 @@ use yii\widgets\DetailView;
*/ */
$this->title = $model-><?= $generator->getNameAttribute() ?>; $this->title = $model-><?= $generator->getNameAttribute() ?>;
$this->params['breadcrumbs'][] = ['label' => '<?= Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass))) ?>', 'url' => ['index']]; $this->params['breadcrumbs'][] = ['label' => <?= $generator->generateString(Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass)))) ?>, 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title; $this->params['breadcrumbs'][] = $this->title;
?> ?>
<div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-view"> <div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-view">
...@@ -30,11 +30,11 @@ $this->params['breadcrumbs'][] = $this->title; ...@@ -30,11 +30,11 @@ $this->params['breadcrumbs'][] = $this->title;
<h1><?= "<?= " ?>Html::encode($this->title) ?></h1> <h1><?= "<?= " ?>Html::encode($this->title) ?></h1>
<p> <p>
<?= "<?= " ?>Html::a('Update', ['update', <?= $urlParams ?>], ['class' => 'btn btn-primary']) ?> <?= "<?= " ?>Html::a(<?= $generator->generateString('Update') ?>, ['update', <?= $urlParams ?>], ['class' => 'btn btn-primary']) ?>
<?= "<?= " ?>Html::a('Delete', ['delete', <?= $urlParams ?>], [ <?= "<?= " ?>Html::a(<?= $generator->generateString('Delete') ?>, ['delete', <?= $urlParams ?>], [
'class' => 'btn btn-danger', 'class' => 'btn btn-danger',
'data' => [ 'data' => [
'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'), 'confirm' => <?= $generator->generateString('Are you sure you want to delete this item?') ?>,
'method' => 'post', 'method' => 'post',
], ],
]) ?> ]) ?>
......
...@@ -14,3 +14,4 @@ echo $form->field($generator, 'indexWidgetType')->dropDownList([ ...@@ -14,3 +14,4 @@ echo $form->field($generator, 'indexWidgetType')->dropDownList([
'grid' => 'GridView', 'grid' => 'GridView',
'list' => 'ListView', 'list' => 'ListView',
]); ]);
echo $form->field($generator, 'translatable')->checkbox();
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