You are here

protected function LocaleLookup::resolveCacheMiss in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/locale/src/LocaleLookup.php \Drupal\locale\LocaleLookup::resolveCacheMiss()

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 139
Contains \Drupal\locale\LocaleLookup.

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