Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TranslatedLists.php
Go to the documentation of this file.
1 <?php
8 
13 
15 {
19  protected $_config;
20 
24  protected $localeResolver;
25 
31  public function __construct(
32  \Magento\Framework\Locale\ConfigInterface $config,
33  \Magento\Framework\Locale\ResolverInterface $localeResolver,
34  $locale = null
35  ) {
36  $this->_config = $config;
37  $this->localeResolver = $localeResolver;
38  if ($locale !== null) {
39  $this->localeResolver->setLocale($locale);
40  }
41  }
42 
46  public function getOptionLocales()
47  {
48  return $this->_getOptionLocales();
49  }
50 
54  public function getTranslatedOptionLocales()
55  {
56  return $this->_getOptionLocales(true);
57  }
58 
66  protected function _getOptionLocales($translatedName = false)
67  {
68  $currentLocale = $this->localeResolver->getLocale();
69  $locales = \ResourceBundle::getLocales('') ?: [];
70  $languages = (new LanguageBundle())->get($currentLocale)['Languages'];
71  $countries = (new RegionBundle())->get($currentLocale)['Countries'];
72 
73  $options = [];
74  $allowedLocales = $this->_config->getAllowedLocales();
75  foreach ($locales as $locale) {
76  if (!in_array($locale, $allowedLocales)) {
77  continue;
78  }
79  $language = \Locale::getPrimaryLanguage($locale);
80  $country = \Locale::getRegion($locale);
81  if (!$languages[$language] || !$countries[$country]) {
82  continue;
83  }
84  if ($translatedName) {
85  $label = ucwords(\Locale::getDisplayLanguage($locale, $locale))
86  . ' (' . \Locale::getDisplayRegion($locale, $locale) . ') / '
87  . $languages[$language]
88  . ' (' . $countries[$country] . ')';
89  } else {
90  $label = $languages[$language]
91  . ' (' . $countries[$country] . ')';
92  }
93  $options[] = ['value' => $locale, 'label' => $label];
94  }
95  return $this->_sortOptionArray($options);
96  }
97 
101  public function getOptionTimezones()
102  {
103  $options = [];
104  $locale = $this->localeResolver->getLocale();
105  $zones = \DateTimeZone::listIdentifiers(\DateTimeZone::ALL) ?: [];
106  foreach ($zones as $code) {
107  $options[] = [
108  'label' => \IntlTimeZone::createTimeZone($code)
109  ->getDisplayName(false, \IntlTimeZone::DISPLAY_LONG, $locale) . ' (' . $code . ')',
110  'value' => $code,
111  ];
112  }
113  return $this->_sortOptionArray($options);
114  }
115 
119  public function getOptionWeekdays($preserveCodes = false, $ucFirstCode = false)
120  {
121  $options = [];
122  $days = (new DataBundle())
123  ->get($this->localeResolver->getLocale())['calendar']['gregorian']['dayNames']['format']['wide'] ?: [];
124  $englishDays = (new DataBundle())->get('en_US')['calendar']['gregorian']['dayNames']['format']['abbreviated'];
125  foreach ($days as $code => $name) {
126  $code = $preserveCodes ? $englishDays[$code] : $code;
127  $options[] = ['label' => $name, 'value' => $ucFirstCode ? ucfirst($code) : $code];
128  }
129  return $options;
130  }
131 
135  public function getOptionCountries()
136  {
137  $options = [];
138  $countries = (new RegionBundle())->get($this->localeResolver->getLocale())['Countries'] ?: [];
139  foreach ($countries as $code => $name) {
140  $options[] = ['label' => $name, 'value' => $code];
141  }
142  return $this->_sortOptionArray($options);
143  }
144 
148  public function getOptionCurrencies()
149  {
150  $currencies = (new CurrencyBundle())->get($this->localeResolver->getLocale())['Currencies'] ?: [];
151  $options = [];
152  $allowed = $this->_config->getAllowedCurrencies();
153  foreach ($currencies as $code => $data) {
154  if (!in_array($code, $allowed)) {
155  continue;
156  }
157  $options[] = ['label' => $data[1], 'value' => $code];
158  }
159  return $this->_sortOptionArray($options);
160  }
161 
165  public function getOptionAllCurrencies()
166  {
167  $currencyBundle = new \Magento\Framework\Locale\Bundle\CurrencyBundle();
168  $locale = $this->localeResolver->getLocale();
169  $currencies = $currencyBundle->get($locale)['Currencies'] ?: [];
170 
171  $options = [];
172  foreach ($currencies as $code => $data) {
173  $options[] = ['label' => $data[1], 'value' => $code];
174  }
175  return $this->_sortOptionArray($options);
176  }
177 
182  protected function _sortOptionArray($option)
183  {
184  $data = [];
185  foreach ($option as $item) {
186  $data[$item['value']] = $item['label'];
187  }
188  asort($data);
189  $option = [];
190  foreach ($data as $key => $label) {
191  $option[] = ['value' => $key, 'label' => $label];
192  }
193  return $option;
194  }
195 
199  public function getCountryTranslation($value, $locale = null)
200  {
201  if ($locale == null) {
202  return (new RegionBundle())->get($this->localeResolver->getLocale())['Countries'][$value];
203  } else {
204  return (new RegionBundle())->get($locale)['Countries'][$value];
205  }
206  }
207 }
$config
Definition: fraud_order.php:17
getOptionWeekdays($preserveCodes=false, $ucFirstCode=false)
$locales
Definition: locales.php:14
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
__construct(\Magento\Framework\Locale\ConfigInterface $config, \Magento\Framework\Locale\ResolverInterface $localeResolver, $locale=null)
$code
Definition: info.phtml:12
if(!isset($_GET['name'])) $name
Definition: log.php:14