Locale.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. use Symfony\Component\Icu\IcuData;
  12. use Symfony\Component\Intl\Intl;
  13. /**
  14. * Helper class for dealing with locale strings.
  15. *
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. *
  18. * @deprecated Deprecated since version 2.3, to be removed in 3.0. Use
  19. * {@link \Locale} and {@link \Symfony\Component\Intl\Intl} instead.
  20. */
  21. class Locale extends \Locale
  22. {
  23. /**
  24. * Caches the countries in different locales
  25. * @var array
  26. */
  27. protected static $countries = array();
  28. /**
  29. * Caches the languages in different locales
  30. * @var array
  31. */
  32. protected static $languages = array();
  33. /**
  34. * Caches the different locales
  35. * @var array
  36. */
  37. protected static $locales = array();
  38. /**
  39. * Returns the country names for a locale
  40. *
  41. * @param string $locale The locale to use for the country names
  42. *
  43. * @return array The country names with their codes as keys
  44. *
  45. * @throws \RuntimeException When the resource bundles cannot be loaded
  46. */
  47. public static function getDisplayCountries($locale)
  48. {
  49. if (!isset(self::$countries[$locale])) {
  50. self::$countries[$locale] = Intl::getRegionBundle()->getCountryNames($locale);
  51. }
  52. return self::$countries[$locale];
  53. }
  54. /**
  55. * Returns all available country codes
  56. *
  57. * @return array The country codes
  58. * @throws \RuntimeException When the resource bundles cannot be loaded
  59. */
  60. public static function getCountries()
  61. {
  62. return array_keys(self::getDisplayCountries(self::getDefault()));
  63. }
  64. /**
  65. * Returns the language names for a locale
  66. *
  67. * @param string $locale The locale to use for the language names
  68. *
  69. * @return array The language names with their codes as keys
  70. *
  71. * @throws \RuntimeException When the resource bundles cannot be loaded
  72. */
  73. public static function getDisplayLanguages($locale)
  74. {
  75. if (!isset(self::$languages[$locale])) {
  76. self::$languages[$locale] = Intl::getLanguageBundle()->getLanguageNames($locale);
  77. }
  78. return self::$languages[$locale];
  79. }
  80. /**
  81. * Returns all available language codes
  82. *
  83. * @return array The language codes
  84. * @throws \RuntimeException When the resource bundles cannot be loaded
  85. */
  86. public static function getLanguages()
  87. {
  88. return array_keys(self::getDisplayLanguages(self::getDefault()));
  89. }
  90. /**
  91. * Returns the locale names for a locale
  92. *
  93. * @param string $locale The locale to use for the locale names
  94. *
  95. * @return array The locale names with their codes as keys
  96. *
  97. * @throws \RuntimeException When the resource bundles cannot be loaded
  98. */
  99. public static function getDisplayLocales($locale)
  100. {
  101. if (!isset(self::$locales[$locale])) {
  102. self::$locales[$locale] = Intl::getLocaleBundle()->getLocaleNames($locale);
  103. }
  104. return self::$locales[$locale];
  105. }
  106. /**
  107. * Returns all available locale codes
  108. *
  109. * @return array The locale codes
  110. * @throws \RuntimeException When the resource bundles cannot be loaded
  111. */
  112. public static function getLocales()
  113. {
  114. return array_keys(self::getDisplayLocales(self::getDefault()));
  115. }
  116. /**
  117. * Returns the ICU version as defined by the intl extension
  118. *
  119. * @return string|null The ICU version
  120. */
  121. public static function getIntlIcuVersion()
  122. {
  123. return Intl::getIcuVersion();
  124. }
  125. /**
  126. * Returns the ICU Data version as defined by the intl extension
  127. *
  128. * @return string|null The ICU Data version
  129. */
  130. public static function getIntlIcuDataVersion()
  131. {
  132. return Intl::getIcuDataVersion();
  133. }
  134. /**
  135. * Returns the ICU data version that ships with Symfony. If the environment variable USE_INTL_ICU_DATA_VERSION is
  136. * defined, it will try use the ICU data version as defined by the intl extension, if available.
  137. *
  138. * @return string The ICU data version that ships with Symfony
  139. */
  140. public static function getIcuDataVersion()
  141. {
  142. return Intl::getIcuDataVersion();
  143. }
  144. /**
  145. * Returns the directory path of the ICU data that ships with Symfony
  146. *
  147. * @return string The path to the ICU data directory
  148. */
  149. public static function getIcuDataDirectory()
  150. {
  151. return IcuData::getResourceDirectory();
  152. }
  153. /**
  154. * Returns the fallback locale for a given locale, if any
  155. *
  156. * @param string $locale The locale to find the fallback for.
  157. *
  158. * @return string|null The fallback locale, or null if no parent exists
  159. */
  160. protected static function getFallbackLocale($locale)
  161. {
  162. if (false === $pos = strrpos($locale, '_')) {
  163. return null;
  164. }
  165. return substr($locale, 0, $pos);
  166. }
  167. }