blob: bbc687e1391f9acc55fecc332153c7df7635ca37 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?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
12namespace Symfony\Component\Translation\Reader;
13
14use Symfony\Component\Finder\Finder;
15use Symfony\Component\Translation\Loader\LoaderInterface;
16use Symfony\Component\Translation\MessageCatalogue;
17
18/**
19 * TranslationReader reads translation messages from translation files.
20 *
21 * @author Michel Salib <michelsalib@hotmail.com>
22 */
23class TranslationReader implements TranslationReaderInterface
24{
25 /**
26 * Loaders used for import.
27 *
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010028 * @var array<string, LoaderInterface>
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020029 */
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010030 private array $loaders = [];
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020031
32 /**
33 * Adds a loader to the translation extractor.
34 *
35 * @param string $format The format of the loader
36 */
37 public function addLoader(string $format, LoaderInterface $loader)
38 {
39 $this->loaders[$format] = $loader;
40 }
41
42 /**
43 * {@inheritdoc}
44 */
45 public function read(string $directory, MessageCatalogue $catalogue)
46 {
47 if (!is_dir($directory)) {
48 return;
49 }
50
51 foreach ($this->loaders as $format => $loader) {
52 // load any existing translation files
53 $finder = new Finder();
54 $extension = $catalogue->getLocale().'.'.$format;
55 $files = $finder->files()->name('*.'.$extension)->in($directory);
56 foreach ($files as $file) {
57 $domain = substr($file->getFilename(), 0, -1 * \strlen($extension) - 1);
58 $catalogue->addCatalogue($loader->load($file->getPathname(), $catalogue->getLocale(), $domain));
59 }
60 }
61 }
62}