You are here

protected function LocaleLookup::resolveCacheMiss in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/locale/src/LocaleLookup.php \Drupal\locale\LocaleLookup::resolveCacheMiss()
  2. 10 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 135

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([
    '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([
      '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([
      'langcode' => $this->langcode,
      'operation' => 'locale_lookup',
      'data' => $offset,
    ]);
    if (!empty($fallbacks)) {
      foreach ($fallbacks as $langcode) {
        $translation = $this->stringStorage
          ->findTranslation([
          'language' => $langcode,
          'source' => $offset,
          'context' => $this->context,
        ]);
        if ($translation && !empty($translation->translation)) {
          $value = $translation->translation;
          break;
        }
      }
    }
  }
  if (is_string($value) && strpos($value, PoItem::DELIMITER) !== FALSE) {

    // Community translations imported from localize.drupal.org as well as
    // migrated translations may contain @count[number].
    $value = preg_replace('!@count\\[\\d+\\]!', '@count', $value);
  }
  $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;
}