Exceptions
3 years ago
Lang
3 years ago
List
3 years ago
Traits
3 years ago
AbstractTranslator.php
3 years ago
Carbon.php
4 years ago
CarbonConverterInterface.php
4 years ago
CarbonImmutable.php
4 years ago
CarbonInterface.php
3 years ago
CarbonInterval.php
3 years ago
CarbonPeriod.php
3 years ago
CarbonTimeZone.php
3 years ago
Factory.php
4 years ago
FactoryImmutable.php
4 years ago
Language.php
4 years ago
Translator.php
4 years ago
TranslatorImmutable.php
4 years ago
TranslatorStrongTypeInterface.php
4 years ago
index.php
3 years ago
Language.php
151 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Carbon; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use JsonSerializable; |
| 5 | use ReturnTypeWillChange; |
| 6 | class Language implements JsonSerializable |
| 7 | { |
| 8 | protected static $languagesNames; |
| 9 | protected static $regionsNames; |
| 10 | protected $id; |
| 11 | protected $code; |
| 12 | protected $variant; |
| 13 | protected $region; |
| 14 | protected $names; |
| 15 | protected $isoName; |
| 16 | protected $nativeName; |
| 17 | public function __construct(string $id) |
| 18 | { |
| 19 | $this->id = \str_replace('-', '_', $id); |
| 20 | $parts = \explode('_', $this->id); |
| 21 | $this->code = $parts[0]; |
| 22 | if (isset($parts[1])) { |
| 23 | if (!\preg_match('/^[A-Z]+$/', $parts[1])) { |
| 24 | $this->variant = $parts[1]; |
| 25 | $parts[1] = $parts[2] ?? null; |
| 26 | } |
| 27 | if ($parts[1]) { |
| 28 | $this->region = $parts[1]; |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | public static function all() |
| 33 | { |
| 34 | if (!static::$languagesNames) { |
| 35 | static::$languagesNames = (require __DIR__ . '/List/languages.php'); |
| 36 | } |
| 37 | return static::$languagesNames; |
| 38 | } |
| 39 | public static function regions() |
| 40 | { |
| 41 | if (!static::$regionsNames) { |
| 42 | static::$regionsNames = (require __DIR__ . '/List/regions.php'); |
| 43 | } |
| 44 | return static::$regionsNames; |
| 45 | } |
| 46 | public function getNames() : array |
| 47 | { |
| 48 | if (!$this->names) { |
| 49 | $this->names = static::all()[$this->code] ?? ['isoName' => $this->code, 'nativeName' => $this->code]; |
| 50 | } |
| 51 | return $this->names; |
| 52 | } |
| 53 | public function getId() : string |
| 54 | { |
| 55 | return $this->id; |
| 56 | } |
| 57 | public function getCode() : string |
| 58 | { |
| 59 | return $this->code; |
| 60 | } |
| 61 | public function getVariant() : ?string |
| 62 | { |
| 63 | return $this->variant; |
| 64 | } |
| 65 | public function getVariantName() : ?string |
| 66 | { |
| 67 | if ($this->variant === 'Latn') { |
| 68 | return 'Latin'; |
| 69 | } |
| 70 | if ($this->variant === 'Cyrl') { |
| 71 | return 'Cyrillic'; |
| 72 | } |
| 73 | return $this->variant; |
| 74 | } |
| 75 | public function getRegion() : ?string |
| 76 | { |
| 77 | return $this->region; |
| 78 | } |
| 79 | public function getRegionName() : ?string |
| 80 | { |
| 81 | return $this->region ? static::regions()[$this->region] ?? $this->region : null; |
| 82 | } |
| 83 | public function getFullIsoName() : string |
| 84 | { |
| 85 | if (!$this->isoName) { |
| 86 | $this->isoName = $this->getNames()['isoName']; |
| 87 | } |
| 88 | return $this->isoName; |
| 89 | } |
| 90 | public function setIsoName(string $isoName) : self |
| 91 | { |
| 92 | $this->isoName = $isoName; |
| 93 | return $this; |
| 94 | } |
| 95 | public function getFullNativeName() : string |
| 96 | { |
| 97 | if (!$this->nativeName) { |
| 98 | $this->nativeName = $this->getNames()['nativeName']; |
| 99 | } |
| 100 | return $this->nativeName; |
| 101 | } |
| 102 | public function setNativeName(string $nativeName) : self |
| 103 | { |
| 104 | $this->nativeName = $nativeName; |
| 105 | return $this; |
| 106 | } |
| 107 | public function getIsoName() : string |
| 108 | { |
| 109 | $name = $this->getFullIsoName(); |
| 110 | return \trim(\strstr($name, ',', \true) ?: $name); |
| 111 | } |
| 112 | public function getNativeName() : string |
| 113 | { |
| 114 | $name = $this->getFullNativeName(); |
| 115 | return \trim(\strstr($name, ',', \true) ?: $name); |
| 116 | } |
| 117 | public function getIsoDescription() |
| 118 | { |
| 119 | $region = $this->getRegionName(); |
| 120 | $variant = $this->getVariantName(); |
| 121 | return $this->getIsoName() . ($region ? ' (' . $region . ')' : '') . ($variant ? ' (' . $variant . ')' : ''); |
| 122 | } |
| 123 | public function getNativeDescription() |
| 124 | { |
| 125 | $region = $this->getRegionName(); |
| 126 | $variant = $this->getVariantName(); |
| 127 | return $this->getNativeName() . ($region ? ' (' . $region . ')' : '') . ($variant ? ' (' . $variant . ')' : ''); |
| 128 | } |
| 129 | public function getFullIsoDescription() |
| 130 | { |
| 131 | $region = $this->getRegionName(); |
| 132 | $variant = $this->getVariantName(); |
| 133 | return $this->getFullIsoName() . ($region ? ' (' . $region . ')' : '') . ($variant ? ' (' . $variant . ')' : ''); |
| 134 | } |
| 135 | public function getFullNativeDescription() |
| 136 | { |
| 137 | $region = $this->getRegionName(); |
| 138 | $variant = $this->getVariantName(); |
| 139 | return $this->getFullNativeName() . ($region ? ' (' . $region . ')' : '') . ($variant ? ' (' . $variant . ')' : ''); |
| 140 | } |
| 141 | public function __toString() |
| 142 | { |
| 143 | return $this->getId(); |
| 144 | } |
| 145 | #[\ReturnTypeWillChange] |
| 146 | public function jsonSerialize() |
| 147 | { |
| 148 | return $this->getIsoDescription(); |
| 149 | } |
| 150 | } |
| 151 |