-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Locale.php
112 lines (94 loc) · 3.13 KB
/
Locale.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Intl;
if (!class_exists(\Locale::class)) {
throw new \LogicException(sprintf('You cannot use the "%s\Locale" class as the "intl" extension is not installed. See https://php.net/intl.', __NAMESPACE__));
}
/**
* Provides access to locale-related data.
*
* @author Bernhard Schussek <[email protected]>
*
* @internal
*/
final class Locale extends \Locale
{
private static ?string $defaultFallback = 'en';
/**
* Sets the default fallback locale.
*
* The default fallback locale is used as fallback for locales that have no
* fallback otherwise.
*
* @see getFallback()
*/
public static function setDefaultFallback(?string $locale): void
{
self::$defaultFallback = $locale;
}
/**
* Returns the default fallback locale.
*
* @see setDefaultFallback()
* @see getFallback()
*/
public static function getDefaultFallback(): ?string
{
return self::$defaultFallback;
}
/**
* Returns the fallback locale for a given locale.
*
* For example, the fallback of "fr_FR" is "fr". The fallback of "fr" is
* the default fallback locale configured with {@link setDefaultFallback()}.
* The default fallback locale has no fallback.
*
* @return string|null The ICU locale code of the fallback locale, or null
* if no fallback exists
*/
public static function getFallback(string $locale): ?string
{
if (\function_exists('locale_parse')) {
$localeSubTags = locale_parse($locale) ?? ['language' => $locale];
if (1 === \count($localeSubTags)) {
if ('root' !== self::$defaultFallback && self::$defaultFallback === $localeSubTags['language']) {
return 'root';
}
// Don't return default fallback for "root", "meta" or others
// Normal locales have two or three letters
if (\strlen($locale) < 4) {
return self::$defaultFallback;
}
return null;
}
array_pop($localeSubTags);
$fallback = locale_compose($localeSubTags);
return false !== $fallback ? $fallback : null;
}
if (false !== $pos = strrpos($locale, '_')) {
return substr($locale, 0, $pos);
}
if (false !== $pos = strrpos($locale, '-')) {
return substr($locale, 0, $pos);
}
if ('root' !== self::$defaultFallback && self::$defaultFallback === $locale) {
return 'root';
}
// Don't return default fallback for "root", "meta" or others
// Normal locales have two or three letters
return \strlen($locale) < 4 ? self::$defaultFallback : null;
}
/**
* This class must not be instantiated.
*/
private function __construct()
{
}
}