Skip to content

Commit

Permalink
fix(translatable): update the TranslatableInterface to change the `…
Browse files Browse the repository at this point in the history
…trans` method signature and update `TranslatableValue` with said method and deprecate `each` and `sanitize` for future version of charcoal
  • Loading branch information
JoelAlphonso authored and mcaskill committed Mar 5, 2024
1 parent 6723433 commit a728e07
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace Charcoal\Translator;

use Symfony\Component\Translation\TranslatorInterface;

interface TranslatableInterface
{
/**
* @param Translator $translator The translator.
* @param string|null $locale The locale.
* @return string
* @param TranslatorInterface $translator The $translator to use.
* @return mixed
*/
public function trans(Translator $translator, ?string $locale = null): string;
public function trans(TranslatorInterface $translator);
}
50 changes: 33 additions & 17 deletions packages/translator/src/Charcoal/Translator/TranslatableValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use InvalidArgumentException;
use JsonSerializable;
use Stringable;
use Symfony\Component\Translation\TranslatorInterface;

/**
Expand All @@ -19,29 +20,19 @@
class TranslatableValue implements
TranslatableInterface,
JsonSerializable,
\Stringable
Stringable
{
/**
* @var array<string, mixed>
*/
private array $translations;

/** @var array */
private array $parameters;

/** @var string|null */
private ?string $domain;

/**
* @param array<string, mixed>|Translation|self $translations Values keyed by locale.
* @param array $parameters An array of parameters for the message.
* @param string|null $domain The domain for the message or NULL to use the default.
*/
public function __construct($translations, array $parameters = [], string $domain = null)
final public function __construct($translations)
{
$this->translations = $this->sanitizeTranslations($translations);
$this->parameters = $parameters;
$this->domain = $domain;
}

/**
Expand Down Expand Up @@ -115,13 +106,28 @@ public function jsonSerialize(): array
return $this->toArray();
}

/**
* @param callable $callback The callback function.
* @return static
*/
public function map(callable $callback): self
{
$keys = array_keys($this->translations);

$translations = array_map($callback, $this->translations, $keys);

return new static(array_combine($keys, $translations));
}

/**
* Transform each translation value.
*
* This method is to maintain compatibility with {@see Translation}.
*
* @param (callable(mixed, string): mixed) $callback Function to apply to each value.
* @return self
*
* @deprecated Will be removed in future version in favor of keeping this class Immutable.
*/
public function each(callable $callback): self
{
Expand All @@ -138,6 +144,8 @@ public function each(callable $callback): self
*
* @param (callable(mixed): mixed) $callback Function to apply to each value.
* @return self
*
* @deprecated Will be removed in future version in favor of keeping this class Immutable.
*/
public function sanitize(callable $callback): self
{
Expand All @@ -148,12 +156,20 @@ public function sanitize(callable $callback): self
}

/**
* @param Translator $translator The translator.
* @param string|null $locale The locale.
* @return string
* @param string $locale The requested locale.
* @return mixed
*/
public function toLocale(string $locale)
{
return ($this->translations[$locale] ?? null);
}

/**
* @param TranslatorInterface $translator The translator to use.
* @return mixed
*/
public function trans(Translator $translator, ?string $locale = null): string
public function trans(TranslatorInterface $translator)
{
return $translator->translate($this->translations, $this->parameters, $this->domain, $locale);
return $this->toLocale($translator->getLocale());
}
}

0 comments on commit a728e07

Please sign in to comment.