IcuDatFileLoader.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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\Config\Resource\FileResource;
  12. use Symfony\Component\Translation\Exception\InvalidResourceException;
  13. use Symfony\Component\Translation\Exception\NotFoundResourceException;
  14. use Symfony\Component\Translation\MessageCatalogue;
  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. try {
  34. $rb = new \ResourceBundle($locale, $resource);
  35. } catch (\Exception $e) {
  36. $rb = null;
  37. }
  38. if (!$rb) {
  39. throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource));
  40. } elseif (intl_is_failure($rb->getErrorCode())) {
  41. throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
  42. }
  43. $messages = $this->flatten($rb);
  44. $catalogue = new MessageCatalogue($locale);
  45. $catalogue->add($messages, $domain);
  46. if (class_exists('Symfony\Component\Config\Resource\FileResource')) {
  47. $catalogue->addResource(new FileResource($resource.'.dat'));
  48. }
  49. return $catalogue;
  50. }
  51. }