function LocaleLookup::resolveCacheMiss

Same name 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()
  3. 11.x core/modules/locale/src/LocaleLookup.php \Drupal\locale\LocaleLookup::resolveCacheMiss()

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.