Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve translation #220

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions packages/mailer/Twig/TranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Draw\Component\Mailer\Twig;

use Symfony\Component\Translation\TranslatableMessage;
use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
Expand All @@ -19,10 +21,7 @@ public function getFilters(): array
];
}

/**
* @param string|string[] $messages
*/
public function trans(string|array $messages, array $arguments = [], ?string $domain = null, ?string $locale = null, ?int $count = null): ?string
public function trans(null|string|\Stringable|array|TranslatableInterface $messages, string|array $arguments = [], ?string $domain = null, ?string $locale = null, ?int $count = null): ?string
{
if (!\is_array($messages)) {
$messages = [$messages];
Expand All @@ -34,6 +33,20 @@ public function trans(string|array $messages, array $arguments = [], ?string $do

$result = reset($messages);
foreach ($messages as $message) {
if ($message instanceof TranslatableInterface) {
if ($message instanceof TranslatableMessage && '' === $message->getMessage()) {
return '';
}

$result = $message->trans($this->translator, $locale ?? (\is_string($arguments) ? $arguments : null));

if ($result != $message) {
return $result;
}

continue;
}

$result = $this->translator->trans($message, $arguments, $domain, $locale);
if ($result != $message) {
return $result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct(
->scalarNode('controller_class')->defaultValue('sonata.admin.controller.crud')->end()
->scalarNode('icon')->defaultNull()->end()
->scalarNode('label')->defaultNull()->end()
->scalarNode('translation_domain')->defaultValue('SonataAdminBundle')->end()
->scalarNode('show_in_dashboard')->defaultTrue()->end()
->enumNode('pager_type')->values(['default', 'simple'])->defaultValue('default')->end()
->end();
Expand Down Expand Up @@ -64,7 +65,7 @@ public static function configureFromConfiguration(Definition $definition, array
array_filter(
array_intersect_key(
$config,
array_flip(['group', 'icon', 'label', 'pager_type', 'show_in_dashboard'])
array_flip(['group', 'icon', 'label', 'pager_type', 'show_in_dashboard', 'translation_domain'])
) + ['manager_type' => 'orm', 'model_class' => $config['entity_class'], 'controller' => $config['controller_class']],
fn ($value) => null !== $value
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</div>
{% if link %}
<a href="{{ link }}" class="small-box-footer">
{{ link_label|trans({}, 'SonataAdminBundle') }} <i class="fas fa-arrow-circle-right" aria-hidden="true"></i>
{{ link_label|trans({}, translation_domain) }} <i class="fas fa-arrow-circle-right" aria-hidden="true"></i>
</a>
{% endif %}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
{% if filter_url %}
<a href="{{ filter_url }}" class="btn btn-sm btn-default">
<i class="fas fa-list" aria-hidden="true"></i>
List
{{ 'link_list'|trans({}, 'SonataAdminBundle') }}
</a>
{% endif %}
</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function getDefaultConfiguration(): array
'label' => 'config',
'pager_type' => 'default',
'show_in_dashboard' => true,
'translation_domain' => 'SonataAdminBundle',
],
],
'console' => [
Expand All @@ -47,6 +48,7 @@ public function getDefaultConfiguration(): array
'label' => 'Execution',
'pager_type' => 'simple',
'show_in_dashboard' => true,
'translation_domain' => 'SonataAdminBundle',
],
'commands' => [],
],
Expand All @@ -60,6 +62,7 @@ public function getDefaultConfiguration(): array
'label' => 'Migration',
'pager_type' => 'default',
'show_in_dashboard' => true,
'translation_domain' => 'SonataAdminBundle',
],
],
'messenger' => [
Expand All @@ -76,6 +79,7 @@ public function getDefaultConfiguration(): array
'label' => 'Message',
'pager_type' => 'simple',
'show_in_dashboard' => true,
'translation_domain' => 'SonataAdminBundle',
'enabled' => true,
],
],
Expand Down Expand Up @@ -110,6 +114,7 @@ public function getDefaultConfiguration(): array
'label' => 'User lock',
'pager_type' => 'simple',
'show_in_dashboard' => true,
'translation_domain' => 'SonataAdminBundle',
],
],
],
Expand Down
Loading