diff --git a/src/Command/ControllerCommand.php b/src/Command/ControllerCommand.php index c78bc5df..faa19f2c 100644 --- a/src/Command/ControllerCommand.php +++ b/src/Command/ControllerCommand.php @@ -112,14 +112,15 @@ public function bake(string $controllerName, Arguments $args, ConsoleIo $io): vo $currentModelName = $controllerName; $plugin = $this->plugin; - if ($plugin) { - $plugin .= '.'; + $pluginPath = $plugin; + if ($pluginPath) { + $pluginPath .= '.'; } - if ($this->getTableLocator()->exists($plugin . $currentModelName)) { - $modelObj = $this->getTableLocator()->get($plugin . $currentModelName); + if ($this->getTableLocator()->exists($pluginPath . $currentModelName)) { + $modelObj = $this->getTableLocator()->get($pluginPath . $currentModelName); } else { - $modelObj = $this->getTableLocator()->get($plugin . $currentModelName, [ + $modelObj = $this->getTableLocator()->get($pluginPath . $currentModelName, [ 'connectionName' => $this->connection, ]); } diff --git a/src/Command/TemplateCommand.php b/src/Command/TemplateCommand.php index f467c1aa..8edd388a 100644 --- a/src/Command/TemplateCommand.php +++ b/src/Command/TemplateCommand.php @@ -408,6 +408,10 @@ public function getContent(Arguments $args, ConsoleIo $io, string $action, ?arra } $renderer->set('indexColumns', $indexColumns); + // Always use domain translations when in plugin context + $useDomain = (bool)$this->plugin; + $renderer->set('useDomain', $useDomain); + return $renderer->generate("Bake.Template/$action"); } diff --git a/templates/bake/Template/index.twig b/templates/bake/Template/index.twig index 4a81b76f..bac6863b 100644 --- a/templates/bake/Template/index.twig +++ b/templates/bake/Template/index.twig @@ -21,9 +21,9 @@ ?>
{% set fields = Bake.filterFields(fields, schema, modelObject, indexColumns, ['binary', 'text']) %} - Html->link(__('New {{ singularHumanName }}'), ['action' => 'add'], ['class' => 'button float-right']) ?> + Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'New {{ singularHumanName }}'), ['action' => 'add'], ['class' => 'button float-right']) ?> {% set done = [] %} -

+

@@ -31,7 +31,7 @@ {% for field in fields %} {% endfor %} - + @@ -63,14 +63,14 @@ {% endfor %} {% set pk = '$' ~ singularVar ~ '->' ~ primaryKey[0] %} @@ -81,12 +81,12 @@
    - Paginator->first('<< ' . __('first')) ?> - Paginator->prev('< ' . __('previous')) ?> + Paginator->first('<< ' . {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'first')) ?> + Paginator->prev('< ' . {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'previous')) ?> Paginator->numbers() ?> - Paginator->next(__('next') . ' >') ?> - Paginator->last(__('last') . ' >>') ?> + Paginator->next({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'next') . ' >') ?> + Paginator->last({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'last') . ' >>') ?>
-

Paginator->counter(__('Page {{ '{{' }}page{{ '}}' }} of {{ '{{' }}pages{{ '}}' }}, showing {{ '{{' }}current{{ '}}' }} record(s) out of {{ '{{' }}count{{ '}}' }} total')) ?>

+

Paginator->counter({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Page {{ '{{' }}page{{ '}}' }} of {{ '{{' }}pages{{ '}}' }}, showing {{ '{{' }}current{{ '}}' }} record(s) out of {{ '{{' }}count{{ '}}' }} total')) ?>

\ No newline at end of file diff --git a/templates/bake/Template/login.twig b/templates/bake/Template/login.twig index 77f14d1c..8f50536c 100644 --- a/templates/bake/Template/login.twig +++ b/templates/bake/Template/login.twig @@ -21,10 +21,10 @@
Form->create() ?>
- + Form->control('username') ?> Form->control('password') ?>
- Form->button(__('Login')); ?> + Form->button({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Login')); ?> Form->end() ?>
diff --git a/templates/bake/Template/view.twig b/templates/bake/Template/view.twig index e4234c0e..28f5905f 100644 --- a/templates/bake/Template/view.twig +++ b/templates/bake/Template/view.twig @@ -28,11 +28,12 @@
@@ -44,12 +45,12 @@ {%~ if associationFields[field] is defined %} {%~ set details = associationFields[field] %}
- + {%~ else %} - + {%~ endif %} @@ -58,7 +59,7 @@ {% if associations.HasOne %} {%~ for alias, details in associations.HasOne %} - + {%~ endfor %} @@ -66,9 +67,9 @@ {% if groupedFields.number %} {%~ for field in groupedFields.number %} - - {%~ set columnData = Bake.columnData(field, schema) %} - {%~ if columnData.null %} + +{% set columnData = Bake.columnData(field, schema) %} +{% if columnData.null %} {%~ else %} @@ -79,7 +80,7 @@ {% if groupedFields.enum %} {%~ for field in groupedFields.enum %} - + {%~ set columnData = Bake.columnData(field, schema) %} {%~ set supportsLabel = Bake.enumSupportsLabel(field, schema) %} {%~ if columnData.null %} @@ -93,7 +94,7 @@ {% if groupedFields.date %} {%~ for field in groupedFields.date %} - + {%~ endfor %} @@ -101,8 +102,8 @@ {% if groupedFields.boolean %} {%~ for field in groupedFields.boolean %} - - + + {%~ endfor %} {% endif %} @@ -110,7 +111,7 @@ {% if groupedFields.text %} {%~ for field in groupedFields.text %}
- +
Text->autoParagraph(h(${{ singularVar }}->{{ field }})); ?>
@@ -122,15 +123,15 @@ {%~ set otherSingularVar = alias|singularize|variable %} {%~ set otherPluralHumanName = details.controller|underscore|humanize %}
Paginator->sort('{{ field }}') ?>
- Html->link(__('View'), ['action' => 'view', {{ pk|raw }}]) ?> - Html->link(__('Edit'), ['action' => 'edit', {{ pk|raw }}]) ?> + Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'View'), ['action' => 'view', {{ pk|raw }}]) ?> + Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Edit'), ['action' => 'edit', {{ pk|raw }}]) ?> Form->postLink( - __('Delete'), + {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Delete'), ['action' => 'delete', {{ pk|raw }}], [ 'method' => 'delete', - 'confirm' => __('Are you sure you want to delete # {0}?', {{ pk|raw }}), + 'confirm' => {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Are you sure you want to delete # {0}?', {{ pk|raw }}), ] ) ?>
hasValue('{{ details.property }}') ? $this->Html->link(${{ singularVar }}->{{ details.property }}->{{ details.displayField }}, ['controller' => '{{ details.controller }}', 'action' => 'view', ${{ singularVar }}->{{ details.property }}->{{ details.primaryKey[0] }}]) : '' ?>
{{ field }}) ?>
hasValue('{{ details.property }}') ? $this->Html->link(${{ singularVar }}->{{ details.property }}->{{ details.displayField }}, ['controller' => '{{ details.controller }}', 'action' => 'view', ${{ singularVar }}->{{ details.property }}->{{ details.primaryKey[0] }}]) : '' ?>
{{ field }} === null ? '' : $this->Number->format(${{ singularVar }}->{{ field }}) ?>Number->format(${{ singularVar }}->{{ field }}) ?>
{{ field }}) ?>
{{ field }} ? __('Yes') : __('No'); ?>{{ field }} ? {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Yes') : {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'No'); ?>
{%~ for field in details.fields %} - + {%~ endfor %} - + {{ details.property }} as ${{ otherSingularVar }}) : ?> @@ -139,14 +140,14 @@ {%~ endfor %} {%~ set otherPk = '$' ~ otherSingularVar ~ '->' ~ details.primaryKey[0] %} diff --git a/templates/bake/element/Controller/add.twig b/templates/bake/element/Controller/add.twig index 593ae4d4..73c9b83d 100644 --- a/templates/bake/element/Controller/add.twig +++ b/templates/bake/element/Controller/add.twig @@ -28,11 +28,11 @@ if ($this->request->is('post')) { ${{ singularName }} = $this->{{ currentModelName }}->patchEntity(${{ singularName }}, $this->request->getData()); if ($this->{{ currentModelName }}->save(${{ singularName }})) { - $this->Flash->success(__('The {{ singularHumanName|lower }} has been saved.')); + $this->Flash->success({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} has been saved.')); return $this->redirect(['action' => 'index']); } - $this->Flash->error(__('The {{ singularHumanName|lower }} could not be saved. Please, try again.')); + $this->Flash->error({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} could not be saved. Please, try again.')); } {% set associations = Bake.aliasExtractor(modelObj, 'BelongsTo') %} {% set associations = associations|merge(Bake.aliasExtractor(modelObj, 'BelongsToMany')) %} diff --git a/templates/bake/element/Controller/delete.twig b/templates/bake/element/Controller/delete.twig index bab93c72..70be262f 100644 --- a/templates/bake/element/Controller/delete.twig +++ b/templates/bake/element/Controller/delete.twig @@ -28,9 +28,9 @@ $this->Authorization->authorize(${{ singularName }}); {% endif %} if ($this->{{ currentModelName }}->delete(${{ singularName }})) { - $this->Flash->success(__('The {{ singularHumanName|lower }} has been deleted.')); + $this->Flash->success({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} has been deleted.')); } else { - $this->Flash->error(__('The {{ singularHumanName|lower }} could not be deleted. Please, try again.')); + $this->Flash->error({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} could not be deleted. Please, try again.')); } return $this->redirect(['action' => 'index']); diff --git a/templates/bake/element/Controller/edit.twig b/templates/bake/element/Controller/edit.twig index 4e0b97d9..564b8033 100644 --- a/templates/bake/element/Controller/edit.twig +++ b/templates/bake/element/Controller/edit.twig @@ -32,11 +32,11 @@ if ($this->request->is(['patch', 'post', 'put'])) { ${{ singularName }} = $this->{{ currentModelName }}->patchEntity(${{ singularName }}, $this->request->getData()); if ($this->{{ currentModelName }}->save(${{ singularName }})) { - $this->Flash->success(__('The {{ singularHumanName|lower }} has been saved.')); + $this->Flash->success({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} has been saved.')); return $this->redirect(['action' => 'index']); } - $this->Flash->error(__('The {{ singularHumanName|lower }} could not be saved. Please, try again.')); + $this->Flash->error({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} could not be saved. Please, try again.')); } {% for assoc in belongsTo|merge(belongsToMany) %} {%~ set otherName = Bake.getAssociatedTableAlias(modelObj, assoc) %} diff --git a/templates/bake/element/Controller/login.twig b/templates/bake/element/Controller/login.twig index 409f0243..84e11624 100644 --- a/templates/bake/element/Controller/login.twig +++ b/templates/bake/element/Controller/login.twig @@ -27,7 +27,7 @@ $this->request->allowMethod(['get', 'post']); $result = $this->Authentication->getResult(); if ($result->isValid()) { - $this->Flash->success(__('Login successful')); + $this->Flash->success({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'Login successful')); $redirect = $this->Authentication->getLoginRedirect(); if ($redirect) { return $this->redirect($redirect); @@ -36,6 +36,6 @@ // Display error if user submitted and authentication failed if ($this->request->is('post')) { - $this->Flash->error(__('Invalid username or password')); + $this->Flash->error({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'Invalid username or password')); } } diff --git a/tests/TestCase/Command/TemplateCommandTest.php b/tests/TestCase/Command/TemplateCommandTest.php index e730bf0d..15f9c28c 100644 --- a/tests/TestCase/Command/TemplateCommandTest.php +++ b/tests/TestCase/Command/TemplateCommandTest.php @@ -659,6 +659,28 @@ public function testBakeIndexPlugin() $this->assertFileContains('$comment->article->id', $this->generatedFile); } + /** + * test Bake with plugins with __d() translations. + * + * @return void + */ + public function testBakePluginTemplatesWithDomain() + { + $this->_loadTestPlugin('BakeTest'); + $path = Plugin::templatePath('BakeTest'); + + // Setup association to ensure properties don't have dots + $model = $this->getTableLocator()->get('BakeTest.Comments'); + $model->belongsTo('Articles'); + + $this->generatedFile = $path . 'Comments/index.php'; + $this->exec('bake template BakeTest.comments index'); + + $this->assertExitCode(CommandInterface::CODE_SUCCESS); + $this->assertFileExists($this->generatedFile); + $this->assertFileContains('__d(\'BakeTest\', ', $this->generatedFile); + } + /** * Ensure that models in a tree don't include form fields for lft/rght * diff --git a/tests/comparisons/Controller/testBakeWithPlugin.php b/tests/comparisons/Controller/testBakeWithPlugin.php index fb16c6e7..a3f09154 100644 --- a/tests/comparisons/Controller/testBakeWithPlugin.php +++ b/tests/comparisons/Controller/testBakeWithPlugin.php @@ -50,11 +50,11 @@ public function add() if ($this->request->is('post')) { $bakeArticle = $this->BakeArticles->patchEntity($bakeArticle, $this->request->getData()); if ($this->BakeArticles->save($bakeArticle)) { - $this->Flash->success(__('The bake article has been saved.')); + $this->Flash->success(__d('BakeTest', 'The bake article has been saved.')); return $this->redirect(['action' => 'index']); } - $this->Flash->error(__('The bake article could not be saved. Please, try again.')); + $this->Flash->error(__d('BakeTest', 'The bake article could not be saved. Please, try again.')); } $bakeUsers = $this->BakeArticles->BakeUsers->find('list', limit: 200)->all(); $bakeTags = $this->BakeArticles->BakeTags->find('list', limit: 200)->all(); @@ -74,11 +74,11 @@ public function edit($id = null) if ($this->request->is(['patch', 'post', 'put'])) { $bakeArticle = $this->BakeArticles->patchEntity($bakeArticle, $this->request->getData()); if ($this->BakeArticles->save($bakeArticle)) { - $this->Flash->success(__('The bake article has been saved.')); + $this->Flash->success(__d('BakeTest', 'The bake article has been saved.')); return $this->redirect(['action' => 'index']); } - $this->Flash->error(__('The bake article could not be saved. Please, try again.')); + $this->Flash->error(__d('BakeTest', 'The bake article could not be saved. Please, try again.')); } $bakeUsers = $this->BakeArticles->BakeUsers->find('list', limit: 200)->all(); $bakeTags = $this->BakeArticles->BakeTags->find('list', limit: 200)->all(); @@ -97,9 +97,9 @@ public function delete($id = null) $this->request->allowMethod(['post', 'delete']); $bakeArticle = $this->BakeArticles->get($id); if ($this->BakeArticles->delete($bakeArticle)) { - $this->Flash->success(__('The bake article has been deleted.')); + $this->Flash->success(__d('BakeTest', 'The bake article has been deleted.')); } else { - $this->Flash->error(__('The bake article could not be deleted. Please, try again.')); + $this->Flash->error(__d('BakeTest', 'The bake article could not be deleted. Please, try again.')); } return $this->redirect(['action' => 'index']);
- Html->link(__('View'), ['controller' => '{{ details.controller }}', 'action' => 'view', {{ otherPk|raw }}]) ?> - Html->link(__('Edit'), ['controller' => '{{ details.controller }}', 'action' => 'edit', {{ otherPk|raw }}]) ?> + Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'View'), ['controller' => '{{ details.controller }}', 'action' => 'view', {{ otherPk|raw }}]) ?> + Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Edit'), ['controller' => '{{ details.controller }}', 'action' => 'edit', {{ otherPk|raw }}]) ?> Form->postLink( - __('Delete'), + {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Delete'), ['controller' => '{{ details.controller }}', 'action' => 'delete', {{ otherPk|raw }}], [ 'method' => 'delete', - 'confirm' => __('Are you sure you want to delete # {0}?', {{ otherPk|raw }}), + 'confirm' => {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Are you sure you want to delete # {0}?', {{ otherPk|raw }}), ] ) ?>