Skip to content

Commit

Permalink
Add translate() method to GlobalContext + Translate "Actions"
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Nov 25, 2023
1 parent 6a94d24 commit 5bc2e41
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions messages/es/yii-dataview.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
return [
'No results found.' => 'No se han encontrado resultados.',
'Page <b>{currentPage}</b> of <b>{totalPages}</b>' => 'Pagina <b>{currentPage}</b> de <b>{totalPages}</b>',
'Actions' => 'Acciones',
];
1 change: 1 addition & 0 deletions messages/ru/yii-dataview.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
return [
'No results found.' => 'Результатов не найдено.',
'Page <b>{currentPage}</b> of <b>{totalPages}</b>' => 'Страница <b>{currentPage}</b> из <b>{totalPages}</b>',
'Actions' => 'Действия',
];
4 changes: 2 additions & 2 deletions src/BaseListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract class BaseListView extends Widget
* @var TranslatorInterface A translator instance used for translations of messages. If it was not set
* explicitly in the constructor, a default one created automatically in {@see createDefaultTranslator()}.
*/
private TranslatorInterface $translator;
protected readonly TranslatorInterface $translator;

private array $attributes = [];
protected ?string $emptyText = null;
Expand All @@ -57,7 +57,7 @@ abstract class BaseListView extends Widget
public function __construct(
TranslatorInterface|null $translator = null,
private UrlGeneratorInterface|null $urlGenerator = null,
private string $translationCategory = self::DEFAULT_TRANSLATION_CATEGORY,
protected readonly string $translationCategory = self::DEFAULT_TRANSLATION_CATEGORY,
) {
$this->translator = $translator ?? $this->createDefaultTranslator();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Column/ActionColumnRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function renderHeader(ColumnInterface $column, Cell $cell, GlobalContext
{
$this->checkColumn($column);
return $cell
->content($column->header ?? 'Actions')
->content($column->header ?? $context->translate('Actions'))
->addAttributes($column->headerAttributes);
}

Expand Down
15 changes: 12 additions & 3 deletions src/Column/Base/GlobalContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@

namespace Yiisoft\Yii\DataView\Column\Base;

use Stringable;
use Yiisoft\Data\Reader\ReadableDataInterface;
use Yiisoft\Translator\TranslatorInterface;

final class GlobalContext
{
public function __construct(
public readonly ReadableDataInterface $dataReader,
public readonly array $sortLinkAttributes,
public readonly array $urlArguments = [],
public readonly array $urlQueryParameters = [],
public readonly ?string $filterModelName = null,
public readonly array $urlArguments,
public readonly array $urlQueryParameters,
public readonly ?string $filterModelName,
private readonly TranslatorInterface $translator,
private readonly string $translationCategory,
) {
}

public function translate(string|Stringable $id): string
{
return $this->translator->translate($id, category: $this->translationCategory);
}
}
2 changes: 2 additions & 0 deletions src/GridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,8 @@ protected function renderItems(): string
$this->urlArguments,
$this->urlQueryParameters,
$this->filterModelName,
$this->translator,
$this->translationCategory,
);

if ($this->columnsGroupEnabled) {
Expand Down

0 comments on commit 5bc2e41

Please sign in to comment.