TranslatedLists.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Locale;
  7. use Magento\Framework\Locale\Bundle\CurrencyBundle;
  8. use Magento\Framework\Locale\Bundle\DataBundle;
  9. use Magento\Framework\Locale\Bundle\LanguageBundle;
  10. use Magento\Framework\Locale\Bundle\RegionBundle;
  11. class TranslatedLists implements ListsInterface
  12. {
  13. /**
  14. * @var \Magento\Framework\Locale\ConfigInterface
  15. */
  16. protected $_config;
  17. /**
  18. * @var \Magento\Framework\Locale\ResolverInterface
  19. */
  20. protected $localeResolver;
  21. /**
  22. * @param \Magento\Framework\Locale\ConfigInterface $config
  23. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  24. * @param string $locale
  25. */
  26. public function __construct(
  27. \Magento\Framework\Locale\ConfigInterface $config,
  28. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  29. $locale = null
  30. ) {
  31. $this->_config = $config;
  32. $this->localeResolver = $localeResolver;
  33. if ($locale !== null) {
  34. $this->localeResolver->setLocale($locale);
  35. }
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function getOptionLocales()
  41. {
  42. return $this->_getOptionLocales();
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. public function getTranslatedOptionLocales()
  48. {
  49. return $this->_getOptionLocales(true);
  50. }
  51. /**
  52. * Get options array for locale dropdown
  53. *
  54. * @param bool $translatedName translation flag
  55. * @return array
  56. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  57. */
  58. protected function _getOptionLocales($translatedName = false)
  59. {
  60. $currentLocale = $this->localeResolver->getLocale();
  61. $locales = \ResourceBundle::getLocales('') ?: [];
  62. $languages = (new LanguageBundle())->get($currentLocale)['Languages'];
  63. $countries = (new RegionBundle())->get($currentLocale)['Countries'];
  64. $options = [];
  65. $allowedLocales = $this->_config->getAllowedLocales();
  66. foreach ($locales as $locale) {
  67. if (!in_array($locale, $allowedLocales)) {
  68. continue;
  69. }
  70. $language = \Locale::getPrimaryLanguage($locale);
  71. $country = \Locale::getRegion($locale);
  72. if (!$languages[$language] || !$countries[$country]) {
  73. continue;
  74. }
  75. if ($translatedName) {
  76. $label = ucwords(\Locale::getDisplayLanguage($locale, $locale))
  77. . ' (' . \Locale::getDisplayRegion($locale, $locale) . ') / '
  78. . $languages[$language]
  79. . ' (' . $countries[$country] . ')';
  80. } else {
  81. $label = $languages[$language]
  82. . ' (' . $countries[$country] . ')';
  83. }
  84. $options[] = ['value' => $locale, 'label' => $label];
  85. }
  86. return $this->_sortOptionArray($options);
  87. }
  88. /**
  89. * @inheritdoc
  90. */
  91. public function getOptionTimezones()
  92. {
  93. $options = [];
  94. $locale = $this->localeResolver->getLocale();
  95. $zones = \DateTimeZone::listIdentifiers(\DateTimeZone::ALL) ?: [];
  96. foreach ($zones as $code) {
  97. $options[] = [
  98. 'label' => \IntlTimeZone::createTimeZone($code)
  99. ->getDisplayName(false, \IntlTimeZone::DISPLAY_LONG, $locale) . ' (' . $code . ')',
  100. 'value' => $code,
  101. ];
  102. }
  103. return $this->_sortOptionArray($options);
  104. }
  105. /**
  106. * @inheritdoc
  107. */
  108. public function getOptionWeekdays($preserveCodes = false, $ucFirstCode = false)
  109. {
  110. $options = [];
  111. $days = (new DataBundle())
  112. ->get($this->localeResolver->getLocale())['calendar']['gregorian']['dayNames']['format']['wide'] ?: [];
  113. $englishDays = (new DataBundle())->get('en_US')['calendar']['gregorian']['dayNames']['format']['abbreviated'];
  114. foreach ($days as $code => $name) {
  115. $code = $preserveCodes ? $englishDays[$code] : $code;
  116. $options[] = ['label' => $name, 'value' => $ucFirstCode ? ucfirst($code) : $code];
  117. }
  118. return $options;
  119. }
  120. /**
  121. * @inheritdoc
  122. */
  123. public function getOptionCountries()
  124. {
  125. $options = [];
  126. $countries = (new RegionBundle())->get($this->localeResolver->getLocale())['Countries'] ?: [];
  127. foreach ($countries as $code => $name) {
  128. $options[] = ['label' => $name, 'value' => $code];
  129. }
  130. return $this->_sortOptionArray($options);
  131. }
  132. /**
  133. * @inheritdoc
  134. */
  135. public function getOptionCurrencies()
  136. {
  137. $currencies = (new CurrencyBundle())->get($this->localeResolver->getLocale())['Currencies'] ?: [];
  138. $options = [];
  139. $allowed = $this->_config->getAllowedCurrencies();
  140. foreach ($currencies as $code => $data) {
  141. if (!in_array($code, $allowed)) {
  142. continue;
  143. }
  144. $options[] = ['label' => $data[1], 'value' => $code];
  145. }
  146. return $this->_sortOptionArray($options);
  147. }
  148. /**
  149. * @inheritdoc
  150. */
  151. public function getOptionAllCurrencies()
  152. {
  153. $currencyBundle = new \Magento\Framework\Locale\Bundle\CurrencyBundle();
  154. $locale = $this->localeResolver->getLocale();
  155. $currencies = $currencyBundle->get($locale)['Currencies'] ?: [];
  156. $options = [];
  157. foreach ($currencies as $code => $data) {
  158. $options[] = ['label' => $data[1], 'value' => $code];
  159. }
  160. return $this->_sortOptionArray($options);
  161. }
  162. /**
  163. * @param array $option
  164. * @return array
  165. */
  166. protected function _sortOptionArray($option)
  167. {
  168. $data = [];
  169. foreach ($option as $item) {
  170. $data[$item['value']] = $item['label'];
  171. }
  172. asort($data);
  173. $option = [];
  174. foreach ($data as $key => $label) {
  175. $option[] = ['value' => $key, 'label' => $label];
  176. }
  177. return $option;
  178. }
  179. /**
  180. * @inheritdoc
  181. */
  182. public function getCountryTranslation($value, $locale = null)
  183. {
  184. if ($locale == null) {
  185. return (new RegionBundle())->get($this->localeResolver->getLocale())['Countries'][$value];
  186. } else {
  187. return (new RegionBundle())->get($locale)['Countries'][$value];
  188. }
  189. }
  190. }