4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Reader;
14 use Symfony\Component\Finder\Finder;
15 use Symfony\Component\Translation\Loader\LoaderInterface;
16 use Symfony\Component\Translation\MessageCatalogue;
19 * TranslationReader reads translation messages from translation files.
21 * @author Michel Salib <michelsalib@hotmail.com>
23 class TranslationReader implements TranslationReaderInterface
26 * Loaders used for import.
30 private $loaders = array();
33 * Adds a loader to the translation extractor.
35 * @param string $format The format of the loader
36 * @param LoaderInterface $loader
38 public function addLoader($format, LoaderInterface $loader)
40 $this->loaders[$format] = $loader;
46 public function read($directory, MessageCatalogue $catalogue)
48 if (!is_dir($directory)) {
52 foreach ($this->loaders as $format => $loader) {
53 // load any existing translation files
54 $finder = new Finder();
55 $extension = $catalogue->getLocale().'.'.$format;
56 $files = $finder->files()->name('*.'.$extension)->in($directory);
57 foreach ($files as $file) {
58 $domain = substr($file->getFilename(), 0, -1 * \strlen($extension) - 1);
59 $catalogue->addCatalogue($loader->load($file->getPathname(), $catalogue->getLocale(), $domain));