comparison core/modules/locale/src/LocaleLookup.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\locale;
4
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;
11
12 /**
13 * A cache collector to allow for dynamic building of the locale cache.
14 */
15 class LocaleLookup extends CacheCollector {
16
17 /**
18 * A language code.
19 *
20 * @var string
21 */
22 protected $langcode;
23
24 /**
25 * The msgctxt context.
26 *
27 * @var string
28 */
29 protected $context;
30
31 /**
32 * The locale storage.
33 *
34 * @var \Drupal\locale\StringStorageInterface
35 */
36 protected $stringStorage;
37
38 /**
39 * The cache backend that should be used.
40 *
41 * @var \Drupal\Core\Cache\CacheBackendInterface
42 */
43 protected $cache;
44
45 /**
46 * The lock backend that should be used.
47 *
48 * @var \Drupal\Core\Lock\LockBackendInterface
49 */
50 protected $lock;
51
52 /**
53 * The configuration factory.
54 *
55 * @var \Drupal\Core\Config\ConfigFactoryInterface
56 */
57 protected $configFactory;
58
59 /**
60 * The language manager.
61 *
62 * @var \Drupal\Core\Language\LanguageManagerInterface
63 */
64 protected $languageManager;
65
66 /**
67 * The request stack.
68 *
69 * @var \Symfony\Component\HttpFoundation\RequestStack
70 */
71 protected $requestStack;
72
73 /**
74 * Constructs a LocaleLookup object.
75 *
76 * @param string $langcode
77 * The language code.
78 * @param string $context
79 * The string context.
80 * @param \Drupal\locale\StringStorageInterface $string_storage
81 * The string storage.
82 * @param \Drupal\Core\Cache\CacheBackendInterface $cache
83 * The cache backend.
84 * @param \Drupal\Core\Lock\LockBackendInterface $lock
85 * The lock backend.
86 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
87 * The config factory.
88 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
89 * The language manager.
90 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
91 * The request stack.
92 */
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;
99
100 $this->cache = $cache;
101 $this->lock = $lock;
102 $this->tags = ['locale'];
103 $this->requestStack = $request_stack;
104 }
105
106 /**
107 * {@inheritdoc}
108 */
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";
117
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 = [];
127 }
128 return $this->cid;
129 }
130
131 /**
132 * {@inheritdoc}
133 */
134 protected function resolveCacheMiss($offset) {
135 $translation = $this->stringStorage->findTranslation([
136 'language' => $this->langcode,
137 'source' => $offset,
138 'context' => $this->context,
139 ]);
140
141 if ($translation) {
142 $value = !empty($translation->translation) ? $translation->translation : TRUE;
143 }
144 else {
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([
148 'source' => $offset,
149 'context' => $this->context,
150 'version' => \Drupal::VERSION,
151 ])->addLocation('path', $this->requestStack->getCurrentRequest()->getRequestUri())->save();
152 $value = TRUE;
153 }
154
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,
163 'source' => $offset,
164 'context' => $this->context,
165 ]);
166
167 if ($translation && !empty($translation->translation)) {
168 $value = $translation->translation;
169 break;
170 }
171 }
172 }
173 }
174
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);
182 }
183 return $value;
184 }
185
186 }