LocaleResolver.php in Currency 8.3
File
src/LocaleResolver.php
View source
<?php
namespace Drupal\currency;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
class LocaleResolver implements LocaleResolverInterface {
protected $configFactory;
protected $currencyLocaleStorage;
protected $eventDispatcher;
protected $languageManager;
protected $currencyLocales = [];
public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory, EventDispatcherInterface $event_dispatcher) {
$this->configFactory = $config_factory;
$this->currencyLocaleStorage = $entity_type_manager
->getStorage('currency_locale');
$this->eventDispatcher = $event_dispatcher;
$this->languageManager = $language_manager;
}
public function resolveCurrencyLocale($language_type = LanguageInterface::TYPE_CONTENT) {
if (empty($this->currencyLocales[$language_type])) {
$currency_locale = NULL;
$language_code = $this->languageManager
->getCurrentLanguage($language_type)
->getId();
$country_code = $this->eventDispatcher
->resolveCountryCode();
if ($country_code) {
$currency_locale = $this->currencyLocaleStorage
->load($language_code . '_' . $country_code);
}
if (!$currency_locale) {
$country_code = $this->configFactory
->get('system.date')
->get('country.default');
if ($country_code) {
$currency_locale = $this->currencyLocaleStorage
->load($language_code . '_' . $country_code);
}
}
if (!$currency_locale) {
$currency_locale = $this->currencyLocaleStorage
->load($this::DEFAULT_LOCALE);
}
if ($currency_locale) {
$this->currencyLocales[$language_type] = $currency_locale;
}
else {
throw new \RuntimeException(sprintf('The currency locale for %s could not be loaded.', $this::DEFAULT_LOCALE));
}
}
return $this->currencyLocales[$language_type];
}
}