Translate Scheme::$title

This commit is contained in:
Giuseppe Criscione 2024-10-06 18:15:52 +02:00
parent d1978be1fa
commit 9af3f366c3

View File

@ -7,8 +7,10 @@ use Formwork\Data\Traits\DataArrayable;
use Formwork\Fields\FieldCollection;
use Formwork\Fields\FieldFactory;
use Formwork\Fields\Layout\Layout;
use Formwork\Translations\Translation;
use Formwork\Translations\Translations;
use Formwork\Utils\Arr;
use Formwork\Utils\Str;
use InvalidArgumentException;
class Scheme implements Arrayable
@ -20,6 +22,8 @@ class Scheme implements Arrayable
*/
protected string $path;
protected string $title;
protected SchemeOptions $options;
/**
@ -33,8 +37,6 @@ class Scheme implements Arrayable
$this->extend($this->schemes->get($this->data['extend']));
}
$this->data['title'] ??= $this->id;
$this->options = new SchemeOptions($this->data['options'] ?? []);
}
@ -56,7 +58,20 @@ class Scheme implements Arrayable
*/
public function title(): string
{
return $this->data['title'];
if (isset($this->title)) {
return $this->title;
}
$title = $this->data['title'] ?? $this->id;
if (isset($this->data['title'])) {
try {
$title = $this->translate($title, $this->translations->getCurrent());
} catch (InvalidArgumentException) {
}
}
return $this->title = $title;
}
/**
@ -83,4 +98,25 @@ class Scheme implements Arrayable
$this->data = array_replace_recursive($scheme->data, $this->data);
}
protected function translate(mixed $value, Translation $translation): mixed
{
$language = $translation->code();
if (is_array($value)) {
if (isset($value[$language])) {
$value = $value[$language];
}
} elseif (!is_string($value)) {
return $value;
}
$interpolate = fn ($value) => is_string($value) ? Str::interpolate($value, fn ($key) => $translation->translate($key)) : $value;
if (is_array($value)) {
return Arr::map($value, $interpolate);
}
return $interpolate($value);
}
}