You are here

class DefaultLocaleResolver in Price 8

Same name and namespace in other branches
  1. 3.x src/Resolver/DefaultLocaleResolver.php \Drupal\price\Resolver\DefaultLocaleResolver
  2. 2.0.x src/Resolver/DefaultLocaleResolver.php \Drupal\price\Resolver\DefaultLocaleResolver
  3. 2.x src/Resolver/DefaultLocaleResolver.php \Drupal\price\Resolver\DefaultLocaleResolver
  4. 3.0.x src/Resolver/DefaultLocaleResolver.php \Drupal\price\Resolver\DefaultLocaleResolver

Returns the locale based on the current language and country.

Hierarchy

Expanded class hierarchy of DefaultLocaleResolver

1 string reference to 'DefaultLocaleResolver'
price.services.yml in ./price.services.yml
price.services.yml
1 service uses DefaultLocaleResolver
price.default_locale_resolver in ./price.services.yml
Drupal\price\Resolver\DefaultLocaleResolver

File

src/Resolver/DefaultLocaleResolver.php, line 12

Namespace

Drupal\price\Resolver
View source
class DefaultLocaleResolver implements LocaleResolverInterface {

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The current country.
   *
   * @var \Drupal\price\CurrentCountryInterface
   */
  protected $currentCountry;

  /**
   * Constructs a new DefaultLocaleResolver object.
   *
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Drupal\price\CurrentCountryInterface $current_country
   *   The current country.
   */
  public function __construct(LanguageManagerInterface $language_manager, CurrentCountryInterface $current_country) {
    $this->languageManager = $language_manager;
    $this->currentCountry = $current_country;
  }

  /**
   * {@inheritdoc}
   */
  public function resolve() {

    // The getCurrentLanguage() fallback is a workaround for core bug #2684873.
    $language = $this->languageManager
      ->getConfigOverrideLanguage() ?: $this->languageManager
      ->getCurrentLanguage();
    $langcode = $language
      ->getId();
    $langcode_parts = explode('-', $langcode);
    if (count($langcode_parts) > 1 && strlen(end($langcode_parts)) == 2) {

      // The current language already has a country component (e.g. 'pt-br'),
      // it qualifies as a full locale.
      $locale = $langcode;
    }
    elseif ($country = $this->currentCountry
      ->getCountry()) {

      // Assemble the locale using the resolved country. This can result
      // in non-existent combinations such as 'en-RS', it's up to the locale
      // consumers (e.g. the number format repository) to perform fallback.
      $locale = $langcode . '-' . $country;
    }
    else {

      // Worst case scenario, the country is unknown.
      $locale = $langcode;
    }
    return new Locale($locale);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultLocaleResolver::$currentCountry protected property The current country.
DefaultLocaleResolver::$languageManager protected property The language manager.
DefaultLocaleResolver::resolve public function Resolves the locale. Overrides LocaleResolverInterface::resolve
DefaultLocaleResolver::__construct public function Constructs a new DefaultLocaleResolver object.