Locale.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\Intl;
  11. /**
  12. * Provides access to locale-related data.
  13. *
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. *
  16. * @internal
  17. */
  18. final class Locale extends \Locale
  19. {
  20. /**
  21. * @var string
  22. */
  23. private static $defaultFallback = 'en';
  24. /**
  25. * Sets the default fallback locale.
  26. *
  27. * The default fallback locale is used as fallback for locales that have no
  28. * fallback otherwise.
  29. *
  30. * @param string $locale The default fallback locale
  31. *
  32. * @see getFallback()
  33. */
  34. public static function setDefaultFallback($locale)
  35. {
  36. self::$defaultFallback = $locale;
  37. }
  38. /**
  39. * Returns the default fallback locale.
  40. *
  41. * @return string The default fallback locale
  42. *
  43. * @see setDefaultFallback()
  44. * @see getFallback()
  45. */
  46. public static function getDefaultFallback()
  47. {
  48. return self::$defaultFallback;
  49. }
  50. /**
  51. * Returns the fallback locale for a given locale.
  52. *
  53. * For example, the fallback of "fr_FR" is "fr". The fallback of "fr" is
  54. * the default fallback locale configured with {@link setDefaultFallback()}.
  55. * The default fallback locale has no fallback.
  56. *
  57. * @param string $locale The ICU locale code to find the fallback for
  58. *
  59. * @return string|null The ICU locale code of the fallback locale, or null
  60. * if no fallback exists
  61. */
  62. public static function getFallback($locale)
  63. {
  64. if (false === $pos = strrpos($locale, '_')) {
  65. if (self::$defaultFallback === $locale) {
  66. return 'root';
  67. }
  68. // Don't return default fallback for "root", "meta" or others
  69. // Normal locales have two or three letters
  70. if (strlen($locale) < 4) {
  71. return self::$defaultFallback;
  72. }
  73. return;
  74. }
  75. return substr($locale, 0, $pos);
  76. }
  77. /**
  78. * This class must not be instantiated.
  79. */
  80. private function __construct()
  81. {
  82. }
  83. }