Locale.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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|null
  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|null $locale The default fallback locale
  31. *
  32. * @see getFallback()
  33. */
  34. public static function setDefaultFallback(?string $locale)
  35. {
  36. self::$defaultFallback = $locale;
  37. }
  38. /**
  39. * Returns the default fallback locale.
  40. *
  41. * @return string|null The default fallback locale
  42. *
  43. * @see setDefaultFallback()
  44. * @see getFallback()
  45. */
  46. public static function getDefaultFallback(): ?string
  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. * @return string|null The ICU locale code of the fallback locale, or null
  58. * if no fallback exists
  59. */
  60. public static function getFallback(string $locale): ?string
  61. {
  62. if (\function_exists('locale_parse')) {
  63. $localeSubTags = locale_parse($locale) ?? ['language' => $locale];
  64. if (1 === \count($localeSubTags)) {
  65. if ('root' !== self::$defaultFallback && self::$defaultFallback === $localeSubTags['language']) {
  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 null;
  74. }
  75. array_pop($localeSubTags);
  76. $fallback = locale_compose($localeSubTags);
  77. return false !== $fallback ? $fallback : null;
  78. }
  79. if (false !== $pos = strrpos($locale, '_')) {
  80. return substr($locale, 0, $pos);
  81. }
  82. if (false !== $pos = strrpos($locale, '-')) {
  83. return substr($locale, 0, $pos);
  84. }
  85. if ('root' !== self::$defaultFallback && self::$defaultFallback === $locale) {
  86. return 'root';
  87. }
  88. // Don't return default fallback for "root", "meta" or others
  89. // Normal locales have two or three letters
  90. return \strlen($locale) < 4 ? self::$defaultFallback : null;
  91. }
  92. /**
  93. * This class must not be instantiated.
  94. */
  95. private function __construct()
  96. {
  97. }
  98. }