SymfonyComponentTranslationLoaderIcuResFileLoader

IcuResFileLoader loads translations from a resource bundle.

Defined (1)

The class is defined in the following location(s).

/vendor/symfony/translation/Loader/IcuResFileLoader.php  
  1. class IcuResFileLoader implements LoaderInterface 
  2. /** 
  3. * {@inheritdoc} 
  4. */ 
  5. public function load($resource, $locale, $domain = 'messages') 
  6. if (!stream_is_local($resource)) { 
  7. throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); 
  8.  
  9. if (!is_dir($resource)) { 
  10. throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); 
  11.  
  12. try { 
  13. $rb = new \ResourceBundle($locale, $resource); 
  14. } catch (\Exception $e) { 
  15. // HHVM compatibility: constructor throws on invalid resource 
  16. $rb = null; 
  17.  
  18. if (!$rb) { 
  19. throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource)); 
  20. } elseif (intl_is_failure($rb->getErrorCode())) { 
  21. throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode()); 
  22.  
  23. $messages = $this->flatten($rb); 
  24. $catalogue = new MessageCatalogue($locale); 
  25. $catalogue->add($messages, $domain); 
  26.  
  27. if (class_exists('Symfony\Component\Config\Resource\DirectoryResource')) { 
  28. $catalogue->addResource(new DirectoryResource($resource)); 
  29.  
  30. return $catalogue; 
  31.  
  32. /** 
  33. * Flattens an ResourceBundle. 
  34. * The scheme used is: 
  35. * key { key2 { key3 { "value" } } } 
  36. * Becomes: 
  37. * 'key.key2.key3' => 'value' 
  38. * This function takes an array by reference and will modify it 
  39. * @param \ResourceBundle $rb the ResourceBundle that will be flattened 
  40. * @param array $messages used internally for recursive calls 
  41. * @param string $path current path being parsed, used internally for recursive calls 
  42. * @return array the flattened ResourceBundle 
  43. */ 
  44. protected function flatten(\ResourceBundle $rb, array &$messages = array(), $path = null) 
  45. foreach ($rb as $key => $value) { 
  46. $nodePath = $path ? $path.'.'.$key : $key; 
  47. if ($value instanceof \ResourceBundle) { 
  48. $this->flatten($value, $messages, $nodePath); 
  49. } else { 
  50. $messages[$nodePath] = $value; 
  51.  
  52. return $messages;