/vendor/symfony/src/Symfony/Component/Locale/Locale.php

https://github.com/israelnoguera/parejas · PHP · 221 lines · 107 code · 37 blank · 77 comment · 11 complexity · bf7ac1213f152778815ab79edadd603b MD5 · raw file

  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. /**
  143. * Returns the ICU version
  144. *
  145. * @return string|null The ICU version
  146. */
  147. static public function getIcuVersion()
  148. {
  149. if (defined('INTL_ICU_VERSION')) {
  150. return INTL_ICU_VERSION;
  151. }
  152. try {
  153. $reflector = new \ReflectionExtension('intl');
  154. } catch (\ReflectionException $e) {
  155. return;
  156. }
  157. ob_start();
  158. $reflector->info();
  159. $output = strip_tags(ob_get_clean());
  160. preg_match('/^ICU version (?:=>)?(.*)$/m', $output, $matches);
  161. return trim($matches[1]);
  162. }
  163. /**
  164. * Returns the ICU Data version
  165. *
  166. * @return string|null The ICU Data version
  167. */
  168. static public function getIcuDataVersion()
  169. {
  170. if (defined('INTL_ICU_DATA_VERSION')) {
  171. return INTL_ICU_DATA_VERSION;
  172. }
  173. try {
  174. $reflector = new \ReflectionExtension('intl');
  175. } catch (\ReflectionException $e) {
  176. return;
  177. }
  178. ob_start();
  179. $reflector->info();
  180. $output = strip_tags(ob_get_clean());
  181. preg_match('/^ICU Data version (?:=>)?(.*)$/m', $output, $matches);
  182. return trim($matches[1]);
  183. }
  184. }