You are here

public function LocaleResolver::resolveCurrencyLocale in Currency 8.3

Loads the locale to use.

Parameters

string $language_type: One of the \Drupal\Core\Language\LanguageInterface\TYPE_* constants.

Return value

\Drupal\currency\Entity\CurrencyLocaleInterface

Throws

\RuntimeException

Overrides LocaleResolverInterface::resolveCurrencyLocale

File

src/LocaleResolver.php, line 69

Class

LocaleResolver
Gets the right locale for the environment.

Namespace

Drupal\currency

Code

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();

    // Try this request's country code.
    $country_code = $this->eventDispatcher
      ->resolveCountryCode();
    if ($country_code) {
      $currency_locale = $this->currencyLocaleStorage
        ->load($language_code . '_' . $country_code);
    }

    // Try the site's default 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);
      }
    }

    // Try the Currency default.
    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];
}