Lists.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Setup;
  7. use Magento\Framework\Locale\Bundle\CurrencyBundle;
  8. use Magento\Framework\Locale\Bundle\LanguageBundle;
  9. use Magento\Framework\Locale\Bundle\RegionBundle;
  10. use Magento\Framework\Locale\ConfigInterface;
  11. use Magento\Framework\Locale\Resolver;
  12. class Lists
  13. {
  14. /**
  15. * List of allowed locales
  16. *
  17. * @var array
  18. */
  19. protected $allowedLocales;
  20. /**
  21. * List of allowed currencies
  22. *
  23. * @var array
  24. */
  25. private $allowedCurrencies;
  26. /**
  27. * @param ConfigInterface $localeConfig
  28. */
  29. public function __construct(ConfigInterface $localeConfig)
  30. {
  31. $this->allowedLocales = $localeConfig->getAllowedLocales();
  32. $this->allowedCurrencies = $localeConfig->getAllowedCurrencies();
  33. }
  34. /**
  35. * Retrieve list of timezones
  36. *
  37. * @param bool $doSort
  38. * @return array
  39. */
  40. public function getTimezoneList($doSort = true)
  41. {
  42. $zones = \DateTimeZone::listIdentifiers(\DateTimeZone::ALL);
  43. $list = [];
  44. foreach ($zones as $code) {
  45. $list[$code] = \IntlTimeZone::createTimeZone($code)->getDisplayName(
  46. false,
  47. \IntlTimeZone::DISPLAY_LONG,
  48. Resolver::DEFAULT_LOCALE
  49. ) . ' (' . $code . ')';
  50. }
  51. if ($doSort) {
  52. asort($list);
  53. }
  54. return $list;
  55. }
  56. /**
  57. * Retrieve list of currencies
  58. *
  59. * @return array
  60. */
  61. public function getCurrencyList()
  62. {
  63. $currencies = (new CurrencyBundle())->get(Resolver::DEFAULT_LOCALE)['Currencies'];
  64. $list = [];
  65. foreach ($currencies as $code => $data) {
  66. $isAllowedCurrency = array_search($code, $this->allowedCurrencies) !== false;
  67. if (!$isAllowedCurrency) {
  68. continue;
  69. }
  70. $list[$code] = $data[1] . ' (' . $code . ')';
  71. }
  72. asort($list);
  73. return $list;
  74. }
  75. /**
  76. * Retrieve list of locales
  77. *
  78. * @return array
  79. */
  80. public function getLocaleList()
  81. {
  82. $languages = (new LanguageBundle())->get(Resolver::DEFAULT_LOCALE)['Languages'];
  83. $countries = (new RegionBundle())->get(Resolver::DEFAULT_LOCALE)['Countries'];
  84. $locales = \ResourceBundle::getLocales('') ?: [];
  85. $list = [];
  86. foreach ($locales as $locale) {
  87. if (!in_array($locale, $this->allowedLocales)) {
  88. continue;
  89. }
  90. $language = \Locale::getPrimaryLanguage($locale);
  91. $country = \Locale::getRegion($locale);
  92. if (!$languages[$language] || !$countries[$country]) {
  93. continue;
  94. }
  95. $list[$locale] = $languages[$language] . ' (' . $countries[$country] . ')';
  96. }
  97. asort($list);
  98. return $list;
  99. }
  100. }