PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/pyneff/carsharing
PHP | 298 lines | 150 code | 51 blank | 97 comment | 19 complexity | 94514e1e5dfb233ddf347c9b243ecaa4 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0, BSD-3-Clause, BSD-2-Clause
  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. * The ICU data version that ships with Symfony
  15. */
  16. const ICU_DATA_VERSION = '49';
  17. /**
  18. * Caches the countries in different locales
  19. * @var array
  20. */
  21. protected static $countries = array();
  22. /**
  23. * Caches the languages in different locales
  24. * @var array
  25. */
  26. protected static $languages = array();
  27. /**
  28. * Caches the different locales
  29. * @var array
  30. */
  31. protected static $locales = array();
  32. /**
  33. * Returns the country names for a locale
  34. *
  35. * @param string $locale The locale to use for the country names
  36. *
  37. * @return array The country names with their codes as keys
  38. *
  39. * @throws RuntimeException When the resource bundles cannot be loaded
  40. */
  41. public static function getDisplayCountries($locale)
  42. {
  43. if (!isset(self::$countries[$locale])) {
  44. $bundle = \ResourceBundle::create($locale, self::getIcuDataDirectory().'/region');
  45. if (null === $bundle) {
  46. throw new \RuntimeException(sprintf('The country resource bundle could not be loaded for locale "%s"', $locale));
  47. }
  48. $collator = new \Collator($locale);
  49. $countries = array();
  50. $bundleCountries = $bundle->get('Countries') ?: array();
  51. foreach ($bundleCountries as $code => $name) {
  52. // Global countries (f.i. "America") have numeric codes
  53. // Countries have alphabetic codes
  54. // "ZZ" is the code for unknown country
  55. if (ctype_alpha($code) && 'ZZ' !== $code) {
  56. $countries[$code] = $name;
  57. }
  58. }
  59. $fallbackLocale = self::getFallbackLocale($locale);
  60. if (null !== $fallbackLocale) {
  61. $countries = array_merge(self::getDisplayCountries($fallbackLocale), $countries);
  62. }
  63. $collator->asort($countries);
  64. self::$countries[$locale] = $countries;
  65. }
  66. return self::$countries[$locale];
  67. }
  68. /**
  69. * Returns all available country codes
  70. *
  71. * @return array The country codes
  72. * @throws RuntimeException When the resource bundles cannot be loaded
  73. */
  74. public static function getCountries()
  75. {
  76. return array_keys(self::getDisplayCountries(self::getDefault()));
  77. }
  78. /**
  79. * Returns the language names for a locale
  80. *
  81. * @param string $locale The locale to use for the language names
  82. *
  83. * @return array The language names with their codes as keys
  84. *
  85. * @throws RuntimeException When the resource bundles cannot be loaded
  86. */
  87. public static function getDisplayLanguages($locale)
  88. {
  89. if (!isset(self::$languages[$locale])) {
  90. $bundle = \ResourceBundle::create($locale, self::getIcuDataDirectory().'/lang');
  91. if (null === $bundle) {
  92. throw new \RuntimeException(sprintf('The language resource bundle could not be loaded for locale "%s"', $locale));
  93. }
  94. $collator = new \Collator($locale);
  95. $languages = array();
  96. $bundleLanguages = $bundle->get('Languages') ?: array();
  97. foreach ($bundleLanguages as $code => $name) {
  98. // "mul" is the code for multiple languages
  99. if ('mul' !== $code) {
  100. $languages[$code] = $name;
  101. }
  102. }
  103. $fallbackLocale = self::getFallbackLocale($locale);
  104. if (null !== $fallbackLocale) {
  105. $languages = array_merge(self::getDisplayLanguages($fallbackLocale), $languages);
  106. }
  107. $collator->asort($languages);
  108. self::$languages[$locale] = $languages;
  109. }
  110. return self::$languages[$locale];
  111. }
  112. /**
  113. * Returns all available language codes
  114. *
  115. * @return array The language codes
  116. * @throws RuntimeException When the resource bundles cannot be loaded
  117. */
  118. public static function getLanguages()
  119. {
  120. return array_keys(self::getDisplayLanguages(self::getDefault()));
  121. }
  122. /**
  123. * Returns the locale names for a locale
  124. *
  125. * @param string $locale The locale to use for the locale names
  126. * @return array The locale names with their codes as keys
  127. * @throws RuntimeException When the resource bundles cannot be loaded
  128. */
  129. public static function getDisplayLocales($locale)
  130. {
  131. if (!isset(self::$locales[$locale])) {
  132. $bundle = \ResourceBundle::create($locale, self::getIcuDataDirectory().'/names');
  133. if (null === $bundle) {
  134. throw new \RuntimeException(sprintf('The locale resource bundle could not be loaded for locale "%s"', $locale));
  135. }
  136. $collator = new \Collator($locale);
  137. $locales = array();
  138. $bundleLocales = $bundle->get('Locales') ?: array();
  139. foreach ($bundleLocales as $code => $name) {
  140. $locales[$code] = $name;
  141. }
  142. $fallbackLocale = self::getFallbackLocale($locale);
  143. if (null !== $fallbackLocale) {
  144. $locales = array_merge(self::getDisplayLocales($fallbackLocale), $locales);
  145. }
  146. $collator->asort($locales);
  147. self::$locales[$locale] = $locales;
  148. }
  149. return self::$locales[$locale];
  150. }
  151. /**
  152. * Returns all available locale codes
  153. *
  154. * @return array The locale codes
  155. * @throws RuntimeException When the resource bundles cannot be loaded
  156. */
  157. public static function getLocales()
  158. {
  159. return array_keys(self::getDisplayLocales(self::getDefault()));
  160. }
  161. /**
  162. * Returns the ICU version as defined by the intl extension
  163. *
  164. * @return string|null The ICU version
  165. */
  166. public static function getIntlIcuVersion()
  167. {
  168. if (defined('INTL_ICU_VERSION')) {
  169. return INTL_ICU_VERSION;
  170. }
  171. try {
  172. $reflector = new \ReflectionExtension('intl');
  173. } catch (\ReflectionException $e) {
  174. return;
  175. }
  176. ob_start();
  177. $reflector->info();
  178. $output = strip_tags(ob_get_clean());
  179. preg_match('/^ICU version (?:=>)?(.*)$/m', $output, $matches);
  180. return trim($matches[1]);
  181. }
  182. /**
  183. * Returns the ICU Data version as defined by the intl extension
  184. *
  185. * @return string|null The ICU Data version
  186. */
  187. public static function getIntlIcuDataVersion()
  188. {
  189. if (defined('INTL_ICU_DATA_VERSION')) {
  190. return INTL_ICU_DATA_VERSION;
  191. }
  192. try {
  193. $reflector = new \ReflectionExtension('intl');
  194. } catch (\ReflectionException $e) {
  195. return;
  196. }
  197. ob_start();
  198. $reflector->info();
  199. $output = strip_tags(ob_get_clean());
  200. preg_match('/^ICU Data version (?:=>)?(.*)$/m', $output, $matches);
  201. return trim($matches[1]);
  202. }
  203. /**
  204. * Returns the ICU data version that ships with Symfony. If the environment variable USE_INTL_ICU_DATA_VERSION is
  205. * defined, it will try use the ICU data version as defined by the intl extension, if available.
  206. *
  207. * @return string The ICU data version that ships with Symfony
  208. */
  209. public static function getIcuDataVersion()
  210. {
  211. static $dataVersion;
  212. if (null === $dataVersion) {
  213. $dataVersion = self::ICU_DATA_VERSION;
  214. if (getenv('USE_INTL_ICU_DATA_VERSION') && self::getIntlIcuVersion()) {
  215. $dataVersion = self::getIntlIcuVersion();
  216. preg_match('/^(?P<version>[0-9]\.[0-9]|[0-9]{2,})/', $dataVersion, $matches);
  217. $dataVersion = $matches['version'];
  218. }
  219. }
  220. return $dataVersion;
  221. }
  222. /**
  223. * Returns the directory path of the ICU data that ships with Symfony
  224. *
  225. * @return string The path to the ICU data directory
  226. */
  227. public static function getIcuDataDirectory()
  228. {
  229. return __DIR__.'/Resources/data/'.self::getIcuDataVersion();
  230. }
  231. /**
  232. * Returns the fallback locale for a given locale, if any
  233. *
  234. * @param $locale The locale to find the fallback for
  235. * @return string|null The fallback locale, or null if no parent exists
  236. */
  237. protected static function getFallbackLocale($locale)
  238. {
  239. if ($locale === self::getDefault()) {
  240. return null;
  241. }
  242. if (false === $pos = strrpos($locale, '_')) {
  243. return self::getDefault();
  244. }
  245. return substr($locale, 0, $pos);
  246. }
  247. }