Languages.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\Intl;
  11. use Symfony\Component\Intl\Exception\MissingResourceException;
  12. /**
  13. * Gives access to language-related ICU data.
  14. *
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. * @author Roland Franssen <franssen.roland@gmail.com>
  17. */
  18. final class Languages extends ResourceBundle
  19. {
  20. /**
  21. * Returns all available languages as two-letter codes.
  22. *
  23. * Languages are returned as lowercase ISO 639-1 two-letter language codes.
  24. * For languages that don't have a two-letter code, the ISO 639-2
  25. * three-letter code is used instead.
  26. *
  27. * A full table of ISO 639 language codes can be found here:
  28. * http://www-01.sil.org/iso639-3/codes.asp
  29. *
  30. * @return string[] an array of canonical ISO 639-1 language codes
  31. */
  32. public static function getLanguageCodes(): array
  33. {
  34. return self::readEntry(['Languages'], 'meta');
  35. }
  36. public static function exists(string $language): bool
  37. {
  38. try {
  39. self::readEntry(['Names', $language]);
  40. return true;
  41. } catch (MissingResourceException $e) {
  42. return false;
  43. }
  44. }
  45. /**
  46. * Gets the language name from its alpha2 code.
  47. *
  48. * A full locale may be passed to obtain a more localized language name, e.g. "American English" for "en_US".
  49. *
  50. * @throws MissingResourceException if the language code does not exist
  51. */
  52. public static function getName(string $language, string $displayLocale = null): string
  53. {
  54. try {
  55. return self::readEntry(['Names', $language], $displayLocale);
  56. } catch (MissingResourceException $e) {
  57. try {
  58. return self::readEntry(['LocalizedNames', $language], $displayLocale);
  59. } catch (MissingResourceException $e) {
  60. if (false !== $i = strrpos($language, '_')) {
  61. return self::getName(substr($language, 0, $i), $displayLocale);
  62. }
  63. throw $e;
  64. }
  65. }
  66. }
  67. /**
  68. * Gets the list of language names indexed with alpha2 codes as keys.
  69. *
  70. * @return string[]
  71. */
  72. public static function getNames(string $displayLocale = null): array
  73. {
  74. return self::asort(self::readEntry(['Names'], $displayLocale), $displayLocale);
  75. }
  76. /**
  77. * Returns the ISO 639-2 three-letter code of a language, given a two-letter code.
  78. *
  79. * @throws MissingResourceException if the language has no corresponding three-letter code
  80. */
  81. public static function getAlpha3Code(string $language): string
  82. {
  83. return self::readEntry(['Alpha2ToAlpha3', $language], 'meta');
  84. }
  85. /**
  86. * Returns the ISO 639-1 two-letter code of a language, given a three letter code.
  87. *
  88. * @throws MissingResourceException if the language has no corresponding three-letter code
  89. */
  90. public static function getAlpha2Code(string $language): string
  91. {
  92. return self::readEntry(['Alpha3ToAlpha2', $language], 'meta');
  93. }
  94. /**
  95. * Returns all available languages as three-letter codes.
  96. *
  97. * Languages are returned as lowercase ISO 639-2 three-letter language codes.
  98. *
  99. * @return string[] an array of canonical ISO 639-2 language codes
  100. */
  101. public static function getAlpha3Codes(): array
  102. {
  103. return self::readEntry(['Alpha3Languages'], 'meta');
  104. }
  105. /**
  106. * @param string $language ISO 639-2 three-letter language code
  107. */
  108. public static function alpha3CodeExists(string $language): bool
  109. {
  110. try {
  111. self::getAlpha2Code($language);
  112. return true;
  113. } catch (MissingResourceException $e) {
  114. static $cache;
  115. if (null === $cache) {
  116. $cache = array_flip(self::getAlpha3Codes());
  117. }
  118. return isset($cache[$language]);
  119. }
  120. }
  121. /**
  122. * Gets the language name from its ISO 639-2 three-letter code.
  123. *
  124. * @throws MissingResourceException if the country code does not exists
  125. */
  126. public static function getAlpha3Name(string $language, string $displayLocale = null): string
  127. {
  128. try {
  129. return self::getName(self::getAlpha2Code($language), $displayLocale);
  130. } catch (MissingResourceException $e) {
  131. if (3 === \strlen($language)) {
  132. return self::getName($language, $displayLocale);
  133. }
  134. throw $e;
  135. }
  136. }
  137. /**
  138. * Gets the list of language names indexed with ISO 639-2 three-letter codes as keys.
  139. *
  140. * Same as method getNames, but with ISO 639-2 three-letter codes instead of ISO 639-1 codes as keys.
  141. *
  142. * @return string[]
  143. */
  144. public static function getAlpha3Names($displayLocale = null): array
  145. {
  146. $alpha2Names = self::getNames($displayLocale);
  147. $alpha3Names = [];
  148. foreach ($alpha2Names as $alpha2Code => $name) {
  149. if (3 === \strlen($alpha2Code)) {
  150. $alpha3Names[$alpha2Code] = $name;
  151. continue;
  152. }
  153. try {
  154. $alpha3Names[self::getAlpha3Code($alpha2Code)] = $name;
  155. } catch (MissingResourceException $e) {
  156. }
  157. }
  158. return $alpha3Names;
  159. }
  160. protected static function getPath(): string
  161. {
  162. return Intl::getDataDirectory().'/'.Intl::LANGUAGE_DIR;
  163. }
  164. }