LanguageDataProvider.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\Locale;
  12. use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReaderInterface;
  13. /**
  14. * Data provider for language-related ICU data.
  15. *
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. *
  18. * @internal
  19. */
  20. class LanguageDataProvider
  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 getLanguages()
  44. {
  45. return $this->reader->readEntry($this->path, 'meta', array('Languages'));
  46. }
  47. public function getAliases()
  48. {
  49. return $this->reader->readEntry($this->path, 'root', array('Aliases'));
  50. }
  51. public function getName($language, $displayLocale = null)
  52. {
  53. if (null === $displayLocale) {
  54. $displayLocale = Locale::getDefault();
  55. }
  56. return $this->reader->readEntry($this->path, $displayLocale, array('Names', $language));
  57. }
  58. public function getNames($displayLocale = null)
  59. {
  60. if (null === $displayLocale) {
  61. $displayLocale = Locale::getDefault();
  62. }
  63. $languages = $this->reader->readEntry($this->path, $displayLocale, array('Names'));
  64. if ($languages instanceof \Traversable) {
  65. $languages = iterator_to_array($languages);
  66. }
  67. $collator = new \Collator($displayLocale);
  68. $collator->asort($languages);
  69. return $languages;
  70. }
  71. public function getAlpha3Code($language)
  72. {
  73. return $this->reader->readEntry($this->path, 'meta', array('Alpha2ToAlpha3', $language));
  74. }
  75. }