You are here

class CurrentLocale in Commerce Core 8.2

Holds a reference to the current locale, resolved on demand.

The ChainLocaleResolver runs the registered locale resolvers one by one until one of them returns the locale. The DefaultLocaleResolver runs last, and contains the default logic which assembles the locale based on the current language and country.

Hierarchy

Expanded class hierarchy of CurrentLocale

See also

\Drupal\commerce\Resolver\ChainLocaleResolver

\Drupal\commerce\Resolver\DefaultLocaleResolver

1 string reference to 'CurrentLocale'
commerce.services.yml in ./commerce.services.yml
commerce.services.yml
1 service uses CurrentLocale
commerce.current_locale in ./commerce.services.yml
Drupal\commerce\CurrentLocale

File

src/CurrentLocale.php, line 19

Namespace

Drupal\commerce
View source
class CurrentLocale implements CurrentLocaleInterface {

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * The chain resolver.
   *
   * @var \Drupal\commerce\Resolver\ChainLocaleResolverInterface
   */
  protected $chainResolver;

  /**
   * Static cache of resolved locales. One per request.
   *
   * @var \SplObjectStorage
   */
  protected $locales;

  /**
   * Constructs a new CurrentLocale object.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\commerce\Resolver\ChainLocaleResolverInterface $chain_resolver
   *   The chain resolver.
   */
  public function __construct(RequestStack $request_stack, ChainLocaleResolverInterface $chain_resolver) {
    $this->requestStack = $request_stack;
    $this->chainResolver = $chain_resolver;
    $this->locales = new \SplObjectStorage();
  }

  /**
   * {@inheritdoc}
   */
  public function getLocale() {
    $request = $this->requestStack
      ->getCurrentRequest();
    if (!$this->locales
      ->contains($request)) {
      $this->locales[$request] = $this->chainResolver
        ->resolve();
    }
    return $this->locales[$request];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CurrentLocale::$chainResolver protected property The chain resolver.
CurrentLocale::$locales protected property Static cache of resolved locales. One per request.
CurrentLocale::$requestStack protected property The request stack.
CurrentLocale::getLocale public function Gets the locale for the current request. Overrides CurrentLocaleInterface::getLocale
CurrentLocale::__construct public function Constructs a new CurrentLocale object.