Cli
2 months ago
Exceptions
2 months ago
Lang
2 months ago
Laravel
2 months ago
List
2 months ago
MessageFormatter
2 months ago
PHPStan
2 months ago
Traits
2 months ago
AbstractTranslator.php
2 months ago
Carbon.php
2 months ago
CarbonConverterInterface.php
2 months ago
CarbonImmutable.php
2 months ago
CarbonInterface.php
2 months ago
CarbonInterval.php
2 months ago
CarbonPeriod.php
2 months ago
CarbonPeriodImmutable.php
2 months ago
CarbonTimeZone.php
2 months ago
Factory.php
2 months ago
FactoryImmutable.php
2 months ago
Language.php
2 months ago
Translator.php
2 months ago
TranslatorImmutable.php
2 months ago
TranslatorStrongTypeInterface.php
2 months ago
Language.php
292 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This file is part of the Carbon package. |
| 5 | * |
| 6 | * (c) Brian Nesbitt <brian@nesbot.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace ProfilePressVendor\Carbon; |
| 12 | |
| 13 | use JsonSerializable; |
| 14 | use ReturnTypeWillChange; |
| 15 | class Language implements JsonSerializable |
| 16 | { |
| 17 | /** |
| 18 | * @var array |
| 19 | */ |
| 20 | protected static $languagesNames; |
| 21 | /** |
| 22 | * @var array |
| 23 | */ |
| 24 | protected static $regionsNames; |
| 25 | /** |
| 26 | * @var string |
| 27 | */ |
| 28 | protected $id; |
| 29 | /** |
| 30 | * @var string |
| 31 | */ |
| 32 | protected $code; |
| 33 | /** |
| 34 | * @var string|null |
| 35 | */ |
| 36 | protected $variant; |
| 37 | /** |
| 38 | * @var string|null |
| 39 | */ |
| 40 | protected $region; |
| 41 | /** |
| 42 | * @var array |
| 43 | */ |
| 44 | protected $names; |
| 45 | /** |
| 46 | * @var string |
| 47 | */ |
| 48 | protected $isoName; |
| 49 | /** |
| 50 | * @var string |
| 51 | */ |
| 52 | protected $nativeName; |
| 53 | public function __construct(string $id) |
| 54 | { |
| 55 | $this->id = str_replace('-', '_', $id); |
| 56 | $parts = explode('_', $this->id); |
| 57 | $this->code = $parts[0]; |
| 58 | if (isset($parts[1])) { |
| 59 | if (!preg_match('/^[A-Z]+$/', $parts[1])) { |
| 60 | $this->variant = $parts[1]; |
| 61 | $parts[1] = $parts[2] ?? null; |
| 62 | } |
| 63 | if ($parts[1]) { |
| 64 | $this->region = $parts[1]; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | /** |
| 69 | * Get the list of the known languages. |
| 70 | * |
| 71 | * @return array |
| 72 | */ |
| 73 | public static function all() |
| 74 | { |
| 75 | if (!static::$languagesNames) { |
| 76 | static::$languagesNames = require __DIR__ . '/List/languages.php'; |
| 77 | } |
| 78 | return static::$languagesNames; |
| 79 | } |
| 80 | /** |
| 81 | * Get the list of the known regions. |
| 82 | * |
| 83 | * @return array |
| 84 | */ |
| 85 | public static function regions() |
| 86 | { |
| 87 | if (!static::$regionsNames) { |
| 88 | static::$regionsNames = require __DIR__ . '/List/regions.php'; |
| 89 | } |
| 90 | return static::$regionsNames; |
| 91 | } |
| 92 | /** |
| 93 | * Get both isoName and nativeName as an array. |
| 94 | * |
| 95 | * @return array |
| 96 | */ |
| 97 | public function getNames(): array |
| 98 | { |
| 99 | if (!$this->names) { |
| 100 | $this->names = static::all()[$this->code] ?? ['isoName' => $this->code, 'nativeName' => $this->code]; |
| 101 | } |
| 102 | return $this->names; |
| 103 | } |
| 104 | /** |
| 105 | * Returns the original locale ID. |
| 106 | * |
| 107 | * @return string |
| 108 | */ |
| 109 | public function getId(): string |
| 110 | { |
| 111 | return $this->id; |
| 112 | } |
| 113 | /** |
| 114 | * Returns the code of the locale "en"/"fr". |
| 115 | * |
| 116 | * @return string |
| 117 | */ |
| 118 | public function getCode(): string |
| 119 | { |
| 120 | return $this->code; |
| 121 | } |
| 122 | /** |
| 123 | * Returns the variant code such as cyrl/latn. |
| 124 | * |
| 125 | * @return string|null |
| 126 | */ |
| 127 | public function getVariant(): ?string |
| 128 | { |
| 129 | return $this->variant; |
| 130 | } |
| 131 | /** |
| 132 | * Returns the variant such as Cyrillic/Latin. |
| 133 | * |
| 134 | * @return string|null |
| 135 | */ |
| 136 | public function getVariantName(): ?string |
| 137 | { |
| 138 | if ($this->variant === 'Latn') { |
| 139 | return 'Latin'; |
| 140 | } |
| 141 | if ($this->variant === 'Cyrl') { |
| 142 | return 'Cyrillic'; |
| 143 | } |
| 144 | return $this->variant; |
| 145 | } |
| 146 | /** |
| 147 | * Returns the region part of the locale. |
| 148 | * |
| 149 | * @return string|null |
| 150 | */ |
| 151 | public function getRegion(): ?string |
| 152 | { |
| 153 | return $this->region; |
| 154 | } |
| 155 | /** |
| 156 | * Returns the region name for the current language. |
| 157 | * |
| 158 | * @return string|null |
| 159 | */ |
| 160 | public function getRegionName(): ?string |
| 161 | { |
| 162 | return $this->region ? static::regions()[$this->region] ?? $this->region : null; |
| 163 | } |
| 164 | /** |
| 165 | * Returns the long ISO language name. |
| 166 | * |
| 167 | * @return string |
| 168 | */ |
| 169 | public function getFullIsoName(): string |
| 170 | { |
| 171 | if (!$this->isoName) { |
| 172 | $this->isoName = $this->getNames()['isoName']; |
| 173 | } |
| 174 | return $this->isoName; |
| 175 | } |
| 176 | /** |
| 177 | * Set the ISO language name. |
| 178 | * |
| 179 | * @param string $isoName |
| 180 | */ |
| 181 | public function setIsoName(string $isoName): self |
| 182 | { |
| 183 | $this->isoName = $isoName; |
| 184 | return $this; |
| 185 | } |
| 186 | /** |
| 187 | * Return the full name of the language in this language. |
| 188 | * |
| 189 | * @return string |
| 190 | */ |
| 191 | public function getFullNativeName(): string |
| 192 | { |
| 193 | if (!$this->nativeName) { |
| 194 | $this->nativeName = $this->getNames()['nativeName']; |
| 195 | } |
| 196 | return $this->nativeName; |
| 197 | } |
| 198 | /** |
| 199 | * Set the name of the language in this language. |
| 200 | * |
| 201 | * @param string $nativeName |
| 202 | */ |
| 203 | public function setNativeName(string $nativeName): self |
| 204 | { |
| 205 | $this->nativeName = $nativeName; |
| 206 | return $this; |
| 207 | } |
| 208 | /** |
| 209 | * Returns the short ISO language name. |
| 210 | * |
| 211 | * @return string |
| 212 | */ |
| 213 | public function getIsoName(): string |
| 214 | { |
| 215 | $name = $this->getFullIsoName(); |
| 216 | return trim(strstr($name, ',', \true) ?: $name); |
| 217 | } |
| 218 | /** |
| 219 | * Get the short name of the language in this language. |
| 220 | * |
| 221 | * @return string |
| 222 | */ |
| 223 | public function getNativeName(): string |
| 224 | { |
| 225 | $name = $this->getFullNativeName(); |
| 226 | return trim(strstr($name, ',', \true) ?: $name); |
| 227 | } |
| 228 | /** |
| 229 | * Get a string with short ISO name, region in parentheses if applicable, variant in parentheses if applicable. |
| 230 | * |
| 231 | * @return string |
| 232 | */ |
| 233 | public function getIsoDescription() |
| 234 | { |
| 235 | $region = $this->getRegionName(); |
| 236 | $variant = $this->getVariantName(); |
| 237 | return $this->getIsoName() . ($region ? ' (' . $region . ')' : '') . ($variant ? ' (' . $variant . ')' : ''); |
| 238 | } |
| 239 | /** |
| 240 | * Get a string with short native name, region in parentheses if applicable, variant in parentheses if applicable. |
| 241 | * |
| 242 | * @return string |
| 243 | */ |
| 244 | public function getNativeDescription() |
| 245 | { |
| 246 | $region = $this->getRegionName(); |
| 247 | $variant = $this->getVariantName(); |
| 248 | return $this->getNativeName() . ($region ? ' (' . $region . ')' : '') . ($variant ? ' (' . $variant . ')' : ''); |
| 249 | } |
| 250 | /** |
| 251 | * Get a string with long ISO name, region in parentheses if applicable, variant in parentheses if applicable. |
| 252 | * |
| 253 | * @return string |
| 254 | */ |
| 255 | public function getFullIsoDescription() |
| 256 | { |
| 257 | $region = $this->getRegionName(); |
| 258 | $variant = $this->getVariantName(); |
| 259 | return $this->getFullIsoName() . ($region ? ' (' . $region . ')' : '') . ($variant ? ' (' . $variant . ')' : ''); |
| 260 | } |
| 261 | /** |
| 262 | * Get a string with long native name, region in parentheses if applicable, variant in parentheses if applicable. |
| 263 | * |
| 264 | * @return string |
| 265 | */ |
| 266 | public function getFullNativeDescription() |
| 267 | { |
| 268 | $region = $this->getRegionName(); |
| 269 | $variant = $this->getVariantName(); |
| 270 | return $this->getFullNativeName() . ($region ? ' (' . $region . ')' : '') . ($variant ? ' (' . $variant . ')' : ''); |
| 271 | } |
| 272 | /** |
| 273 | * Returns the original locale ID. |
| 274 | * |
| 275 | * @return string |
| 276 | */ |
| 277 | public function __toString() |
| 278 | { |
| 279 | return $this->getId(); |
| 280 | } |
| 281 | /** |
| 282 | * Get a string with short ISO name, region in parentheses if applicable, variant in parentheses if applicable. |
| 283 | * |
| 284 | * @return string |
| 285 | */ |
| 286 | #[ReturnTypeWillChange] |
| 287 | public function jsonSerialize() |
| 288 | { |
| 289 | return $this->getIsoDescription(); |
| 290 | } |
| 291 | } |
| 292 |