LocaleLookup.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace Drupal\locale;
  3. use Drupal\Component\Gettext\PoItem;
  4. use Drupal\Core\Cache\CacheBackendInterface;
  5. use Drupal\Core\Cache\CacheCollector;
  6. use Drupal\Core\Config\ConfigFactoryInterface;
  7. use Drupal\Core\Language\LanguageManagerInterface;
  8. use Drupal\Core\Lock\LockBackendInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. /**
  11. * A cache collector to allow for dynamic building of the locale cache.
  12. */
  13. class LocaleLookup extends CacheCollector {
  14. /**
  15. * A language code.
  16. *
  17. * @var string
  18. */
  19. protected $langcode;
  20. /**
  21. * The msgctxt context.
  22. *
  23. * @var string
  24. */
  25. protected $context;
  26. /**
  27. * The locale storage.
  28. *
  29. * @var \Drupal\locale\StringStorageInterface
  30. */
  31. protected $stringStorage;
  32. /**
  33. * The cache backend that should be used.
  34. *
  35. * @var \Drupal\Core\Cache\CacheBackendInterface
  36. */
  37. protected $cache;
  38. /**
  39. * The lock backend that should be used.
  40. *
  41. * @var \Drupal\Core\Lock\LockBackendInterface
  42. */
  43. protected $lock;
  44. /**
  45. * The configuration factory.
  46. *
  47. * @var \Drupal\Core\Config\ConfigFactoryInterface
  48. */
  49. protected $configFactory;
  50. /**
  51. * The language manager.
  52. *
  53. * @var \Drupal\Core\Language\LanguageManagerInterface
  54. */
  55. protected $languageManager;
  56. /**
  57. * The request stack.
  58. *
  59. * @var \Symfony\Component\HttpFoundation\RequestStack
  60. */
  61. protected $requestStack;
  62. /**
  63. * Constructs a LocaleLookup object.
  64. *
  65. * @param string $langcode
  66. * The language code.
  67. * @param string $context
  68. * The string context.
  69. * @param \Drupal\locale\StringStorageInterface $string_storage
  70. * The string storage.
  71. * @param \Drupal\Core\Cache\CacheBackendInterface $cache
  72. * The cache backend.
  73. * @param \Drupal\Core\Lock\LockBackendInterface $lock
  74. * The lock backend.
  75. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  76. * The config factory.
  77. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
  78. * The language manager.
  79. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
  80. * The request stack.
  81. */
  82. public function __construct($langcode, $context, StringStorageInterface $string_storage, CacheBackendInterface $cache, LockBackendInterface $lock, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager, RequestStack $request_stack) {
  83. $this->langcode = $langcode;
  84. $this->context = (string) $context;
  85. $this->stringStorage = $string_storage;
  86. $this->configFactory = $config_factory;
  87. $this->languageManager = $language_manager;
  88. $this->cache = $cache;
  89. $this->lock = $lock;
  90. $this->tags = ['locale'];
  91. $this->requestStack = $request_stack;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. protected function getCid() {
  97. if (!isset($this->cid)) {
  98. // Add the current user's role IDs to the cache key, this ensures that,
  99. // for example, strings for admin menu items and settings forms are not
  100. // cached for anonymous users.
  101. $user = \Drupal::currentUser();
  102. $rids = $user ? implode(':', $user->getRoles()) : '';
  103. $this->cid = "locale:{$this->langcode}:{$this->context}:$rids";
  104. // Getting the roles from the current user might have resulted in t()
  105. // calls that attempted to get translations from the locale cache. In that
  106. // case they would not go into this method again as
  107. // CacheCollector::lazyLoadCache() already set the loaded flag. They would
  108. // however call resolveCacheMiss() and add that string to the list of
  109. // cache misses that need to be written into the cache. Prevent that by
  110. // resetting that list. All that happens in such a case are a few uncached
  111. // translation lookups.
  112. $this->keysToPersist = [];
  113. }
  114. return $this->cid;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. protected function resolveCacheMiss($offset) {
  120. $translation = $this->stringStorage->findTranslation([
  121. 'language' => $this->langcode,
  122. 'source' => $offset,
  123. 'context' => $this->context,
  124. ]);
  125. if ($translation) {
  126. $value = !empty($translation->translation) ? $translation->translation : TRUE;
  127. }
  128. else {
  129. // We don't have the source string, update the {locales_source} table to
  130. // indicate the string is not translated.
  131. $this->stringStorage->createString([
  132. 'source' => $offset,
  133. 'context' => $this->context,
  134. 'version' => \Drupal::VERSION,
  135. ])->addLocation('path', $this->requestStack->getCurrentRequest()->getRequestUri())->save();
  136. $value = TRUE;
  137. }
  138. // If there is no translation available for the current language then use
  139. // language fallback to try other translations.
  140. if ($value === TRUE) {
  141. $fallbacks = $this->languageManager->getFallbackCandidates(['langcode' => $this->langcode, 'operation' => 'locale_lookup', 'data' => $offset]);
  142. if (!empty($fallbacks)) {
  143. foreach ($fallbacks as $langcode) {
  144. $translation = $this->stringStorage->findTranslation([
  145. 'language' => $langcode,
  146. 'source' => $offset,
  147. 'context' => $this->context,
  148. ]);
  149. if ($translation && !empty($translation->translation)) {
  150. $value = $translation->translation;
  151. break;
  152. }
  153. }
  154. }
  155. }
  156. if (is_string($value) && strpos($value, PoItem::DELIMITER) !== FALSE) {
  157. // Community translations imported from localize.drupal.org as well as
  158. // migrated translations may contain @count[number].
  159. $value = preg_replace('!@count\[\d+\]!', '@count', $value);
  160. }
  161. $this->storage[$offset] = $value;
  162. // Disabling the usage of string caching allows a module to watch for
  163. // the exact list of strings used on a page. From a performance
  164. // perspective that is a really bad idea, so we have no user
  165. // interface for this. Be careful when turning this option off!
  166. if ($this->configFactory->get('locale.settings')->get('cache_strings')) {
  167. $this->persist($offset);
  168. }
  169. return $value;
  170. }
  171. }