Skip to content

Commit

Permalink
fix(translator): Revert trans() method of TranslatableInterface
Browse files Browse the repository at this point in the history
This commit reverts a728e07 and 6723433
  • Loading branch information
mcaskill committed Mar 12, 2024
1 parent d3c71b9 commit 81393f6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
interface TranslatableInterface
{
/**
* @param TranslatorInterface $translator The $translator to use.
* @param TranslatorInterface $translator The translator.
* @param ?string $locale The locale.
* @return mixed
*/
public function trans(TranslatorInterface $translator);
public function trans(TranslatorInterface $translator, ?string $locale = null);
}
19 changes: 11 additions & 8 deletions packages/translator/src/Charcoal/Translator/TranslatableValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,24 @@ public function sanitize(callable $callback): self
}

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

/**
* @param string $locale The requested locale.
* @param TranslatorInterface $translator The translator.
* @param ?string $locale The locale.
* @return mixed
*/
public function toLocale(string $locale)
public function trans(TranslatorInterface $translator, ?string $locale = null)
{
return ($this->translations[$locale] ?? null);
$locale ??= $translator->getLocale();

return $this->toLocale($locale);
}

/**
Expand Down Expand Up @@ -221,7 +224,7 @@ public function offsetGet($offset)
}

/**
* @param strin`g $offset The lang offset to set.
* @param string $offset The lang offset to set.
* @param mixed $value The value to store.
* @return void
* @throws InvalidArgumentException If array key isn't a string.
Expand All @@ -247,7 +250,7 @@ public function offsetSet($offset, $value)
}

/**
* @param string $offset The language offset to unset.
* @param string $offset The language offset to unset.
* @return void
* @throws InvalidArgumentException If array key isn't a string.
*/
Expand Down
24 changes: 13 additions & 11 deletions packages/translator/src/Charcoal/Translator/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace Charcoal\Translator;

use ArrayAccess;
use Charcoal\Translator\LocalesManager;
use DomainException;
use InvalidArgumentException;
use JsonSerializable;
// From 'charcoal-translator'
use Charcoal\Translator\LocalesManager;
use Symfony\Component\Translation\TranslatorInterface;

/**
Expand Down Expand Up @@ -197,6 +196,18 @@ public function each(callable $callback)
return $this;
}

/**
* @param TranslatorInterface $translator The translator.
* @param ?string $locale The locale.
* @return string
*/
public function trans(TranslatorInterface $translator, ?string $locale = null)
{
$locale ??= $translator->getLocale();

return $this->val[$locale];
}

/**
* Assign the current translation value(s).
*
Expand Down Expand Up @@ -239,13 +250,4 @@ private function setVal($val)

return $this;
}

/**
* @param TranslatorInterface $translator The translator to use.
* @return mixed
*/
public function trans(TranslatorInterface $translator)
{
return ($this->val[$translator->getLocale()] ?? null);
}
}

0 comments on commit 81393f6

Please sign in to comment.