IntlBundleReader.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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\Intl\Data\Bundle\Reader;
  11. use Symfony\Component\Intl\Data\Util\ArrayAccessibleResourceBundle;
  12. use Symfony\Component\Intl\Exception\ResourceBundleNotFoundException;
  13. /**
  14. * Reads binary .res resource bundles.
  15. *
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. *
  18. * @internal
  19. */
  20. class IntlBundleReader implements BundleReaderInterface
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function read(string $path, string $locale)
  26. {
  27. // Point for future extension: Modify this class so that it works also
  28. // if the \ResourceBundle class is not available.
  29. try {
  30. // Never enable fallback. We want to know if a bundle cannot be found
  31. $bundle = new \ResourceBundle($locale, $path, false);
  32. } catch (\Exception $e) {
  33. $bundle = null;
  34. }
  35. // The bundle is NULL if the path does not look like a resource bundle
  36. // (i.e. contain a bunch of *.res files)
  37. if (null === $bundle) {
  38. throw new ResourceBundleNotFoundException(sprintf('The resource bundle "%s/%s.res" could not be found.', $path, $locale));
  39. }
  40. // Other possible errors are U_USING_FALLBACK_WARNING and U_ZERO_ERROR,
  41. // which are OK for us.
  42. return new ArrayAccessibleResourceBundle($bundle);
  43. }
  44. }