0 follower

Final Class Yiisoft\Translator\Extractor\TranslationExtractor

InheritanceYiisoft\Translator\Extractor\TranslationExtractor

Extracts translator IDs from files within a given path.

Method Details

Hide inherited methods

__construct() public method

TranslationExtractor constructor.

public __construct( string $path, string[]|null $only null, string[]|null $except null ): mixed
$path string

Path to start extraction at.

$only string[]|null

List of patterns that the files or directories should match. See \Yiisoft\Files\PathMatcher\PathMatcher.

$except string[]|null

List of patterns that the files or directories should not match. See \Yiisoft\Files\PathMatcher\PathMatcher.

                public function __construct(string $path, ?array $only = null, ?array $except = null)
{
    if (!is_dir($path)) {
        throw new RuntimeException(sprintf('Directory "%s" does not exist.', $path));
    }
    $this->path = $path;
    if ($only !== null) {
        $this->only = $only;
    }
    if ($except !== null) {
        $this->except = $except;
    }
}

            
extract() public method

Extract messages.

public extract( string $defaultCategory 'app', string|null $translatorCall null ): array
$defaultCategory string

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

$translatorCall string|null

Translation call to look for.

return array

Extracted messages.

                public function extract(string $defaultCategory = 'app', ?string $translatorCall = null): array
{
    $messages = [];
    $parser = new ContentParser($defaultCategory, $translatorCall);
    $files = FileHelper::findFiles($this->path, [
        'filter' => (new PathMatcher())
            ->only(...$this->only)
            ->except(...$this->except),
        'recursive' => true,
    ]);
    foreach ($files as $file) {
        /**
         * @var string $fileContent We assume that `file_get_contents` can always read the file.
         */
        $fileContent = file_get_contents($file);
        $messages = array_merge_recursive($messages, $parser->extract($fileContent));
        if ($parser->hasSkippedLines()) {
            $this->skippedLines[$file] = $parser->getSkippedLines();
        }
    }
    return $messages;
}

            
getSkippedLines() public method

public getSkippedLines( ): array
return array

Lines that were skipped during parsing.

The format is:

return [
    'fileName' => [
        [
            int $numberOfLine,
            string $incorrectLine,
        ],
    ],
]

                public function getSkippedLines(): array
{
    return $this->skippedLines;
}

            
hasSkippedLines() public method

public hasSkippedLines( ): boolean
return boolean

Whether there are skipped lines.

                public function hasSkippedLines(): bool
{
    return !empty($this->skippedLines);
}