Skip to content

Commit a6cc010

Browse files
authored
Merge pull request #44 from cleverage/40
#40 Fix localisation issues
2 parents 2cd1286 + 04e3754 commit a6cc010

17 files changed

Lines changed: 105 additions & 21 deletions

config/services/controller.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ services:
1212
$context: '@EasyCorp\Bundle\EasyAdminBundle\Factory\AdminContextFactory'
1313
$logDirectory: '%kernel.logs_dir%'
1414
$processExecutionRepository: '@cleverage_ui_process.repository.process_execution'
15+
$intlFormatter: '@EasyCorp\Bundle\EasyAdminBundle\Intl\IntlFormatter'
16+
$translator: '@translator'
1517
tags:
1618
- { name: 'controller.service_arguments' }

config/services/twig.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ services:
2626
cleverage_ui_process.twig.log_level_extension_runtime:
2727
class: CleverAge\UiProcessBundle\Twig\Runtime\LogLevelExtensionRuntime
2828
public: false
29+
calls:
30+
- [setTranslator, ['@translator']]
2931
tags:
3032
- { name: 'twig.runtime' }
3133

@@ -56,4 +58,4 @@ services:
5658
class: CleverAge\UiProcessBundle\Twig\Components\BootstrapModal
5759
shared: false
5860
tags:
59-
- { name: 'twig.component', key: 'ui:BootstrapModal', template: '@CleverAgeUiProcess/components/BootstrapModal.html.twig' }
61+
- { name: 'twig.component', key: 'ui:BootstrapModal', template: '@CleverAgeUiProcess/components/BootstrapModal.html.twig' }

src/Controller/Admin/LogRecordCrudController.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function configureFields(string $pageName): iterable
5151
return [
5252
LogLevelField::new('level'),
5353
TextField::new('message')->setMaxLength(512),
54-
DateTimeField::new('createdAt')->setFormat('Y/M/dd H:mm:ss'),
54+
DateTimeField::new('createdAt')->setFormat('short', 'medium'),
5555
ContextField::new('context')
5656
->onlyOnDetail(),
5757
BooleanField::new('contextIsEmpty', 'Has context info ?')
@@ -87,9 +87,14 @@ public function configureFilters(Filters $filters): Filters
8787
$processList = array_map(fn (ProcessConfiguration $cfg) => $cfg->getCode(), $processList);
8888

8989
return $filters->add(
90-
LogProcessFilter::new('process', $processList, $id)
90+
LogProcessFilter::new('Process', $processList, $id)
9191
)->add(
92-
ChoiceFilter::new('level')->setChoices(array_combine(Level::NAMES, Level::VALUES))
92+
ChoiceFilter::new('level')
93+
->setTranslatableChoices(array_combine(
94+
Level::VALUES,
95+
array_map(fn ($value) => 'enum.log_level.'.strtolower((string) $value), Level::NAMES)
96+
))
97+
->setFormTypeOption('translation_domain', 'enums'),
9398
)->add('message')->add('context')->add('createdAt');
9499
}
95100
}

src/Controller/Admin/Process/ListAction.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CleverAge\UiProcessBundle\Controller\Admin\Process;
1515

