class LocaleLookup

Extends CacheArray to allow for dynamic building of the locale cache.

Hierarchy

Expanded class hierarchy of LocaleLookup

1 file declares its use of LocaleLookup
locale.module in drupal/core/modules/locale/locale.module
Enables the translation of the user interface to languages other than English.

File

drupal/core/modules/locale/lib/Drupal/locale/LocaleLookup.php, line 17
Definition of LocaleLookup

Namespace

Drupal\locale
View source
class LocaleLookup extends CacheArray {

  /**
   * A language code.
   * @var string
   */
  protected $langcode;

  /**
   * The msgctxt context.
   * @var string
   */
  protected $context;

  /**
   * The locale storage
   *
   * @var Drupal\locale\StringStorageInterface
   */
  protected $stringStorage;

  /**
   * Constructs a LocaleCache object.
   */
  public function __construct($langcode, $context, $stringStorage) {
    $this->langcode = $langcode;
    $this->context = (string) $context;
    $this->stringStorage = $stringStorage;

    // Add the current user's role IDs to the cache key, this ensures that, for
    // example, strings for admin menu items and settings forms are not cached
    // for anonymous users.
    $rids = implode(':', array_keys($GLOBALS['user']->roles));
    parent::__construct("locale:{$langcode}:{$context}:{$rids}", 'cache', array(
      'locale' => TRUE,
    ));
  }

  /**
   * Implements CacheArray::resolveCacheMiss().
   */
  protected function resolveCacheMiss($offset) {
    $translation = $this->stringStorage
      ->findTranslation(array(
      'language' => $this->langcode,
      'source' => $offset,
      'context' => $this->context,
    ));
    if ($translation) {
      $value = !empty($translation->translation) ? $translation->translation : TRUE;
    }
    else {

      // We don't have the source string, update the {locales_source} table to
      // indicate the string is not translated.
      $this->stringStorage
        ->createString(array(
        'source' => $offset,
        'context' => $this->context,
        'version' => VERSION,
      ))
        ->addLocation('path', request_uri())
        ->save();
      $value = TRUE;
    }
    $this->storage[$offset] = $value;

    // Disabling the usage of string caching allows a module to watch for
    // the exact list of strings used on a page. From a performance
    // perspective that is a really bad idea, so we have no user
    // interface for this. Be careful when turning this option off!
    if (variable_get('locale_cache_strings', 1)) {
      $this
        ->persist($offset);
    }
    return $value;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheArray::$bin protected property A bin to pass to cache()->set() and cache()->get().
CacheArray::$cid protected property A cid to pass to cache()->set() and cache()->get().
CacheArray::$keysToPersist protected property An array of keys to add to the cache at the end of the request.
CacheArray::$storage protected property Storage for the data itself.
CacheArray::$tags protected property A tags array to pass to cache()->set().
CacheArray::offsetExists public function Implements ArrayAccess::offsetExists(). 1
CacheArray::offsetGet public function Implements ArrayAccess::offsetGet(). 1
CacheArray::offsetSet public function Implements ArrayAccess::offsetSet().
CacheArray::offsetUnset public function Implements ArrayAccess::offsetUnset().
CacheArray::persist protected function Flags an offset value to be written to the persistent cache.
CacheArray::set protected function Writes a value to the persistent cache immediately. 1
CacheArray::__destruct public function Destructs the CacheArray object.
LocaleLookup::$context protected property The msgctxt context.
LocaleLookup::$langcode protected property A language code.
LocaleLookup::$stringStorage protected property The locale storage
LocaleLookup::resolveCacheMiss protected function Implements CacheArray::resolveCacheMiss(). Overrides CacheArray::resolveCacheMiss
LocaleLookup::__construct public function Constructs a LocaleCache object. Overrides CacheArray::__construct