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