LocaleLookup::resolveCacheMiss

protected LocaleLookup::resolveCacheMiss($offset)

Resolves a cache miss.

When an offset is not found in the object, this is treated as a cache miss. This method allows classes using this implementation to look up the actual value and allow it to be cached.

Parameters

string $key: The offset that was requested.

Return value

mixed The value of the offset, or NULL if no value was found.

Overrides CacheCollector::resolveCacheMiss

File

core/modules/locale/src/LocaleLookup.php, line 134

Class

LocaleLookup
A cache collector to allow for dynamic building of the locale cache.

Namespace

Drupal\locale

Code

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' => \Drupal::VERSION,
    ))->addLocation('path', $this->requestStack->getCurrentRequest()->getRequestUri())->save();
    $value = TRUE;
  }

  // If there is no translation available for the current language then use
  // language fallback to try other translations.
  if ($value === TRUE) {
    $fallbacks = $this->languageManager->getFallbackCandidates(array('langcode' => $this->langcode, 'operation' => 'locale_lookup', 'data' => $offset));
    if (!empty($fallbacks)) {
      foreach ($fallbacks as $langcode) {
        $translation = $this->stringStorage->findTranslation(array(
          'language' => $langcode,
          'source' => $offset,
          'context' => $this->context,
        ));

        if ($translation && !empty($translation->translation)) {
          $value = $translation->translation;
          break;
        }
      }
    }
  }

  $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 ($this->configFactory->get('locale.settings')->get('cache_strings')) {
    $this->persist($offset);
  }
  return $value;
}
doc_Drupal
2016-10-29 09:24:06
Comments
Leave a Comment

Please login to continue.