Commit d54c6159 by Luciano Baraglia

Add category for I18N in GII

parent 4827d732
...@@ -450,12 +450,12 @@ abstract class Generator extends Model ...@@ -450,12 +450,12 @@ abstract class Generator extends Model
} }
/** /**
* Generates a string depending on translatable property * Generates a string depending on enableI18N property
* @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->translatable) { if ($this->enableI18N) {
// If there are placeholders, use them // If there are placeholders, use them
if (count($placeholders) > 0) { if (count($placeholders) > 0) {
$search = ['array (', ')']; $search = ['array (', ')'];
...@@ -464,20 +464,20 @@ abstract class Generator extends Model ...@@ -464,20 +464,20 @@ abstract class Generator extends Model
} else { } else {
$ph = ''; $ph = '';
} }
$string = "Yii::t('app', '" . $string . "'" . $ph . ")"; $str = "Yii::t('" . $this->translationCategory . "', '" . $string . "'" . $ph . ")";
} else { } else {
// No translatable, replace placeholders by real words, if any // No I18N, replace placeholders by real words, if any
if (count($placeholders) > 0) { if (count($placeholders) > 0) {
$phKeys = array_map(function($word) { $phKeys = array_map(function($word) {
return '{' . $word . '}'; return '{' . $word . '}';
}, array_keys($placeholders)); }, array_keys($placeholders));
$phValues = array_values($placeholders); $phValues = array_values($placeholders);
$string = "'" . str_replace($phKeys, $phValues, $string) . "'"; $str = "'" . str_replace($phKeys, $phValues, $string) . "'";
} else { } else {
// No placeholders, just the given string // No placeholders, just the given string
$string = "'" . $string . "'"; $str = "'" . $string . "'";
} }
} }
return $string; return $str;
} }
} }
...@@ -115,6 +115,11 @@ yii.gii = (function ($) { ...@@ -115,6 +115,11 @@ 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 #generator-enablei18n').on('change',function () {
$('#crud-generator .field-generator-translationcategory').toggle($(this).is(':checked'));
}).change();
// hide Generate button if any input is changed // hide Generate button if any input is changed
$('.default-view .form-group input,select,textarea').change(function () { $('.default-view .form-group input,select,textarea').change(function () {
$('.default-view-results,.default-view-files').hide(); $('.default-view-results,.default-view-files').hide();
......
...@@ -36,7 +36,8 @@ class Generator extends \yii\gii\Generator ...@@ -36,7 +36,8 @@ 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; public $enableI18N = false;
public $translationCategory = 'app';
/** /**
* @inheritdoc * @inheritdoc
...@@ -72,7 +73,8 @@ class Generator extends \yii\gii\Generator ...@@ -72,7 +73,8 @@ 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'], [['enableI18N'], 'boolean'],
[['translationCategory'], 'validateTranslationCategory', 'skipOnEmpty' => false],
]); ]);
} }
...@@ -88,7 +90,8 @@ class Generator extends \yii\gii\Generator ...@@ -88,7 +90,8 @@ 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', 'enableI18N' => 'Enable I18N',
'translationCategory' => 'Translation Category',
]); ]);
} }
...@@ -110,8 +113,9 @@ class Generator extends \yii\gii\Generator ...@@ -110,8 +113,9 @@ 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. '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.', 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.',
]; ];
} }
...@@ -128,7 +132,7 @@ class Generator extends \yii\gii\Generator ...@@ -128,7 +132,7 @@ class Generator extends \yii\gii\Generator
*/ */
public function stickyAttributes() public function stickyAttributes()
{ {
return ['baseControllerClass', 'moduleID', 'indexWidgetType', 'translatable']; return ['baseControllerClass', 'moduleID', 'indexWidgetType', 'enableI18N', 'translationCategory'];
} }
/** /**
...@@ -158,6 +162,16 @@ class Generator extends \yii\gii\Generator ...@@ -158,6 +162,16 @@ class Generator extends \yii\gii\Generator
} }
/** /**
* Checks if translation category is not empty when I18N is enabled
*/
public function validateTranslationCategory()
{
if ((boolean)$this->enableI18N && empty($this->translationCategory)) {
$this->addError('translationCategory', "Translation Category cannot be blank.");
}
}
/**
* @inheritdoc * @inheritdoc
*/ */
public function generate() public function generate()
......
...@@ -14,4 +14,5 @@ echo $form->field($generator, 'indexWidgetType')->dropDownList([ ...@@ -14,4 +14,5 @@ echo $form->field($generator, 'indexWidgetType')->dropDownList([
'grid' => 'GridView', 'grid' => 'GridView',
'list' => 'ListView', 'list' => 'ListView',
]); ]);
echo $form->field($generator, 'translatable')->checkbox(); echo $form->field($generator, 'enableI18N')->checkbox();
echo $form->field($generator, 'translationCategory');
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