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