Locales.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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;
  11. use Symfony\Component\Intl\Exception\MissingResourceException;
  12. /**
  13. * Gives access to locale-related ICU data.
  14. *
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. * @author Roland Franssen <franssen.roland@gmail.com>
  17. */
  18. final class Locales extends ResourceBundle
  19. {
  20. /**
  21. * @return string[]
  22. */
  23. public static function getLocales(): array
  24. {
  25. return self::readEntry(['Locales'], 'meta');
  26. }
  27. /**
  28. * @return string[]
  29. */
  30. public static function getAliases(): array
  31. {
  32. return self::readEntry(['Aliases'], 'meta');
  33. }
  34. public static function exists(string $locale): bool
  35. {
  36. try {
  37. self::readEntry(['Names', $locale]);
  38. return true;
  39. } catch (MissingResourceException $e) {
  40. return \in_array($locale, self::getAliases(), true);
  41. }
  42. }
  43. /**
  44. * @throws MissingResourceException if the locale does not exist
  45. */
  46. public static function getName(string $locale, string $displayLocale = null): string
  47. {
  48. try {
  49. return self::readEntry(['Names', $locale], $displayLocale);
  50. } catch (MissingResourceException $e) {
  51. if (false === $aliased = array_search($locale, self::getAliases(), true)) {
  52. throw $e;
  53. }
  54. return self::readEntry(['Names', $aliased], $displayLocale);
  55. }
  56. }
  57. /**
  58. * @return string[]
  59. */
  60. public static function getNames($displayLocale = null): array
  61. {
  62. return self::asort(self::readEntry(['Names'], $displayLocale), $displayLocale);
  63. }
  64. protected static function getPath(): string
  65. {
  66. return Intl::getDataDirectory().'/'.Intl::LOCALE_DIR;
  67. }
  68. }