Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
8c8f3d0c
Commit
8c8f3d0c
authored
Mar 20, 2014
by
Luciano Baraglia
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Proposal: GII generate strings for I18N
parent
772f56f0
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
55 additions
and
17 deletions
+55
-17
Generator.php
extensions/gii/Generator.php
+32
-0
Generator.php
extensions/gii/generators/crud/Generator.php
+6
-1
search.php
extensions/gii/generators/crud/default/search.php
+2
-2
_form.php
extensions/gii/generators/crud/default/views/_form.php
+1
-1
_search.php
extensions/gii/generators/crud/default/views/_search.php
+2
-2
create.php
extensions/gii/generators/crud/default/views/create.php
+2
-2
index.php
extensions/gii/generators/crud/default/views/index.php
+2
-2
update.php
extensions/gii/generators/crud/default/views/update.php
+3
-3
view.php
extensions/gii/generators/crud/default/views/view.php
+4
-4
form.php
extensions/gii/generators/crud/form.php
+1
-0
No files found.
extensions/gii/Generator.php
View file @
8c8f3d0c
...
...
@@ -448,4 +448,36 @@ abstract class Generator extends Model
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
;
}
}
extensions/gii/generators/crud/Generator.php
View file @
8c8f3d0c
...
...
@@ -36,6 +36,7 @@ class Generator extends \yii\gii\Generator
public
$baseControllerClass
=
'yii\web\Controller'
;
public
$indexWidgetType
=
'grid'
;
public
$searchModelClass
;
public
$translatable
=
false
;
/**
* @inheritdoc
...
...
@@ -71,6 +72,7 @@ class Generator extends \yii\gii\Generator
[[
'indexWidgetType'
],
'in'
,
'range'
=>
[
'grid'
,
'list'
]],
[[
'modelClass'
],
'validateModelClass'
],
[[
'moduleID'
],
'validateModuleID'
],
[[
'translatable'
],
'boolean'
],
]);
}
...
...
@@ -86,6 +88,7 @@ class Generator extends \yii\gii\Generator
'baseControllerClass'
=>
'Base Controller Class'
,
'indexWidgetType'
=>
'Widget Used in Index Page'
,
'searchModelClass'
=>
'Search Model Class'
,
'translatable'
=>
'Generate Translatable Strings'
,
]);
}
...
...
@@ -107,6 +110,8 @@ 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.
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
*/
public
function
stickyAttributes
()
{
return
[
'baseControllerClass'
,
'moduleID'
,
'indexWidgetType'
];
return
[
'baseControllerClass'
,
'moduleID'
,
'indexWidgetType'
,
'translatable'
];
}
/**
...
...
extensions/gii/generators/crud/default/search.php
View file @
8c8f3d0c
...
...
@@ -3,7 +3,7 @@
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\gii\generators\crud\Generator $generator
...
...
@@ -49,7 +49,7 @@ class <?= $searchModelClass ?> extends Model
{
return [
<?php
foreach
(
$labels
as
$name
=>
$label
)
:
?>
<?=
"'
$name
' =>
'"
.
addslashes
(
$label
)
.
"'
,
\n
"
?>
<?=
"'
$name
' =>
"
.
$generator
->
generateString
(
addslashes
(
$label
))
.
"
,
\n
"
?>
<?php
endforeach
;
?>
];
}
...
...
extensions/gii/generators/crud/default/views/_form.php
View file @
8c8f3d0c
...
...
@@ -36,7 +36,7 @@ use yii\widgets\ActiveForm;
echo
" <?= "
.
$generator
->
generateActiveField
(
$attribute
)
.
" ?>
\n\n
"
;
}
?>
<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>
<?=
"<?php "
?>
ActiveForm::end(); ?>
...
...
extensions/gii/generators/crud/default/views/_search.php
View file @
8c8f3d0c
...
...
@@ -39,8 +39,8 @@ foreach ($generator->getColumnNames() as $attribute) {
}
?>
<div
class=
"form-group"
>
<?=
"<?= "
?>
Html::submitButton(
'Search'
, ['class' => 'btn btn-primary']) ?>
<?=
"<?= "
?>
Html::resetButton(
'Reset'
, ['class' => 'btn btn-default']) ?>
<?=
"<?= "
?>
Html::submitButton(
<?=
$generator
->
generateString
(
'Search'
)
?>
, ['class' => 'btn btn-primary']) ?>
<?=
"<?= "
?>
Html::resetButton(
<?=
$generator
->
generateString
(
'Reset'
)
?>
, ['class' => 'btn btn-default']) ?>
</div>
<?=
"<?php "
?>
ActiveForm::end(); ?>
...
...
extensions/gii/generators/crud/default/views/create.php
View file @
8c8f3d0c
...
...
@@ -18,8 +18,8 @@ use yii\helpers\Html;
* @var
<?=
ltrim
(
$generator
->
modelClass
,
'\\'
)
?>
$model
*/
$this->title =
'Create
<?=
Inflector
::
camel2words
(
StringHelper
::
basename
(
$generator
->
modelClass
))
?>
'
;
$this->params['breadcrumbs'][] = ['label' =>
'
<?=
Inflector
::
pluralize
(
Inflector
::
camel2words
(
StringHelper
::
basename
(
$generator
->
modelClass
)))
?>
'
, 'url' => ['index']];
$this->title =
<?=
$generator
->
generateString
(
'Create {modelClass}'
,
[
'modelClass'
=>
Inflector
::
camel2words
(
StringHelper
::
basename
(
$generator
->
modelClass
))])
?>
;
$this->params['breadcrumbs'][] = ['label' =>
<?=
$generator
->
generateString
(
Inflector
::
pluralize
(
Inflector
::
camel2words
(
StringHelper
::
basename
(
$generator
->
modelClass
))))
?>
, 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div
class=
"
<?=
Inflector
::
camel2id
(
StringHelper
::
basename
(
$generator
->
modelClass
))
?>
-create"
>
...
...
extensions/gii/generators/crud/default/views/index.php
View file @
8c8f3d0c
...
...
@@ -23,7 +23,7 @@ use <?= $generator->indexWidgetType === 'grid' ? "yii\\grid\\GridView" : "yii\\w
* @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;
?>
<div
class=
"
<?=
Inflector
::
camel2id
(
StringHelper
::
basename
(
$generator
->
modelClass
))
?>
-index"
>
...
...
@@ -33,7 +33,7 @@ $this->params['breadcrumbs'][] = $this->title;
<?=
"<?php "
.
(
$generator
->
indexWidgetType
===
'grid'
?
"// "
:
""
)
?>
echo $this->render('_search', ['model' => $searchModel]); ?>
<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>
<?php
if
(
$generator
->
indexWidgetType
===
'grid'
)
:
?>
...
...
extensions/gii/generators/crud/default/views/update.php
View file @
8c8f3d0c
...
...
@@ -20,10 +20,10 @@ use yii\helpers\Html;
* @var
<?=
ltrim
(
$generator
->
modelClass
,
'\\'
)
?>
$model
*/
$this->title =
'Update
<?=
Inflector
::
camel2words
(
StringHelper
::
basename
(
$generator
->
modelClass
))
?>
: '
. $model->
<?=
$generator
->
getNameAttribute
()
?>
;
$this->params['breadcrumbs'][] = ['label' =>
'
<?=
Inflector
::
pluralize
(
Inflector
::
camel2words
(
StringHelper
::
basename
(
$generator
->
modelClass
)))
?>
'
, 'url' => ['index']];
$this->title =
<?=
$generator
->
generateString
(
'Update {modelClass}: '
,
[
'modelClass'
=>
Inflector
::
camel2words
(
StringHelper
::
basename
(
$generator
->
modelClass
))])
?>
. $model->
<?=
$generator
->
getNameAttribute
()
?>
;
$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'][] =
'Update'
;
$this->params['breadcrumbs'][] =
<?=
$generator
->
generateString
(
'Update'
)
?>
;
?>
<div
class=
"
<?=
Inflector
::
camel2id
(
StringHelper
::
basename
(
$generator
->
modelClass
))
?>
-update"
>
...
...
extensions/gii/generators/crud/default/views/view.php
View file @
8c8f3d0c
...
...
@@ -22,7 +22,7 @@ use yii\widgets\DetailView;
*/
$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;
?>
<div
class=
"
<?=
Inflector
::
camel2id
(
StringHelper
::
basename
(
$generator
->
modelClass
))
?>
-view"
>
...
...
@@ -30,11 +30,11 @@ $this->params['breadcrumbs'][] = $this->title;
<h1>
<?=
"<?= "
?>
Html::encode($this->title) ?>
</h1>
<p>
<?=
"<?= "
?>
Html::a(
'Update'
, ['update',
<?=
$urlParams
?>
], ['class' => 'btn btn-primary']) ?>
<?=
"<?= "
?>
Html::a(
'Delete'
, ['delete',
<?=
$urlParams
?>
], [
<?=
"<?= "
?>
Html::a(
<?=
$generator
->
generateString
(
'Update'
)
?>
, ['update',
<?=
$urlParams
?>
], ['class' => 'btn btn-primary']) ?>
<?=
"<?= "
?>
Html::a(
<?=
$generator
->
generateString
(
'Delete'
)
?>
, ['delete',
<?=
$urlParams
?>
], [
'class' => 'btn btn-danger',
'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',
],
]) ?>
...
...
extensions/gii/generators/crud/form.php
View file @
8c8f3d0c
...
...
@@ -14,3 +14,4 @@ echo $form->field($generator, 'indexWidgetType')->dropDownList([
'grid'
=>
'GridView'
,
'list'
=>
'ListView'
,
]);
echo
$form
->
field
(
$generator
,
'translatable'
)
->
checkbox
();
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment