class LocaleLookup in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/locale/src/LocaleLookup.php \Drupal\locale\LocaleLookup
A cache collector to allow for dynamic building of the locale cache.
Hierarchy
- class \Drupal\Core\Cache\CacheCollector implements CacheCollectorInterface, DestructableInterface
- class \Drupal\locale\LocaleLookup
Expanded class hierarchy of LocaleLookup
1 file declares its use of LocaleLookup
- LocaleLookupTest.php in core/
modules/ locale/ tests/ src/ Unit/ LocaleLookupTest.php - Contains \Drupal\Tests\locale\Unit\LocaleLookupTest.
File
- core/
modules/ locale/ src/ LocaleLookup.php, line 20 - Contains \Drupal\locale\LocaleLookup.
Namespace
Drupal\localeView source
class LocaleLookup extends CacheCollector {
/**
* A language code.
*
* @var string
*/
protected $langcode;
/**
* The msgctxt context.
*
* @var string
*/
protected $context;
/**
* The locale storage.
*
* @var \Drupal\locale\StringStorageInterface
*/
protected $stringStorage;
/**
* The cache backend that should be used.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cache;
/**
* The lock backend that should be used.
*
* @var \Drupal\Core\Lock\LockBackendInterface
*/
protected $lock;
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* Constructs a LocaleLookup object.
*
* @param string $langcode
* The language code.
* @param string $context
* The string context.
* @param \Drupal\locale\StringStorageInterface $string_storage
* The string storage.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* The cache backend.
* @param \Drupal\Core\Lock\LockBackendInterface $lock
* The lock backend.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
public function __construct($langcode, $context, StringStorageInterface $string_storage, CacheBackendInterface $cache, LockBackendInterface $lock, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager, RequestStack $request_stack) {
$this->langcode = $langcode;
$this->context = (string) $context;
$this->stringStorage = $string_storage;
$this->configFactory = $config_factory;
$this->languageManager = $language_manager;
$this->cache = $cache;
$this->lock = $lock;
$this->tags = array(
'locale',
);
$this->requestStack = $request_stack;
}
/**
* {@inheritdoc}
*/
protected function getCid() {
if (!isset($this->cid)) {
// 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.
$user = \Drupal::currentUser();
$rids = $user ? implode(':', $user
->getRoles()) : '';
$this->cid = "locale:{$this->langcode}:{$this->context}:{$rids}";
// Getting the roles from the current user might have resulted in t()
// calls that attempted to get translations from the locale cache. In that
// case they would not go into this method again as
// CacheCollector::lazyLoadCache() already set the loaded flag. They would
// however call resolveCacheMiss() and add that string to the list of
// cache misses that need to be written into the cache. Prevent that by
// resetting that list. All that happens in such a case are a few uncached
// translation lookups.
$this->keysToPersist = array();
}
return $this->cid;
}
/**
* {@inheritdoc}
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheCollector:: |
protected | property | Stores the cache creation time. | |
CacheCollector:: |
protected | property | Flag that indicates of the cache has been invalidated. | |
CacheCollector:: |
protected | property | Indicates if the collected cache was already loaded. | |
CacheCollector:: |
protected | property | The cache id that is used for the cache entry. | |
CacheCollector:: |
protected | property | An array of keys to add to the cache on service termination. | |
CacheCollector:: |
protected | property | An array of keys to remove from the cache on service termination. | |
CacheCollector:: |
protected | property | Storage for the data itself. | |
CacheCollector:: |
protected | property | A list of tags that are used for the cache entry. | |
CacheCollector:: |
public | function |
Clears the collected cache entry. Overrides CacheCollectorInterface:: |
1 |
CacheCollector:: |
public | function |
Deletes the element. Overrides CacheCollectorInterface:: |
|
CacheCollector:: |
public | function |
Performs destruct operations. Overrides DestructableInterface:: |
|
CacheCollector:: |
public | function |
Gets value from the cache. Overrides CacheCollectorInterface:: |
2 |
CacheCollector:: |
public | function |
Returns whether data exists for this key. Overrides CacheCollectorInterface:: |
1 |
CacheCollector:: |
protected | function | Invalidate the cache. | |
CacheCollector:: |
protected | function | Loads the cache if not already done. | 1 |
CacheCollector:: |
protected | function | Normalizes a cache ID in order to comply with database limitations. | |
CacheCollector:: |
protected | function | Flags an offset value to be written to the persistent cache. | |
CacheCollector:: |
public | function |
Resets the local cache. Overrides CacheCollectorInterface:: |
1 |
CacheCollector:: |
public | function |
Implements \Drupal\Core\Cache\CacheCollectorInterface::set(). Overrides CacheCollectorInterface:: |
1 |
CacheCollector:: |
protected | function | Writes a value to the persistent cache immediately. | 1 |
LocaleLookup:: |
protected | property |
The cache backend that should be used. Overrides CacheCollector:: |
|
LocaleLookup:: |
protected | property | The configuration factory. | |
LocaleLookup:: |
protected | property | The msgctxt context. | |
LocaleLookup:: |
protected | property | A language code. | |
LocaleLookup:: |
protected | property | The language manager. | |
LocaleLookup:: |
protected | property |
The lock backend that should be used. Overrides CacheCollector:: |
|
LocaleLookup:: |
protected | property | The request stack. | |
LocaleLookup:: |
protected | property | The locale storage. | |
LocaleLookup:: |
protected | function |
Gets the cache ID. Overrides CacheCollector:: |
|
LocaleLookup:: |
protected | function |
Resolves a cache miss. Overrides CacheCollector:: |
|
LocaleLookup:: |
public | function |
Constructs a LocaleLookup object. Overrides CacheCollector:: |