3 namespace Drupal\locale;
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Cache\CacheCollector;
7 use Drupal\Core\Config\ConfigFactoryInterface;
8 use Drupal\Core\Language\LanguageManagerInterface;
9 use Drupal\Core\Lock\LockBackendInterface;
10 use Symfony\Component\HttpFoundation\RequestStack;
13 * A cache collector to allow for dynamic building of the locale cache.
15 class LocaleLookup extends CacheCollector {
25 * The msgctxt context.
34 * @var \Drupal\locale\StringStorageInterface
36 protected $stringStorage;
39 * The cache backend that should be used.
41 * @var \Drupal\Core\Cache\CacheBackendInterface
46 * The lock backend that should be used.
48 * @var \Drupal\Core\Lock\LockBackendInterface
53 * The configuration factory.
55 * @var \Drupal\Core\Config\ConfigFactoryInterface
57 protected $configFactory;
60 * The language manager.
62 * @var \Drupal\Core\Language\LanguageManagerInterface
64 protected $languageManager;
69 * @var \Symfony\Component\HttpFoundation\RequestStack
71 protected $requestStack;
74 * Constructs a LocaleLookup object.
76 * @param string $langcode
78 * @param string $context
80 * @param \Drupal\locale\StringStorageInterface $string_storage
82 * @param \Drupal\Core\Cache\CacheBackendInterface $cache
84 * @param \Drupal\Core\Lock\LockBackendInterface $lock
86 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
88 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
89 * The language manager.
90 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
93 public function __construct($langcode, $context, StringStorageInterface $string_storage, CacheBackendInterface $cache, LockBackendInterface $lock, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager, RequestStack $request_stack) {
94 $this->langcode = $langcode;
95 $this->context = (string) $context;
96 $this->stringStorage = $string_storage;
97 $this->configFactory = $config_factory;
98 $this->languageManager = $language_manager;
100 $this->cache = $cache;
102 $this->tags = ['locale'];
103 $this->requestStack = $request_stack;
109 protected function getCid() {
110 if (!isset($this->cid)) {
111 // Add the current user's role IDs to the cache key, this ensures that,
112 // for example, strings for admin menu items and settings forms are not
113 // cached for anonymous users.
114 $user = \Drupal::currentUser();
115 $rids = $user ? implode(':', $user->getRoles()) : '';
116 $this->cid = "locale:{$this->langcode}:{$this->context}:$rids";
118 // Getting the roles from the current user might have resulted in t()
119 // calls that attempted to get translations from the locale cache. In that
120 // case they would not go into this method again as
121 // CacheCollector::lazyLoadCache() already set the loaded flag. They would
122 // however call resolveCacheMiss() and add that string to the list of
123 // cache misses that need to be written into the cache. Prevent that by
124 // resetting that list. All that happens in such a case are a few uncached
125 // translation lookups.
126 $this->keysToPersist = [];
134 protected function resolveCacheMiss($offset) {
135 $translation = $this->stringStorage->findTranslation([
136 'language' => $this->langcode,
138 'context' => $this->context,
142 $value = !empty($translation->translation) ? $translation->translation : TRUE;
145 // We don't have the source string, update the {locales_source} table to
146 // indicate the string is not translated.
147 $this->stringStorage->createString([
149 'context' => $this->context,
150 'version' => \Drupal::VERSION,
151 ])->addLocation('path', $this->requestStack->getCurrentRequest()->getRequestUri())->save();
155 // If there is no translation available for the current language then use
156 // language fallback to try other translations.
157 if ($value === TRUE) {
158 $fallbacks = $this->languageManager->getFallbackCandidates(['langcode' => $this->langcode, 'operation' => 'locale_lookup', 'data' => $offset]);
159 if (!empty($fallbacks)) {
160 foreach ($fallbacks as $langcode) {
161 $translation = $this->stringStorage->findTranslation([
162 'language' => $langcode,
164 'context' => $this->context,
167 if ($translation && !empty($translation->translation)) {
168 $value = $translation->translation;
175 $this->storage[$offset] = $value;
176 // Disabling the usage of string caching allows a module to watch for
177 // the exact list of strings used on a page. From a performance
178 // perspective that is a really bad idea, so we have no user
179 // interface for this. Be careful when turning this option off!
180 if ($this->configFactory->get('locale.settings')->get('cache_strings')) {
181 $this->persist($offset);