for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Tleckie\Translate;
use Tleckie\Translate\Bookmark\BookmarkInterface;
use Tleckie\Translate\Loader\LoaderInterface;
use function sprintf;
/**
* Class Translator
*
* @package Tleckie\Translate
* @category Translator
* @author Teodoro Leckie Westberg <[email protected]>
*/
class Translator implements TranslatorInterface
{
* @var LoaderInterface
protected LoaderInterface $loader;
* @var string
protected string $locale;
* @var BookmarkInterface
protected BookmarkInterface $bookmark;
* Translator constructor.
* @param LoaderInterface $loader
* @param string $locale
* @param BookmarkInterface|null $bookmark
public function __construct(
LoaderInterface $loader,
string $locale = '',
BookmarkInterface $bookmark = null
) {
$this->loader = $loader;
$this->setLocale($locale);
$this->bookmark = $bookmark ?? new Bookmark();
$this->bookmark->setLoader($loader);
}
* @inheritdoc
public function getBookmark(): BookmarkInterface
return $this->bookmark;
public function getLoader(): LoaderInterface
return $this->loader;
public function getLocale(): string
return $this->locale;
public function setLocale(string $locale): TranslatorInterface
$this->locale = $locale;
return $this;
public function trans(
string $keyToTranslate,
array $arguments = [],
string $fallback = null,
string $locale = null
): string {
$locale = $locale ?? $this->locale;
$replaced = sprintf(
$this->bookmark->getCatalogue($locale)->getByKey($keyToTranslate),
...$arguments
);
if (empty($replaced)) {
return $fallback;
return $replaced;