IcuDatFileLoader.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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\Loader;
  11. use Symfony\Component\Translation\MessageCatalogue;
  12. use Symfony\Component\Translation\Exception\InvalidResourceException;
  13. use Symfony\Component\Translation\Exception\NotFoundResourceException;
  14. use Symfony\Component\Config\Resource\FileResource;
  15. /**
  16. * IcuResFileLoader loads translations from a resource bundle.
  17. *
  18. * @author stealth35
  19. */
  20. class IcuDatFileLoader extends IcuResFileLoader
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function load($resource, $locale, $domain = 'messages')
  26. {
  27. if (!stream_is_local($resource.'.dat')) {
  28. throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
  29. }
  30. if (!file_exists($resource.'.dat')) {
  31. throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
  32. }
  33. $rb = new \ResourceBundle($locale, $resource);
  34. if (!$rb) {
  35. throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource));
  36. } elseif (intl_is_failure($rb->getErrorCode())) {
  37. throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
  38. }
  39. $messages = $this->flatten($rb);
  40. $catalogue = new MessageCatalogue($locale);
  41. $catalogue->add($messages, $domain);
  42. $catalogue->addResource(new FileResource($resource.'.dat'));
  43. return $catalogue;
  44. }
  45. }