0 follower

Final Class Yiisoft\Translator\CategorySource

InheritanceYiisoft\Translator\CategorySource

Represents message category.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Translator\CategorySource
format() Format the message given parameters and locale. Yiisoft\Translator\CategorySource
getMessage() Get a message with ID, locale and parameters specified. Yiisoft\Translator\CategorySource
getMessages() Yiisoft\Translator\CategorySource
getName() Yiisoft\Translator\CategorySource
write() Writes a set of messages for a specified category and locale. Yiisoft\Translator\CategorySource

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string $name, Yiisoft\Translator\MessageReaderInterface $reader, Yiisoft\Translator\MessageFormatterInterface|null $formatter null, Yiisoft\Translator\MessageWriterInterface|null $writer null )
$name string

Category name.

$reader Yiisoft\Translator\MessageReaderInterface

Message reader to get messages from for this category.

$formatter Yiisoft\Translator\MessageFormatterInterface|null

Message formatter to format messages with for this category.

$writer Yiisoft\Translator\MessageWriterInterface|null

Message writer to write messages for this category.

                public function __construct(
    string $name,
    private MessageReaderInterface $reader,
    private ?MessageFormatterInterface $formatter = null,
    private ?MessageWriterInterface $writer = null,
) {
    if (!preg_match('/^[a-z0-9_-]+$/i', $name)) {
        throw new RuntimeException('Category name is invalid. Only letters and numbers are allowed.');
    }
    $this->name = $name;
}

            
format() public method

Format the message given parameters and locale.

public string format ( string $message, array $parameters, string $locale, Yiisoft\Translator\MessageFormatterInterface $defaultFormatter )
$message string

Message to be formatted.

$parameters array

Parameters to use.

$locale string

Locale to use. Usually affects formatting numbers, dates etc.

$defaultFormatter Yiisoft\Translator\MessageFormatterInterface

Message formatter that will be used if formatter not specified in message category.

return string

Formatted message.

                public function format(
    string $message,
    array $parameters,
    string $locale,
    MessageFormatterInterface $defaultFormatter
): string {
    return ($this->formatter ?? $defaultFormatter)->format($message, $parameters, $locale);
}

            
getMessage() public method

Get a message with ID, locale and parameters specified.

public string|null getMessage ( string $id, string $locale, array $parameters = [] )
$id string

Message ID.

$locale string

Locale to get message for.

$parameters array

Message parameters.

return string|null

Message string or null if message was not found.

                public function getMessage(string $id, string $locale, array $parameters = []): ?string
{
    return $this->reader->getMessage($id, $this->name, $locale, $parameters);
}

            
getMessages() public method
public array getMessages ( string $locale )
$locale string

Locale of messages to get.

return array

All messages from category. The format is the following:

[
  'key1' => [
    'message' => 'translation1',
    // Extra metadata that writer may use:
    'comment' => 'Translate carefully!',
  ],
  'key2' => [
    'message' => 'translation2',
    // Extra metadata that writer may use:
    'comment' => 'Translate carefully!',
  ],
]

                public function getMessages(string $locale): array
{
    return $this->reader->getMessages($this->name, $locale);
}

            
getName() public method

public string getName ( )
return string

Category name.

                public function getName(): string
{
    return $this->name;
}

            
write() public method

Writes a set of messages for a specified category and locale.

public void write ( string $locale, array $messages )
$locale string

Locale to write messages for.

$messages array

A set of messages to write. The format is the following: `php [ 'key1' => [

'message' => 'translation1',
// Extra metadata that writer may use:
'comment' => 'Translate carefully!',

], 'key2' => [

'message' => 'translation2',
// Extra metadata that writer may use:
'comment' => 'Translate carefully!',

], ] `

throws Yiisoft\Translator\UnwritableCategorySourceException

When $write is not configured, or it's impossible to write the messages into the source.

                public function write(string $locale, array $messages): void
{
    if ($this->writer === null) {
        throw new UnwritableCategorySourceException($this->name);
    }
    $this->writer->write($this->name, $locale, $messages);
}