Locale.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. $fallbackLocale = self::getFallbackLocale($locale);
  55. if (null !== $fallbackLocale) {
  56. $countries = array_merge(self::getDisplayCountries($fallbackLocale), $countries);
  57. }
  58. $collator->asort($countries);
  59. self::$countries[$locale] = $countries;
  60. }
  61. return self::$countries[$locale];
  62. }
  63. /**
  64. * Returns all available country codes
  65. *
  66. * @return array The country codes
  67. * @throws RuntimeException When the resource bundles cannot be loaded
  68. */
  69. static public function getCountries()
  70. {
  71. return array_keys(self::getDisplayCountries(self::getDefault()));
  72. }
  73. /**
  74. * Returns the language names for a locale
  75. *
  76. * @param string $locale The locale to use for the language names
  77. *
  78. * @return array The language names with their codes as keys
  79. *
  80. * @throws RuntimeException When the resource bundles cannot be loaded
  81. */
  82. static public function getDisplayLanguages($locale)
  83. {
  84. if (!isset(self::$languages[$locale])) {
  85. $bundle = \ResourceBundle::create($locale, __DIR__.'/Resources/data/lang');
  86. if (null === $bundle) {
  87. throw new \RuntimeException('The language resource bundle could not be loaded');
  88. }
  89. $collator = new \Collator($locale);
  90. $languages = array();
  91. foreach ($bundle->get('Languages') as $code => $name) {
  92. // "mul" is the code for multiple languages
  93. if ('mul' !== $code) {
  94. $languages[$code] = $name;
  95. }
  96. }
  97. $fallbackLocale = self::getFallbackLocale($locale);
  98. if (null !== $fallbackLocale) {
  99. $languages = array_merge(self::getDisplayLanguages($fallbackLocale), $languages);
  100. }
  101. $collator->asort($languages);
  102. self::$languages[$locale] = $languages;
  103. }
  104. return self::$languages[$locale];
  105. }
  106. /**
  107. * Returns all available language codes
  108. *
  109. * @return array The language codes
  110. * @throws RuntimeException When the resource bundles cannot be loaded
  111. */
  112. static public function getLanguages()
  113. {
  114. return array_keys(self::getDisplayLanguages(self::getDefault()));
  115. }
  116. /**
  117. * Returns the locale names for a locale
  118. *
  119. * @param string $locale The locale to use for the locale names
  120. * @return array The locale names with their codes as keys
  121. * @throws RuntimeException When the resource bundles cannot be loaded
  122. */
  123. static public function getDisplayLocales($locale)
  124. {
  125. if (!isset(self::$locales[$locale])) {
  126. $bundle = \ResourceBundle::create($locale, __DIR__.'/Resources/data/names');
  127. if (null === $bundle) {
  128. throw new \RuntimeException('The locale resource bundle could not be loaded');
  129. }
  130. $collator = new \Collator($locale);
  131. $locales = array();
  132. foreach ($bundle->get('Locales') as $code => $name) {
  133. $locales[$code] = $name;
  134. }
  135. $fallbackLocale = self::getFallbackLocale($locale);
  136. if (null !== $fallbackLocale) {
  137. $locales = array_merge(self::getDisplayLocales($fallbackLocale), $locales);
  138. }
  139. $collator->asort($locales);
  140. self::$locales[$locale] = $locales;
  141. }
  142. return self::$locales[$locale];
  143. }
  144. /**
  145. * Returns all available locale codes
  146. *
  147. * @return array The locale codes
  148. * @throws RuntimeException When the resource bundles cannot be loaded
  149. */
  150. static public function getLocales()
  151. {
  152. return array_keys(self::getDisplayLocales(self::getDefault()));
  153. }
  154. /**
  155. * Returns the fallback locale for a given locale, if any
  156. *
  157. * @param $locale The locale to find the fallback for
  158. * @return string|null The fallback locale, or null if no parent exists
  159. */
  160. static protected function getFallbackLocale($locale)
  161. {
  162. if ($locale === self::getDefault()) {
  163. return null;
  164. }
  165. if (false === $pos = strrpos($locale, '_')) {
  166. return self::getDefault();
  167. }
  168. return substr($locale, 0, $pos);
  169. }
  170. }