IcuResFileLoader.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\DirectoryResource;
  15. /**
  16. * IcuResFileLoader loads translations from a resource bundle.
  17. *
  18. * @author stealth35
  19. */
  20. class IcuResFileLoader implements LoaderInterface
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function load($resource, $locale, $domain = 'messages')
  26. {
  27. if (!stream_is_local($resource)) {
  28. throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
  29. }
  30. if (!is_dir($resource)) {
  31. throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
  32. }
  33. try {
  34. $rb = new \ResourceBundle($locale, $resource);
  35. } catch (\Exception $e) {
  36. // HHVM compatibility: constructor throws on invalid resource
  37. $rb = null;
  38. }
  39. if (!$rb) {
  40. throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource));
  41. } elseif (intl_is_failure($rb->getErrorCode())) {
  42. throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
  43. }
  44. $messages = $this->flatten($rb);
  45. $catalogue = new MessageCatalogue($locale);
  46. $catalogue->add($messages, $domain);
  47. if (class_exists('Symfony\Component\Config\Resource\DirectoryResource')) {
  48. $catalogue->addResource(new DirectoryResource($resource));
  49. }
  50. return $catalogue;
  51. }
  52. /**
  53. * Flattens an ResourceBundle.
  54. *
  55. * The scheme used is:
  56. * key { key2 { key3 { "value" } } }
  57. * Becomes:
  58. * 'key.key2.key3' => 'value'
  59. *
  60. * This function takes an array by reference and will modify it
  61. *
  62. * @param \ResourceBundle $rb the ResourceBundle that will be flattened
  63. * @param array $messages used internally for recursive calls
  64. * @param string $path current path being parsed, used internally for recursive calls
  65. *
  66. * @return array the flattened ResourceBundle
  67. */
  68. protected function flatten(\ResourceBundle $rb, array &$messages = array(), $path = null)
  69. {
  70. foreach ($rb as $key => $value) {
  71. $nodePath = $path ? $path.'.'.$key : $key;
  72. if ($value instanceof \ResourceBundle) {
  73. $this->flatten($value, $messages, $nodePath);
  74. } else {
  75. $messages[$nodePath] = $value;
  76. }
  77. }
  78. return $messages;
  79. }
  80. }