TranslationReader.php
59 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace IAWPSCOPED\Symfony\Component\Translation\Reader; |
| 12 | |
| 13 | use IAWPSCOPED\Symfony\Component\Finder\Finder; |
| 14 | use IAWPSCOPED\Symfony\Component\Translation\Loader\LoaderInterface; |
| 15 | use IAWPSCOPED\Symfony\Component\Translation\MessageCatalogue; |
| 16 | /** |
| 17 | * TranslationReader reads translation messages from translation files. |
| 18 | * |
| 19 | * @author Michel Salib <michelsalib@hotmail.com> |
| 20 | * @internal |
| 21 | */ |
| 22 | class TranslationReader implements TranslationReaderInterface |
| 23 | { |
| 24 | /** |
| 25 | * Loaders used for import. |
| 26 | * |
| 27 | * @var array<string, LoaderInterface> |
| 28 | */ |
| 29 | private array $loaders = []; |
| 30 | /** |
| 31 | * Adds a loader to the translation extractor. |
| 32 | * |
| 33 | * @param string $format The format of the loader |
| 34 | */ |
| 35 | public function addLoader(string $format, LoaderInterface $loader) |
| 36 | { |
| 37 | $this->loaders[$format] = $loader; |
| 38 | } |
| 39 | /** |
| 40 | * {@inheritdoc} |
| 41 | */ |
| 42 | public function read(string $directory, MessageCatalogue $catalogue) |
| 43 | { |
| 44 | if (!\is_dir($directory)) { |
| 45 | return; |
| 46 | } |
| 47 | foreach ($this->loaders as $format => $loader) { |
| 48 | // load any existing translation files |
| 49 | $finder = new Finder(); |
| 50 | $extension = $catalogue->getLocale() . '.' . $format; |
| 51 | $files = $finder->files()->name('*.' . $extension)->in($directory); |
| 52 | foreach ($files as $file) { |
| 53 | $domain = \substr($file->getFilename(), 0, -1 * \strlen($extension) - 1); |
| 54 | $catalogue->addCatalogue($loader->load($file->getPathname(), $catalogue->getLocale(), $domain)); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 |