TranslationLoader.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\Bundle\FrameworkBundle\Translation;
  11. use Symfony\Component\Finder\Finder;
  12. use Symfony\Component\Translation\Loader\LoaderInterface;
  13. use Symfony\Component\Translation\MessageCatalogue;
  14. /**
  15. * TranslationLoader loads translation messages from translation files.
  16. *
  17. * @author Michel Salib <michelsalib@hotmail.com>
  18. */
  19. class TranslationLoader
  20. {
  21. /**
  22. * Loaders used for import.
  23. *
  24. * @var array
  25. */
  26. private $loaders = array();
  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. * Loads translation messages from a directory to the catalogue.
  39. *
  40. * @param string $directory The directory to look into
  41. * @param MessageCatalogue $catalogue The catalogue
  42. */
  43. public function loadMessages($directory, MessageCatalogue $catalogue)
  44. {
  45. if (!is_dir($directory)) {
  46. return;
  47. }
  48. foreach ($this->loaders as $format => $loader) {
  49. // load any existing translation files
  50. $finder = new Finder();
  51. $extension = $catalogue->getLocale().'.'.$format;
  52. $files = $finder->files()->name('*.'.$extension)->in($directory);
  53. foreach ($files as $file) {
  54. $domain = substr($file->getFilename(), 0, -1 * \strlen($extension) - 1);
  55. $catalogue->addCatalogue($loader->load($file->getPathname(), $catalogue->getLocale(), $domain));
  56. }
  57. }
  58. }
  59. }