Final Class Yiisoft\Translator\CategorySource
| Inheritance | Yiisoft\Translator\CategorySource |
|---|
Represents message category.
Public 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
| 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 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);
}
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);
}
| public array getMessages ( string $locale ) | ||
| $locale | string |
Locale of messages to get. |
| return | array |
All messages from category. The format is the following:
|
|---|---|---|
public function getMessages(string $locale): array
{
return $this->reader->getMessages($this->name, $locale);
}
| public string getName ( ) | ||
| return | string |
Category name. |
|---|---|---|
public function getName(): string
{
return $this->name;
}
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:
], 'key2' => [
],
]
|
| 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);
}
Signup or Login in order to comment.