Browse Source

Add category for I18N in GII

tags/2.0.0-beta
Luciano Baraglia 11 years ago
parent
commit
d54c6159d9
  1. 14
      extensions/gii/Generator.php
  2. 5
      extensions/gii/assets/gii.js
  3. 24
      extensions/gii/generators/crud/Generator.php
  4. 3
      extensions/gii/generators/crud/form.php

14
extensions/gii/Generator.php

@ -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 array $placeholders the placeholders to use by `Yii::t()`
*/
public function generateString($string = '', $placeholders = []){
if ($this->translatable) {
if ($this->enableI18N) {
// If there are placeholders, use them
if (count($placeholders) > 0) {
$search = ['array (', ')'];
@ -464,20 +464,20 @@ abstract class Generator extends Model
} else {
$ph = '';
}
$string = "Yii::t('app', '" . $string . "'" . $ph . ")";
$str = "Yii::t('" . $this->translationCategory . "', '" . $string . "'" . $ph . ")";
} else {
// No translatable, replace placeholders by real words, if any
// No I18N, 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) . "'";
$str = "'" . str_replace($phKeys, $phValues, $string) . "'";
} else {
// No placeholders, just the given string
$string = "'" . $string . "'";
$str = "'" . $string . "'";
}
}
return $string;
return $str;
}
}

5
extensions/gii/assets/gii.js

@ -115,6 +115,11 @@ yii.gii = (function ($) {
$('#model-generator .field-generator-modelclass').toggle($(this).val().indexOf('*') == -1);
}).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
$('.default-view .form-group input,select,textarea').change(function () {
$('.default-view-results,.default-view-files').hide();

24
extensions/gii/generators/crud/Generator.php

@ -36,7 +36,8 @@ class Generator extends \yii\gii\Generator
public $baseControllerClass = 'yii\web\Controller';
public $indexWidgetType = 'grid';
public $searchModelClass;
public $translatable = false;
public $enableI18N = false;
public $translationCategory = 'app';
/**
* @inheritdoc
@ -72,7 +73,8 @@ class Generator extends \yii\gii\Generator
[['indexWidgetType'], 'in', 'range' => ['grid', 'list']],
[['modelClass'], 'validateModelClass'],
[['moduleID'], 'validateModuleID'],
[['translatable'], 'boolean'],
[['enableI18N'], 'boolean'],
[['translationCategory'], 'validateTranslationCategory', 'skipOnEmpty' => false],
]);
}
@ -88,7 +90,8 @@ class Generator extends \yii\gii\Generator
'baseControllerClass' => 'Base Controller Class',
'indexWidgetType' => 'Widget Used in Index Page',
'searchModelClass' => 'Search Model Class',
'translatable' => 'Generate Translatable Strings',
'enableI18N' => 'Enable I18N',
'translationCategory' => 'Translation Category',
]);
}
@ -110,8 +113,9 @@ class Generator extends \yii\gii\Generator
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
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.',
'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
*/
public function stickyAttributes()
{
return ['baseControllerClass', 'moduleID', 'indexWidgetType', 'translatable'];
return ['baseControllerClass', 'moduleID', 'indexWidgetType', 'enableI18N', 'translationCategory'];
}
/**
@ -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
*/
public function generate()

3
extensions/gii/generators/crud/form.php

@ -14,4 +14,5 @@ echo $form->field($generator, 'indexWidgetType')->dropDownList([
'grid' => 'GridView',
'list' => 'ListView',
]);
echo $form->field($generator, 'translatable')->checkbox();
echo $form->field($generator, 'enableI18N')->checkbox();
echo $form->field($generator, 'translationCategory');

Loading…
Cancel
Save