LocaleDataProvider.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\Provider;
  11. use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReaderInterface;
  12. use Symfony\Component\Intl\Locale;
  13. /**
  14. * Data provider for locale-related ICU data.
  15. *
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. *
  18. * @internal
  19. */
  20. class LocaleDataProvider
  21. {
  22. /**
  23. * @var string
  24. */
  25. private $path;
  26. /**
  27. * @var BundleEntryReaderInterface
  28. */
  29. private $reader;
  30. /**
  31. * Creates a data provider that reads locale-related data from .res files.
  32. *
  33. * @param string $path The path to the directory
  34. * containing the .res files.
  35. * @param BundleEntryReaderInterface $reader The reader for reading the .res
  36. * files.
  37. */
  38. public function __construct($path, BundleEntryReaderInterface $reader)
  39. {
  40. $this->path = $path;
  41. $this->reader = $reader;
  42. }
  43. public function getLocales()
  44. {
  45. return $this->reader->readEntry($this->path, 'meta', array('Locales'));
  46. }
  47. public function getAliases()
  48. {
  49. $aliases = $this->reader->readEntry($this->path, 'meta', array('Aliases'));
  50. if ($aliases instanceof \Traversable) {
  51. $aliases = iterator_to_array($aliases);
  52. }
  53. return $aliases;
  54. }
  55. public function getName($locale, $displayLocale = null)
  56. {
  57. if (null === $displayLocale) {
  58. $displayLocale = Locale::getDefault();
  59. }
  60. return $this->reader->readEntry($this->path, $displayLocale, array('Names', $locale));
  61. }
  62. public function getNames($displayLocale = null)
  63. {
  64. if (null === $displayLocale) {
  65. $displayLocale = Locale::getDefault();
  66. }
  67. $names = $this->reader->readEntry($this->path, $displayLocale, array('Names'));
  68. if ($names instanceof \Traversable) {
  69. $names = iterator_to_array($names);
  70. }
  71. $collator = new \Collator($displayLocale);
  72. $collator->asort($names);
  73. return $names;
  74. }
  75. }