0 follower

Final Class Yiisoft\Translator\Translator

InheritanceYiisoft\Translator\Translator
ImplementsYiisoft\Translator\TranslatorInterface

Translator translates a message into the specified language.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string $locale 'en-US', string|null $fallbackLocale null, string $defaultCategory 'app', \Psr\EventDispatcher\EventDispatcherInterface|null $eventDispatcher null, Yiisoft\Translator\MessageFormatterInterface|null $defaultMessageFormatter null )
$locale string

Default locale to use if locale is not specified explicitly.

$fallbackLocale string|null

Locale to use if message for the locale specified was not found. Null for none.

$defaultCategory string
$eventDispatcher \Psr\EventDispatcher\EventDispatcherInterface|null

Event dispatcher for translation events. Null for none.

$defaultMessageFormatter Yiisoft\Translator\MessageFormatterInterface|null

                public function __construct(
    private string $locale = 'en-US',
    private ?string $fallbackLocale = null,
    private string $defaultCategory = 'app',
    private ?EventDispatcherInterface $eventDispatcher = null,
    ?MessageFormatterInterface $defaultMessageFormatter = null,
) {
    $this->defaultMessageFormatter = $defaultMessageFormatter ?? new NullMessageFormatter();
}

            
addCategorySources() public method

public Yiisoft\Translator\Translator addCategorySources ( Yiisoft\Translator\CategorySource $categories )
$categories Yiisoft\Translator\CategorySource

                public function addCategorySources(CategorySource ...$categories): static
{
    foreach ($categories as $category) {
        if (isset($this->categorySources[$category->getName()])) {
            $this->categorySources[$category->getName()][] = $category;
        } else {
            $this->categorySources[$category->getName()] = [$category];
        }
    }
    return $this;
}

            
getLocale() public method

public string getLocale ( )

                public function getLocale(): string
{
    return $this->locale;
}

            
setLocale() public method

public Yiisoft\Translator\Translator setLocale ( string $locale )
$locale string

                public function setLocale(string $locale): static
{
    $this->locale = $locale;
    return $this;
}

            
translate() public method

public string translate ( string|\Stringable $id, array $parameters = [], string|null $category null, string|null $locale null )
$id string|\Stringable
$parameters array
$category string|null
$locale string|null

                public function translate(
    string|Stringable $id,
    array $parameters = [],
    ?string $category = null,
    ?string $locale = null
): string {
    $locale ??= $this->locale;
    $category ??= $this->defaultCategory;
    if (empty($this->categorySources[$category])) {
        $this->dispatchMissingTranslationCategoryEvent($category);
        return $this->defaultMessageFormatter->format((string) $id, $parameters, $this->fallbackLocale ?? $locale);
    }
    return $this->translateUsingCategorySources((string) $id, $parameters, $category, $locale);
}

            
withDefaultCategory() public method

public Yiisoft\Translator\Translator withDefaultCategory ( string $category )
$category string

                public function withDefaultCategory(string $category): static
{
    if (!isset($this->categorySources[$category])) {
        throw new RuntimeException('Category with name "' . $category . '" does not exist.');
    }
    $new = clone $this;
    $new->defaultCategory = $category;
    return $new;
}

            
withLocale() public method

public Yiisoft\Translator\Translator withLocale ( string $locale )
$locale string

                public function withLocale(string $locale): static
{
    $new = clone $this;
    $new->setLocale($locale);
    return $new;
}