1616
use CleverAge\UiProcessBundle\Manager\ProcessConfigurationsManager;
17+
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Intl\IntlFormatterInterface;
1718
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1819
use Symfony\Component\HttpFoundation\Response;
1920
use Symfony\Component\Routing\Attribute\Route;
@@ -23,12 +24,17 @@
2324
#[IsGranted('ROLE_USER')]
2425
class ListAction extends AbstractController
2526
{
27+
public function __construct(private readonly IntlFormatterInterface $intlFormatter)
28+
{
29+
}
30+
2631
public function __invoke(ProcessConfigurationsManager $processConfigurationsManager): Response
2732
{
2833
return $this->render(
2934
'@CleverAgeUiProcess/admin/process/list.html.twig',
3035
[
3136
'processes' => $processConfigurationsManager->getPublicProcesses(),
37+
'IntlFormatterService' => $this->intlFormatter,
3238
]
3339
);
3440
}

src/Controller/Admin/ProcessExecutionCrudController.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@
3232
use Symfony\Component\HttpFoundation\Response;
3333
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
3434
use Symfony\Component\Security\Http\Attribute\IsGranted;
35+
use Symfony\Contracts\Translation\TranslatorInterface;
3536

3637
#[IsGranted('ROLE_USER')]
3738
class ProcessExecutionCrudController extends AbstractCrudController
3839
{
3940
public function __construct(
4041
private readonly ProcessExecutionRepository $processExecutionRepository,
4142
private readonly string $logDirectory,
43+
private readonly TranslatorInterface $translator,
4244
) {
4345
}
4446

@@ -52,12 +54,12 @@ public function configureFields(string $pageName): iterable
5254
return [
5355
TextField::new('code'),
5456
EnumField::new('status'),
55-
DateTimeField::new('startDate')->setFormat('Y/M/dd H:mm:ss'),
56-
DateTimeField::new('endDate')->setFormat('Y/M/dd H:mm:ss'),
57+
DateTimeField::new('startDate')->setFormat('short', 'medium'),
58+
DateTimeField::new('endDate')->setFormat('short', 'medium'),
5759
TextField::new('source')->setTemplatePath('@CleverAgeUiProcess/admin/field/process_source.html.twig'),
5860
TextField::new('target')->setTemplatePath('@CleverAgeUiProcess/admin/field/process_target.html.twig'),
5961
TextField::new('duration')->formatValue(function ($value, ProcessExecution $entity) {
60-
return $entity->duration(); // returned format can be changed here
62+
return $entity->duration($this->translator->trans('%H hour(s) %I min(s) %S s')); // returned format can be changed here
6163
}),
6264
ArrayField::new('report')->setTemplatePath('@CleverAgeUiProcess/admin/field/report.html.twig'),
6365
ContextField::new('context'),

src/Entity/Enum/ProcessExecutionStatus.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,17 @@
1313

1414
namespace CleverAge\UiProcessBundle\Entity\Enum;
1515

16-
enum ProcessExecutionStatus: string
16+
use Symfony\Contracts\Translation\TranslatableInterface;
17+
use Symfony\Contracts\Translation\TranslatorInterface;
18+
19+
enum ProcessExecutionStatus: string implements TranslatableInterface
1720
{
1821
case Started = 'started';
1922
case Finish = 'finish';
2023
case Failed = 'failed';
24+
25+
public function trans(TranslatorInterface $translator, ?string $locale = null): string
26+
{
27+
return $translator->trans('enum.process_execution_status.'.$this->value, domain: 'enums', locale: $locale);
28+
}
2129
}

src/Entity/Enum/ProcessScheduleType.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@
1313

1414
namespace CleverAge\UiProcessBundle\Entity\Enum;
1515

16-
enum ProcessScheduleType: string
16+
use Symfony\Contracts\Translation\TranslatableInterface;
17+
use Symfony\Contracts\Translation\TranslatorInterface;
18+
19+
enum ProcessScheduleType: string implements TranslatableInterface
1720
{
1821
case CRON = 'cron';
1922
case EVERY = 'every';
23+
24+
public function trans(TranslatorInterface $translator, ?string $locale = null): string
25+
{
26+
return $translator->trans('enum.process_schedule_type.'.$this->value, domain: 'enums', locale: $locale);
27+
}
2028
}

src/Twig/Extension/LogLevelExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function getFunctions(): array
2323
{
2424
return [
2525
new TwigFunction('log_label', [LogLevelExtensionRuntime::class, 'getLabel']),
26+
new TwigFunction('log_translation', [LogLevelExtensionRuntime::class, 'getTranslation']),
2627
new TwigFunction('log_css_class', [LogLevelExtensionRuntime::class, 'getCssClass']),
2728
];
2829
}

src/Twig/Runtime/LogLevelExtensionRuntime.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,28 @@
1414
namespace CleverAge\UiProcessBundle\Twig\Runtime;
1515

1616
use Monolog\Level;
17+
use Symfony\Contracts\Translation\TranslatorInterface;
1718
use Twig\Extension\RuntimeExtensionInterface;
1819

1920
class LogLevelExtensionRuntime implements RuntimeExtensionInterface
2021
{
22+
private TranslatorInterface $translator;
23+
24+
public function setTranslator(TranslatorInterface $translator): void
25+
{
26+
$this->translator = $translator;
27+
}
28+
2129
public function getLabel(int $value): string
2230
{
2331
return Level::from($value)->getName();
2432
}
2533

34+
public function getTranslation(string $key): string
35+
{
36+
return $this->translator->trans('enum.log_level.'.strtolower($key), domain: 'enums');
37+
}
38+
2639
public function getCssClass(string|int $value): string
2740
{
2841
return \is_int($value) ?
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
{% set class = field.value.value == 'failed' ? 'danger' : 'success' %}
2-
<span class="badge badge-{{ class }}">{{ field.value.value }}</span>
2+
<span class="badge badge-{{ class }}">{{ field.value|trans() }}</span>

0 commit comments

Comments
 (0)