You are here

public function DefaultLocaleResolver::resolve in Price 8

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

Resolves the locale.

Return value

\Drupal\price\Locale|null The locale object, if resolved. Otherwise NULL, indicating that the next resolver in the chain should be called.

Overrides LocaleResolverInterface::resolve

File

src/Resolver/DefaultLocaleResolver.php, line 44

Class

DefaultLocaleResolver
Returns the locale based on the current language and country.

Namespace

Drupal\price\Resolver

Code

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