Language.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /**
  3. * This file is part of the Carbon package.
  4. *
  5. * (c) Brian Nesbitt <brian@nesbot.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Carbon;
  11. use JsonSerializable;
  12. use ReturnTypeWillChange;
  13. class Language implements JsonSerializable
  14. {
  15. /**
  16. * @var array
  17. */
  18. protected static $languagesNames;
  19. /**
  20. * @var array
  21. */
  22. protected static $regionsNames;
  23. /**
  24. * @var string
  25. */
  26. protected $id;
  27. /**
  28. * @var string
  29. */
  30. protected $code;
  31. /**
  32. * @var string|null
  33. */
  34. protected $variant;
  35. /**
  36. * @var string|null
  37. */
  38. protected $region;
  39. /**
  40. * @var array
  41. */
  42. protected $names;
  43. /**
  44. * @var string
  45. */
  46. protected $isoName;
  47. /**
  48. * @var string
  49. */
  50. protected $nativeName;
  51. public function __construct(string $id)
  52. {
  53. $this->id = str_replace('-', '_', $id);
  54. $parts = explode('_', $this->id);
  55. $this->code = $parts[0];
  56. if (isset($parts[1])) {
  57. if (!preg_match('/^[A-Z]+$/', $parts[1])) {
  58. $this->variant = $parts[1];
  59. $parts[1] = $parts[2] ?? null;
  60. }
  61. if ($parts[1]) {
  62. $this->region = $parts[1];
  63. }
  64. }
  65. }
  66. /**
  67. * Get the list of the known languages.
  68. *
  69. * @return array
  70. */
  71. public static function all()
  72. {
  73. if (!static::$languagesNames) {
  74. static::$languagesNames = require __DIR__.'/List/languages.php';
  75. }
  76. return static::$languagesNames;
  77. }
  78. /**
  79. * Get the list of the known regions.
  80. *
  81. * @return array
  82. */
  83. public static function regions()
  84. {
  85. if (!static::$regionsNames) {
  86. static::$regionsNames = require __DIR__.'/List/regions.php';
  87. }
  88. return static::$regionsNames;
  89. }
  90. /**
  91. * Get both isoName and nativeName as an array.
  92. *
  93. * @return array
  94. */
  95. public function getNames(): array
  96. {
  97. if (!$this->names) {
  98. $this->names = static::all()[$this->code] ?? [
  99. 'isoName' => $this->code,
  100. 'nativeName' => $this->code,
  101. ];
  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. }