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

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