annotate vendor/symfony/translation/Loader/IcuResFileLoader.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Translation\Loader;
Chris@0 13
Chris@17 14 use Symfony\Component\Config\Resource\DirectoryResource;
Chris@0 15 use Symfony\Component\Translation\Exception\InvalidResourceException;
Chris@0 16 use Symfony\Component\Translation\Exception\NotFoundResourceException;
Chris@17 17 use Symfony\Component\Translation\MessageCatalogue;
Chris@0 18
Chris@0 19 /**
Chris@0 20 * IcuResFileLoader loads translations from a resource bundle.
Chris@0 21 *
Chris@0 22 * @author stealth35
Chris@0 23 */
Chris@0 24 class IcuResFileLoader implements LoaderInterface
Chris@0 25 {
Chris@0 26 /**
Chris@0 27 * {@inheritdoc}
Chris@0 28 */
Chris@0 29 public function load($resource, $locale, $domain = 'messages')
Chris@0 30 {
Chris@0 31 if (!stream_is_local($resource)) {
Chris@0 32 throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
Chris@0 33 }
Chris@0 34
Chris@0 35 if (!is_dir($resource)) {
Chris@0 36 throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
Chris@0 37 }
Chris@0 38
Chris@0 39 try {
Chris@0 40 $rb = new \ResourceBundle($locale, $resource);
Chris@0 41 } catch (\Exception $e) {
Chris@0 42 // HHVM compatibility: constructor throws on invalid resource
Chris@0 43 $rb = null;
Chris@0 44 }
Chris@0 45
Chris@0 46 if (!$rb) {
Chris@0 47 throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource));
Chris@0 48 } elseif (intl_is_failure($rb->getErrorCode())) {
Chris@0 49 throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
Chris@0 50 }
Chris@0 51
Chris@0 52 $messages = $this->flatten($rb);
Chris@0 53 $catalogue = new MessageCatalogue($locale);
Chris@0 54 $catalogue->add($messages, $domain);
Chris@0 55
Chris@0 56 if (class_exists('Symfony\Component\Config\Resource\DirectoryResource')) {
Chris@0 57 $catalogue->addResource(new DirectoryResource($resource));
Chris@0 58 }
Chris@0 59
Chris@0 60 return $catalogue;
Chris@0 61 }
Chris@0 62
Chris@0 63 /**
Chris@0 64 * Flattens an ResourceBundle.
Chris@0 65 *
Chris@0 66 * The scheme used is:
Chris@0 67 * key { key2 { key3 { "value" } } }
Chris@0 68 * Becomes:
Chris@0 69 * 'key.key2.key3' => 'value'
Chris@0 70 *
Chris@0 71 * This function takes an array by reference and will modify it
Chris@0 72 *
Chris@14 73 * @param \ResourceBundle $rb The ResourceBundle that will be flattened
Chris@14 74 * @param array $messages Used internally for recursive calls
Chris@14 75 * @param string $path Current path being parsed, used internally for recursive calls
Chris@0 76 *
Chris@0 77 * @return array the flattened ResourceBundle
Chris@0 78 */
Chris@17 79 protected function flatten(\ResourceBundle $rb, array &$messages = [], $path = null)
Chris@0 80 {
Chris@0 81 foreach ($rb as $key => $value) {
Chris@0 82 $nodePath = $path ? $path.'.'.$key : $key;
Chris@0 83 if ($value instanceof \ResourceBundle) {
Chris@0 84 $this->flatten($value, $messages, $nodePath);
Chris@0 85 } else {
Chris@0 86 $messages[$nodePath] = $value;
Chris@0 87 }
Chris@0 88 }
Chris@0 89
Chris@0 90 return $messages;
Chris@0 91 }
Chris@0 92 }