Locale.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\Locale;
  11. class Locale extends \Locale
  12. {
  13. /**
  14. * Caches the countries in different locales
  15. * @var array
  16. */
  17. protected static $countries = array();
  18. /**
  19. * Caches the languages in different locales
  20. * @var array
  21. */
  22. protected static $languages = array();
  23. /**
  24. * Caches the different locales
  25. * @var array
  26. */
  27. protected static $locales = array();
  28. /**
  29. * Returns the country names for a locale
  30. *
  31. * @param string $locale The locale to use for the country names
  32. *
  33. * @return array The country names with their codes as keys
  34. *
  35. * @throws RuntimeException When the resource bundles cannot be loaded
  36. */
  37. static public function getDisplayCountries($locale)
  38. {
  39. if (!isset(self::$countries[$locale])) {
  40. $bundle = \ResourceBundle::create($locale, __DIR__.'/Resources/data/region');
  41. if (null === $bundle) {
  42. throw new \RuntimeException('The country resource bundle could not be loaded');
  43. }
  44. $collator = new \Collator($locale);
  45. $countries = array();
  46. foreach ($bundle->get('Countries') as $code => $name) {
  47. // Global countries (f.i. "America") have numeric codes
  48. // Countries have alphabetic codes
  49. // "ZZ" is the code for unknown country
  50. if (ctype_alpha($code) && 'ZZ' !== $code) {
  51. $countries[$code] = $name;
  52. }
  53. }
  54. $collator->asort($countries);
  55. self::$countries[$locale] = $countries;
  56. }
  57. return self::$countries[$locale];
  58. }
  59. /**
  60. * Returns all available country codes
  61. *
  62. * @return array The country codes
  63. * @throws RuntimeException When the resource bundles cannot be loaded
  64. */
  65. static public function getCountries()
  66. {
  67. return array_keys(self::getDisplayCountries(self::getDefault()));
  68. }
  69. /**
  70. * Returns the language names for a locale
  71. *
  72. * @param string $locale The locale to use for the language names
  73. *
  74. * @return array The language names with their codes as keys
  75. *
  76. * @throws RuntimeException When the resource bundles cannot be loaded
  77. */
  78. static public function getDisplayLanguages($locale)
  79. {
  80. if (!isset(self::$languages[$locale])) {
  81. $bundle = \ResourceBundle::create($locale, __DIR__.'/Resources/data/lang');
  82. if (null === $bundle) {
  83. throw new \RuntimeException('The language resource bundle could not be loaded');
  84. }
  85. $collator = new \Collator($locale);
  86. $languages = array();
  87. foreach ($bundle->get('Languages') as $code => $name) {
  88. // "mul" is the code for multiple languages
  89. if ('mul' !== $code) {
  90. $languages[$code] = $name;
  91. }
  92. }
  93. $collator->asort($languages);
  94. self::$languages[$locale] = $languages;
  95. }
  96. return self::$languages[$locale];
  97. }
  98. /**
  99. * Returns all available language codes
  100. *
  101. * @return array The language codes
  102. * @throws RuntimeException When the resource bundles cannot be loaded
  103. */
  104. static public function getLanguages()
  105. {
  106. return array_keys(self::getDisplayLanguages(self::getDefault()));
  107. }
  108. /**
  109. * Returns the locale names for a locale
  110. *
  111. * @param string $locale The locale to use for the locale names
  112. * @return array The locale names with their codes as keys
  113. * @throws RuntimeException When the resource bundles cannot be loaded
  114. */
  115. static public function getDisplayLocales($locale)
  116. {
  117. if (!isset(self::$locales[$locale])) {
  118. $bundle = \ResourceBundle::create($locale, __DIR__.'/Resources/data/names');
  119. if (null === $bundle) {
  120. throw new \RuntimeException('The locale resource bundle could not be loaded');
  121. }
  122. $collator = new \Collator($locale);
  123. $locales = array();
  124. foreach ($bundle->get('Locales') as $code => $name) {
  125. $locales[$code] = $name;
  126. }
  127. $collator->asort($locales);
  128. self::$locales[$locale] = $locales;
  129. }
  130. return self::$locales[$locale];
  131. }
  132. /**
  133. * Returns all available locale codes
  134. *
  135. * @return array The locale codes
  136. * @throws RuntimeException When the resource bundles cannot be loaded
  137. */
  138. static public function getLocales()
  139. {
  140. return array_keys(self::getDisplayLocales(self::getDefault()));
  141. }
  142. }