0 follower

Final Class Yiisoft\TranslatorExtractor\Extractor

InheritanceYiisoft\TranslatorExtractor\Extractor

Extracts translator IDs from files within a given path and writes them into message source given merging results with what is already there.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\TranslatorExtractor\Extractor
process() Yiisoft\TranslatorExtractor\Extractor
setExcept() Set list of patterns that the files or directories should not match. Yiisoft\TranslatorExtractor\Extractor
setOnly() Set list of patterns that the files or directories should match. Yiisoft\TranslatorExtractor\Extractor

Method Details

Hide inherited methods

__construct() public method

public __construct( Yiisoft\TranslatorExtractor\CategorySource[] $categories, string $translatorCall '->translate' ): mixed
$categories Yiisoft\TranslatorExtractor\CategorySource[]
$translatorCall string

Translation call to look for.

                public function __construct(array $categories, private string $translatorCall = '->translate')
{
    if (empty($categories)) {
        throw new NoCategorySourceConfigException();
    }
    foreach ($categories as $category) {
        $this->categorySources[$category->getName()] = $category;
    }
}

            
process() public method

public process( string $filesPath, string $defaultCategory, string[] $languages, \Symfony\Component\Console\Output\OutputInterface $output ): void
$filesPath string

Path to files to extract from.

$defaultCategory string

Category to use if category isn't set in translation call.

$languages string[]

Languages to write extracted IDs to.

$output \Symfony\Component\Console\Output\OutputInterface

                public function process(string $filesPath, string $defaultCategory, array $languages, OutputInterface $output): void
{
    if (!isset($this->categorySources[$defaultCategory])) {
        $output->writeln('<comment>Default category was not found in a list of Categories.</comment>');
        return;
    }
    $translationExtractor = new TranslationExtractor(
        $filesPath,
        $this->applyRoot($this->only, $filesPath),
        $this->applyRoot($this->except, $filesPath)
    );
    $messagesList = $translationExtractor->extract($defaultCategory, $this->translatorCall);
    if (empty($messagesList)) {
        $output->writeln('<comment>Messages not found</comment>');
        return;
    }
    $output->writeln('Languages: ' . implode(', ', $languages));
    /**
     * @var string $categoryName
     * @var array<array-key, array<string, string>|mixed> $messages
     */
    foreach ($messagesList as $categoryName => $messages) {
        $output->writeln('<info>Category: "' . $categoryName . '", messages found: ' . count($messages) . '.</info>');
        /** @var array<string, array<string, string>> $convertedMessages */
        $convertedMessages = $this->convert($messages);
        foreach ($languages as $language) {
            $extractCategory = isset($this->categorySources[$categoryName]) ? $categoryName : $defaultCategory;
            $this->addMessages($extractCategory, $language, $convertedMessages);
        }
    }
}

            
setExcept() public method

Set list of patterns that the files or directories should not match.

See also \Yiisoft\Files\PathMatcher\PathMatcher.

public setExcept( string[] $except ): void
$except string[]

                public function setExcept(array $except): void
{
    if (!empty($except)) {
        $this->except = $except;
    }
}

            
setOnly() public method

Set list of patterns that the files or directories should match.

See also \Yiisoft\Files\PathMatcher\PathMatcher.

public setOnly( string[] $only ): void
$only string[]

                public function setOnly(array $only): void
{
    if (!empty($only)) {
        $this->only = $only;
    }
}