You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.8 KiB

3 years ago
  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\DirectoryResource;
  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 IcuResFileLoader implements LoaderInterface
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function load($resource, string $locale, string $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. $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(DirectoryResource::class)) {
  47. $catalogue->addResource(new DirectoryResource($resource));
  48. }
  49. return $catalogue;
  50. }
  51. /**
  52. * Flattens an ResourceBundle.
  53. *
  54. * The scheme used is:
  55. * key { key2 { key3 { "value" } } }
  56. * Becomes:
  57. * 'key.key2.key3' => 'value'
  58. *
  59. * This function takes an array by reference and will modify it
  60. *
  61. * @param \ResourceBundle $rb The ResourceBundle that will be flattened
  62. * @param array $messages Used internally for recursive calls
  63. * @param string $path Current path being parsed, used internally for recursive calls
  64. *
  65. * @return array the flattened ResourceBundle
  66. */
  67. protected function flatten(\ResourceBundle $rb, array &$messages = [], string $path = null)
  68. {
  69. foreach ($rb as $key => $value) {
  70. $nodePath = $path ? $path.'.'.$key : $key;
  71. if ($value instanceof \ResourceBundle) {
  72. $this->flatten($value, $messages, $nodePath);
  73. } else {
  74. $messages[$nodePath] = $value;
  75. }
  76. }
  77. return $messages;
  78. }
  79. }