TranslationReader.php 1.8 KB

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