Skip to content

Commit

Permalink
BC layer for old translator contract
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreyu committed Nov 24, 2024
1 parent 7939d44 commit 442e9bb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/Column/Type/ColumnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ public function buildExportHeaderView(ColumnHeaderView $view, ColumnInterface $c

if ($this->translator) {
if ($label instanceof TranslatableInterface) {
$label = $label->trans($this->translator, $this->translator->getLocale());
$locale = null;

if (method_exists(TranslatableInterface::class, 'getLocale')) {
$locale = $this->translator->getLocale();
}

$label = $label->trans($this->translator, $locale);
} else {
$translationDomain = $options['export']['header_translation_domain'];
$translationParameters = $options['export']['header_translation_parameters'];
Expand Down Expand Up @@ -161,7 +167,13 @@ public function buildExportValueView(ColumnValueView $view, ColumnInterface $col

if ($this->translator && (is_string($viewData) || $viewData instanceof TranslatableInterface)) {
if ($viewData instanceof TranslatableInterface) {
$viewData = $viewData->trans($this->translator, $this->translator->getLocale());
$locale = null;

if (method_exists(TranslatableInterface::class, 'getLocale')) {
$locale = $this->translator->getLocale();
}

$viewData = $viewData->trans($this->translator, $locale);
} else {
$translationDomain = $options['export']['value_translation_domain'];
$translationParameters = $options['export']['value_translation_parameters'];
Expand Down
16 changes: 14 additions & 2 deletions tests/Unit/Column/Type/ColumnTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,13 @@ private function createValueRowView(?DataTableView $dataTableView = null, mixed

protected function createTranslator(): MockObject&TranslatorInterface
{
return $this->createMock(TranslatorInterface::class);
$translator = $this->createMock(TranslatorInterface::class);

if (method_exists(TranslatableInterface::class, 'getLocale')) {
$translator->method('getLocale')->willReturn('en');
}

return $translator;
}

protected function createTranslatable(string $value, bool $expectTranslated = true): MockObject&TranslatableInterface
Expand All @@ -989,9 +995,15 @@ protected function createTranslatable(string $value, bool $expectTranslated = tr
if ($expectTranslated) {
$this->translator ??= $this->createTranslator();

$locale = null;

if (method_exists(TranslatableInterface::class, 'getLocale')) {
$locale = 'en';
}

$translatable->expects($this->once())
->method('trans')
->with($this->translator)
->with($this->translator, $locale)
->willReturn($value)
;

Expand Down

0 comments on commit 442e9bb

Please sign in to comment.