暫無描述

TranslationReader.php 1.7KB

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. */
  32. public function addLoader($format, LoaderInterface $loader)
  33. {
  34. $this->loaders[$format] = $loader;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function read($directory, MessageCatalogue $catalogue)
  40. {
  41. if (!is_dir($directory)) {
  42. return;
  43. }
  44. foreach ($this->loaders as $format => $loader) {
  45. // load any existing translation files
  46. $finder = new Finder();
  47. $extension = $catalogue->getLocale().'.'.$format;
  48. $files = $finder->files()->name('*.'.$extension)->in($directory);
  49. foreach ($files as $file) {
  50. $domain = substr($file->getFilename(), 0, -1 * \strlen($extension) - 1);
  51. $catalogue->addCatalogue($loader->load($file->getPathname(), $catalogue->getLocale(), $domain));
  52. }
  53. }
  54. }
  55. }