Skip to content

Commit 02a43ee

Browse files
authored
Merge pull request #1039 from cakephp/3.next-plugin-domain
Allow using __d() in plugin context.
2 parents 47cae5a + 95b5cda commit 02a43ee

File tree

11 files changed

+84
-56
lines changed

11 files changed

+84
-56
lines changed

src/Command/ControllerCommand.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,15 @@ public function bake(string $controllerName, Arguments $args, ConsoleIo $io): vo
112112

113113
$currentModelName = $controllerName;
114114
$plugin = $this->plugin;
115-
if ($plugin) {
116-
$plugin .= '.';
115+
$pluginPath = $plugin;
116+
if ($pluginPath) {
117+
$pluginPath .= '.';
117118
}
118119

119-
if ($this->getTableLocator()->exists($plugin . $currentModelName)) {
120-
$modelObj = $this->getTableLocator()->get($plugin . $currentModelName);
120+
if ($this->getTableLocator()->exists($pluginPath . $currentModelName)) {
121+
$modelObj = $this->getTableLocator()->get($pluginPath . $currentModelName);
121122
} else {
122-
$modelObj = $this->getTableLocator()->get($plugin . $currentModelName, [
123+
$modelObj = $this->getTableLocator()->get($pluginPath . $currentModelName, [
123124
'connectionName' => $this->connection,
124125
]);
125126
}

src/Command/TemplateCommand.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,10 @@ public function getContent(Arguments $args, ConsoleIo $io, string $action, ?arra
408408
}
409409
$renderer->set('indexColumns', $indexColumns);
410410

411+
// Always use domain translations when in plugin context
412+
$useDomain = (bool)$this->plugin;
413+
$renderer->set('useDomain', $useDomain);
414+
411415
return $renderer->generate("Bake.Template/$action");
412416
}
413417

templates/bake/Template/index.twig

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@
2121
?>
2222
<div class="{{ pluralVar }} index content">
2323
{% set fields = Bake.filterFields(fields, schema, modelObject, indexColumns, ['binary', 'text']) %}
24-
<?= $this->Html->link(__('New {{ singularHumanName }}'), ['action' => 'add'], ['class' => 'button float-right']) ?>
24+
<?= $this->Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'New {{ singularHumanName }}'), ['action' => 'add'], ['class' => 'button float-right']) ?>
2525
{% set done = [] %}
26-
<h3><?= __('{{ pluralHumanName }}') ?></h3>
26+
<h3><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ pluralHumanName }}') ?></h3>
2727
<div class="table-responsive">
2828
<table>
2929
<thead>
3030
<tr>
3131
{% for field in fields %}
3232
<th><?= $this->Paginator->sort('{{ field }}') ?></th>
3333
{% endfor %}
34-
<th class="actions"><?= __('Actions') ?></th>
34+
<th class="actions"><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Actions') ?></th>
3535
</tr>
3636
</thead>
3737
<tbody>
@@ -63,14 +63,14 @@
6363
{% endfor %}
6464
{% set pk = '$' ~ singularVar ~ '->' ~ primaryKey[0] %}
6565
<td class="actions">
66-
<?= $this->Html->link(__('View'), ['action' => 'view', {{ pk|raw }}]) ?>
67-
<?= $this->Html->link(__('Edit'), ['action' => 'edit', {{ pk|raw }}]) ?>
66+
<?= $this->Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'View'), ['action' => 'view', {{ pk|raw }}]) ?>
67+
<?= $this->Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Edit'), ['action' => 'edit', {{ pk|raw }}]) ?>
6868
<?= $this->Form->postLink(
69-
__('Delete'),
69+
{% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Delete'),
7070
['action' => 'delete', {{ pk|raw }}],
7171
[
7272
'method' => 'delete',
73-
'confirm' => __('Are you sure you want to delete # {0}?', {{ pk|raw }}),
73+
'confirm' => {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Are you sure you want to delete # {0}?', {{ pk|raw }}),
7474
]
7575
) ?>
7676
</td>
@@ -81,12 +81,12 @@
8181
</div>
8282
<div class="paginator">
8383
<ul class="pagination">
84-
<?= $this->Paginator->first('<< ' . __('first')) ?>
85-
<?= $this->Paginator->prev('< ' . __('previous')) ?>
84+
<?= $this->Paginator->first('<< ' . {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'first')) ?>
85+
<?= $this->Paginator->prev('< ' . {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'previous')) ?>
8686
<?= $this->Paginator->numbers() ?>
87-
<?= $this->Paginator->next(__('next') . ' >') ?>
88-
<?= $this->Paginator->last(__('last') . ' >>') ?>
87+
<?= $this->Paginator->next({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'next') . ' >') ?>
88+
<?= $this->Paginator->last({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'last') . ' >>') ?>
8989
</ul>
90-
<p><?= $this->Paginator->counter(__('Page {{ '{{' }}page{{ '}}' }} of {{ '{{' }}pages{{ '}}' }}, showing {{ '{{' }}current{{ '}}' }} record(s) out of {{ '{{' }}count{{ '}}' }} total')) ?></p>
90+
<p><?= $this->Paginator->counter({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Page {{ '{{' }}page{{ '}}' }} of {{ '{{' }}pages{{ '}}' }}, showing {{ '{{' }}current{{ '}}' }} record(s) out of {{ '{{' }}count{{ '}}' }} total')) ?></p>
9191
</div>
9292
</div>

templates/bake/Template/login.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
<div class="{{ pluralVar }} form content">
2222
<?= $this->Form->create() ?>
2323
<fieldset>
24-
<legend><?= __('Please enter your username and password') ?></legend>
24+
<legend><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Please enter your username and password') ?></legend>
2525
<?= $this->Form->control('username') ?>
2626
<?= $this->Form->control('password') ?>
2727
</fieldset>
28-
<?= $this->Form->button(__('Login')); ?>
28+
<?= $this->Form->button({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Login')); ?>
2929
<?= $this->Form->end() ?>
3030
</div>

templates/bake/Template/view.twig

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
<div class="row">
2929
<aside class="column">
3030
<div class="side-nav">
31-
<h4 class="heading"><?= __('Actions') ?></h4>
32-
<?= $this->Html->link(__('Edit {{ singularHumanName }}'), ['action' => 'edit', {{ pK|raw }}], ['class' => 'side-nav-item']) ?>
33-
<?= $this->Form->postLink(__('Delete {{ singularHumanName }}'), ['action' => 'delete', {{ pK|raw }}], ['confirm' => __('Are you sure you want to delete # {0}?', {{ pK|raw }}), 'class' => 'side-nav-item']) ?>
34-
<?= $this->Html->link(__('List {{ pluralHumanName }}'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
35-
<?= $this->Html->link(__('New {{ singularHumanName }}'), ['action' => 'add'], ['class' => 'side-nav-item']) ?>
31+
<h4 class="heading"><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Actions') ?></h4>
32+
<?= $this->Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Edit {{ singularHumanName }}'), ['action' => 'edit', {{ pK|raw }}], ['class' => 'side-nav-item']) ?>
33+
<?= $this->Form->postLink({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Delete {{ singularHumanName }}'), ['action' => 'delete', {{ pK|raw }}], ['confirm' => {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Are you sure you want to delete # {0}?', {{ pK|raw }}), 'class' => 'side-nav-item']) ?>
34+
<?= $this->Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'List {{ pluralHumanName }}'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
35+
<?= $this->Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'New {{ singularHumanName }}'), ['action' => 'add'], ['class' => 'side-nav-item']) ?>
36+
{% set done = [] %}
3637
</div>
3738
</aside>
3839
<div class="column column-80">
@@ -44,12 +45,12 @@
4445
{%~ if associationFields[field] is defined %}
4546
{%~ set details = associationFields[field] %}
4647
<tr>
47-
<th><?= __('{{ details.property|humanize }}') ?></th>
48+
<th><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ details.property|humanize }}') ?></th>
4849
<td><?= ${{ singularVar }}->hasValue('{{ details.property }}') ? $this->Html->link(${{ singularVar }}->{{ details.property }}->{{ details.displayField }}, ['controller' => '{{ details.controller }}', 'action' => 'view', ${{ singularVar }}->{{ details.property }}->{{ details.primaryKey[0] }}]) : '' ?></td>
4950
</tr>
5051
{%~ else %}
5152
<tr>
52-
<th><?= __('{{ field|humanize }}') ?></th>
53+
<th><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ field|humanize }}') ?></th>
5354
<td><?= h(${{ singularVar }}->{{ field }}) ?></td>
5455
</tr>
5556
{%~ endif %}
@@ -58,17 +59,17 @@
5859
{% if associations.HasOne %}
5960
{%~ for alias, details in associations.HasOne %}
6061
<tr>
61-
<th><?= __('{{ alias|underscore|singularize|humanize }}') ?></th>
62+
<th><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ alias|underscore|singularize|humanize }}') ?></th>
6263
<td><?= ${{ singularVar }}->hasValue('{{ details.property }}') ? $this->Html->link(${{ singularVar }}->{{ details.property }}->{{ details.displayField }}, ['controller' => '{{ details.controller }}', 'action' => 'view', ${{ singularVar }}->{{ details.property }}->{{ details.primaryKey[0] }}]) : '' ?></td>
6364
</tr>
6465
{%~ endfor %}
6566
{% endif %}
6667
{% if groupedFields.number %}
6768
{%~ for field in groupedFields.number %}
6869
<tr>
69-
<th><?= __('{{ field|humanize }}') ?></th>
70-
{%~ set columnData = Bake.columnData(field, schema) %}
71-
{%~ if columnData.null %}
70+
<th><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ field|humanize }}') ?></th>
71+
{% set columnData = Bake.columnData(field, schema) %}
72+
{% if columnData.null %}
7273
<td><?= ${{ singularVar }}->{{ field }} === null ? '' : $this->Number->format(${{ singularVar }}->{{ field }}) ?></td>
7374
{%~ else %}
7475
<td><?= $this->Number->format(${{ singularVar }}->{{ field }}) ?></td>
@@ -79,7 +80,7 @@
7980
{% if groupedFields.enum %}
8081
{%~ for field in groupedFields.enum %}
8182
<tr>
82-
<th><?= __('{{ field|humanize }}') ?></th>
83+
<th><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ field|humanize }}') ?></th>
8384
{%~ set columnData = Bake.columnData(field, schema) %}
8485
{%~ set supportsLabel = Bake.enumSupportsLabel(field, schema) %}
8586
{%~ if columnData.null %}
@@ -93,24 +94,24 @@
9394
{% if groupedFields.date %}
9495
{%~ for field in groupedFields.date %}
9596
<tr>
96-
<th><?= __('{{ field|humanize }}') ?></th>
97+
<th><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ field|humanize }}') ?></th>
9798
<td><?= h(${{ singularVar }}->{{ field }}) ?></td>
9899
</tr>
99100
{%~ endfor %}
100101
{% endif %}
101102
{% if groupedFields.boolean %}
102103
{%~ for field in groupedFields.boolean %}
103104
<tr>
104-
<th><?= __('{{ field|humanize }}') ?></th>
105-
<td><?= ${{ singularVar }}->{{ field }} ? __('Yes') : __('No'); ?></td>
105+
<th><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ field|humanize }}') ?></th>
106+
<td><?= ${{ singularVar }}->{{ field }} ? {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Yes') : {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'No'); ?></td>
106107
</tr>
107108
{%~ endfor %}
108109
{% endif %}
109110
</table>
110111
{% if groupedFields.text %}
111112
{%~ for field in groupedFields.text %}
112113
<div class="text">
113-
<strong><?= __('{{ field|humanize }}') ?></strong>
114+
<strong><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ field|humanize }}') ?></strong>
114115
<blockquote>
115116
<?= $this->Text->autoParagraph(h(${{ singularVar }}->{{ field }})); ?>
116117
</blockquote>
@@ -122,15 +123,15 @@
122123
{%~ set otherSingularVar = alias|singularize|variable %}
123124
{%~ set otherPluralHumanName = details.controller|underscore|humanize %}
124125
<div class="related">
125-
<h4><?= __('Related {{ otherPluralHumanName }}') ?></h4>
126+
<h4><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Related {{ otherPluralHumanName }}') ?></h4>
126127
<?php if (!empty(${{ singularVar }}->{{ details.property }})) : ?>
127128
<div class="table-responsive">
128129
<table>
129130
<tr>
130131
{%~ for field in details.fields %}
131-
<th><?= __('{{ field|humanize }}') ?></th>
132+
<th><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ field|humanize }}') ?></th>
132133
{%~ endfor %}
133-
<th class="actions"><?= __('Actions') ?></th>
134+
<th class="actions"><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Actions') ?></th>
134135
</tr>
135136
<?php foreach (${{ singularVar }}->{{ details.property }} as ${{ otherSingularVar }}) : ?>
136137
<tr>
@@ -139,14 +140,14 @@
139140
{%~ endfor %}
140141
{%~ set otherPk = '$' ~ otherSingularVar ~ '->' ~ details.primaryKey[0] %}
141142
<td class="actions">
142-
<?= $this->Html->link(__('View'), ['controller' => '{{ details.controller }}', 'action' => 'view', {{ otherPk|raw }}]) ?>
143-
<?= $this->Html->link(__('Edit'), ['controller' => '{{ details.controller }}', 'action' => 'edit', {{ otherPk|raw }}]) ?>
143+
<?= $this->Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'View'), ['controller' => '{{ details.controller }}', 'action' => 'view', {{ otherPk|raw }}]) ?>
144+
<?= $this->Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Edit'), ['controller' => '{{ details.controller }}', 'action' => 'edit', {{ otherPk|raw }}]) ?>
144145
<?= $this->Form->postLink(
145-
__('Delete'),
146+
{% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Delete'),
146147
['controller' => '{{ details.controller }}', 'action' => 'delete', {{ otherPk|raw }}],
147148
[
148149
'method' => 'delete',
149-
'confirm' => __('Are you sure you want to delete # {0}?', {{ otherPk|raw }}),
150+
'confirm' => {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Are you sure you want to delete # {0}?', {{ otherPk|raw }}),
150151
]
151152
) ?>
152153
</td>

templates/bake/element/Controller/add.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
if ($this->request->is('post')) {
2929
${{ singularName }} = $this->{{ currentModelName }}->patchEntity(${{ singularName }}, $this->request->getData());
3030
if ($this->{{ currentModelName }}->save(${{ singularName }})) {
31-
$this->Flash->success(__('The {{ singularHumanName|lower }} has been saved.'));
31+
$this->Flash->success({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} has been saved.'));
3232

3333
return $this->redirect(['action' => 'index']);
3434
}
35-
$this->Flash->error(__('The {{ singularHumanName|lower }} could not be saved. Please, try again.'));
35+
$this->Flash->error({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} could not be saved. Please, try again.'));
3636
}
3737
{% set associations = Bake.aliasExtractor(modelObj, 'BelongsTo') %}
3838
{% set associations = associations|merge(Bake.aliasExtractor(modelObj, 'BelongsToMany')) %}

templates/bake/element/Controller/delete.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
$this->Authorization->authorize(${{ singularName }});
2929
{% endif %}
3030
if ($this->{{ currentModelName }}->delete(${{ singularName }})) {
31-
$this->Flash->success(__('The {{ singularHumanName|lower }} has been deleted.'));
31+
$this->Flash->success({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} has been deleted.'));
3232
} else {
33-
$this->Flash->error(__('The {{ singularHumanName|lower }} could not be deleted. Please, try again.'));
33+
$this->Flash->error({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} could not be deleted. Please, try again.'));
3434
}
3535

3636
return $this->redirect(['action' => 'index']);

templates/bake/element/Controller/edit.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
if ($this->request->is(['patch', 'post', 'put'])) {
3333
${{ singularName }} = $this->{{ currentModelName }}->patchEntity(${{ singularName }}, $this->request->getData());
3434
if ($this->{{ currentModelName }}->save(${{ singularName }})) {
35-
$this->Flash->success(__('The {{ singularHumanName|lower }} has been saved.'));
35+
$this->Flash->success({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} has been saved.'));
3636

3737
return $this->redirect(['action' => 'index']);
3838
}
39-
$this->Flash->error(__('The {{ singularHumanName|lower }} could not be saved. Please, try again.'));
39+
$this->Flash->error({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} could not be saved. Please, try again.'));
4040
}
4141
{% for assoc in belongsTo|merge(belongsToMany) %}
4242
{%~ set otherName = Bake.getAssociatedTableAlias(modelObj, assoc) %}

templates/bake/element/Controller/login.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
$this->request->allowMethod(['get', 'post']);
2828
$result = $this->Authentication->getResult();
2929
if ($result->isValid()) {
30-
$this->Flash->success(__('Login successful'));
30+
$this->Flash->success({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'Login successful'));
3131
$redirect = $this->Authentication->getLoginRedirect();
3232
if ($redirect) {
3333
return $this->redirect($redirect);
@@ -36,6 +36,6 @@
3636

3737
// Display error if user submitted and authentication failed
3838
if ($this->request->is('post')) {
39-
$this->Flash->error(__('Invalid username or password'));
39+
$this->Flash->error({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'Invalid username or password'));
4040
}
4141
}

tests/TestCase/Command/TemplateCommandTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,28 @@ public function testBakeIndexPlugin()
659659
$this->assertFileContains('$comment->article->id', $this->generatedFile);
660660
}
661661

662+
/**
663+
* test Bake with plugins with __d() translations.
664+
*
665+
* @return void
666+
*/
667+
public function testBakePluginTemplatesWithDomain()
668+
{
669+
$this->_loadTestPlugin('BakeTest');
670+
$path = Plugin::templatePath('BakeTest');
671+
672+
// Setup association to ensure properties don't have dots
673+
$model = $this->getTableLocator()->get('BakeTest.Comments');
674+
$model->belongsTo('Articles');
675+
676+
$this->generatedFile = $path . 'Comments/index.php';
677+
$this->exec('bake template BakeTest.comments index');
678+
679+
$this->assertExitCode(CommandInterface::CODE_SUCCESS);
680+
$this->assertFileExists($this->generatedFile);
681+
$this->assertFileContains('__d(\'BakeTest\', ', $this->generatedFile);
682+
}
683+
662684
/**
663685
* Ensure that models in a tree don't include form fields for lft/rght
664686
*

0 commit comments

Comments
 (0)