Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
TranslatedLists Class Reference
Inheritance diagram for TranslatedLists:
ListsInterface OptionInterface

Public Member Functions

 __construct (\Magento\Framework\Locale\ConfigInterface $config, \Magento\Framework\Locale\ResolverInterface $localeResolver, $locale=null)
 
 getOptionLocales ()
 
 getTranslatedOptionLocales ()
 
 getOptionTimezones ()
 
 getOptionWeekdays ($preserveCodes=false, $ucFirstCode=false)
 
 getOptionCountries ()
 
 getOptionCurrencies ()
 
 getOptionAllCurrencies ()
 
 getCountryTranslation ($value, $locale=null)
 

Protected Member Functions

 _getOptionLocales ($translatedName=false)
 
 _sortOptionArray ($option)
 

Protected Attributes

 $_config
 
 $localeResolver
 

Detailed Description

Definition at line 14 of file TranslatedLists.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Locale\ConfigInterface  $config,
\Magento\Framework\Locale\ResolverInterface  $localeResolver,
  $locale = null 
)
Parameters
\Magento\Framework\Locale\ConfigInterface$config
\Magento\Framework\Locale\ResolverInterface$localeResolver
string$locale

Definition at line 31 of file TranslatedLists.php.

35  {
36  $this->_config = $config;
37  $this->localeResolver = $localeResolver;
38  if ($locale !== null) {
39  $this->localeResolver->setLocale($locale);
40  }
41  }
$config
Definition: fraud_order.php:17

Member Function Documentation

◆ _getOptionLocales()

_getOptionLocales (   $translatedName = false)
protected

Get options array for locale dropdown

Parameters
bool$translatedNametranslation flag
Returns
array @SuppressWarnings(PHPMD.CyclomaticComplexity)

Definition at line 66 of file TranslatedLists.php.

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  }
$locales
Definition: locales.php:14
$label
Definition: details.phtml:21

◆ _sortOptionArray()

_sortOptionArray (   $option)
protected
Parameters
array$option
Returns
array

Definition at line 182 of file TranslatedLists.php.

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  }
$label
Definition: details.phtml:21

◆ getCountryTranslation()

getCountryTranslation (   $value,
  $locale = null 
)

Returns the localized country name

Parameters
string$valueName to get detailed information about
string$localeOptional locale string
Returns
string

Implements ListsInterface.

Definition at line 199 of file TranslatedLists.php.

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  }
$value
Definition: gender.phtml:16

◆ getOptionAllCurrencies()

getOptionAllCurrencies ( )

Retrieve all currency option list

Returns
array

Implements ListsInterface.

Definition at line 165 of file TranslatedLists.php.

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  }
$code
Definition: info.phtml:12

◆ getOptionCountries()

getOptionCountries ( )

Retrieve country option list

Returns
array

Implements ListsInterface.

Definition at line 135 of file TranslatedLists.php.

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  }
$code
Definition: info.phtml:12
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ getOptionCurrencies()

getOptionCurrencies ( )

Retrieve currency option list

Returns
array

Implements ListsInterface.

Definition at line 148 of file TranslatedLists.php.

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  }
$code
Definition: info.phtml:12

◆ getOptionLocales()

getOptionLocales ( )

Get array of deployed locales.Function result has next format:

[
0 => [
'value' => 'de_DE'
'label' => 'German (Germany)'
],
1 => [
'value' => 'en_GB'
'label' => 'English (United Kingdom)'
],
]
Returns
array

Implements OptionInterface.

Definition at line 46 of file TranslatedLists.php.

47  {
48  return $this->_getOptionLocales();
49  }

◆ getOptionTimezones()

getOptionTimezones ( )

Retrieve timezone option list

Returns
array

Implements ListsInterface.

Definition at line 101 of file TranslatedLists.php.

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  }
$code
Definition: info.phtml:12

◆ getOptionWeekdays()

getOptionWeekdays (   $preserveCodes = false,
  $ucFirstCode = false 
)

Retrieve days of week option list

Parameters
bool$preserveCodes
bool$ucFirstCode
Returns
array

Implements ListsInterface.

Definition at line 119 of file TranslatedLists.php.

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  }
$code
Definition: info.phtml:12
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ getTranslatedOptionLocales()

getTranslatedOptionLocales ( )

Get array of deployed locales with translation.Function result has next format:

[
0 => [
'value' => 'de_DE'
'label' => 'Deutsch (Deutschland) / German (Germany)'
],
1 => [
'value' => 'en_GB'
'label' => 'English (United Kingdom) / English (United Kingdom)'
],
]
Returns
array

Implements OptionInterface.

Definition at line 54 of file TranslatedLists.php.

55  {
56  return $this->_getOptionLocales(true);
57  }

Field Documentation

◆ $_config

$_config
protected

Definition at line 19 of file TranslatedLists.php.

◆ $localeResolver

$localeResolver
protected

Definition at line 24 of file TranslatedLists.php.


The documentation for this class was generated from the following file: