annotate core/modules/locale/src/LocaleTranslation.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\locale;
Chris@0 4
Chris@0 5 use Drupal\Core\Cache\CacheBackendInterface;
Chris@0 6 use Drupal\Core\Config\ConfigFactoryInterface;
Chris@0 7 use Drupal\Core\DestructableInterface;
Chris@0 8 use Drupal\Core\Language\LanguageInterface;
Chris@0 9 use Drupal\Core\Language\LanguageManagerInterface;
Chris@0 10 use Drupal\Core\Lock\LockBackendInterface;
Chris@0 11 use Drupal\Core\StringTranslation\Translator\TranslatorInterface;
Chris@0 12 use Symfony\Component\HttpFoundation\RequestStack;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * String translator using the locale module.
Chris@0 16 *
Chris@0 17 * Full featured translation system using locale's string storage and
Chris@0 18 * database caching.
Chris@0 19 */
Chris@0 20 class LocaleTranslation implements TranslatorInterface, DestructableInterface {
Chris@0 21
Chris@0 22 /**
Chris@0 23 * Storage for strings.
Chris@0 24 *
Chris@0 25 * @var \Drupal\locale\StringStorageInterface
Chris@0 26 */
Chris@0 27 protected $storage;
Chris@0 28
Chris@0 29 /**
Chris@0 30 * The configuration factory.
Chris@0 31 *
Chris@0 32 * @var \Drupal\Core\Config\ConfigFactoryInterface
Chris@0 33 */
Chris@0 34 protected $configFactory;
Chris@0 35
Chris@0 36 /**
Chris@0 37 * Cached translations.
Chris@0 38 *
Chris@0 39 * @var array
Chris@0 40 * Array of \Drupal\locale\LocaleLookup objects indexed by language code
Chris@0 41 * and context.
Chris@0 42 */
Chris@0 43 protected $translations = [];
Chris@0 44
Chris@0 45 /**
Chris@0 46 * The cache backend that should be used.
Chris@0 47 *
Chris@0 48 * @var \Drupal\Core\Cache\CacheBackendInterface
Chris@0 49 */
Chris@0 50 protected $cache;
Chris@0 51
Chris@0 52 /**
Chris@0 53 * The lock backend that should be used.
Chris@0 54 *
Chris@0 55 * @var \Drupal\Core\Lock\LockBackendInterface
Chris@0 56 */
Chris@0 57 protected $lock;
Chris@0 58
Chris@0 59 /**
Chris@0 60 * The translate english configuration value.
Chris@0 61 *
Chris@0 62 * @var bool
Chris@0 63 */
Chris@0 64 protected $translateEnglish;
Chris@0 65
Chris@0 66 /**
Chris@0 67 * The language manager.
Chris@0 68 *
Chris@0 69 * @var \Drupal\Core\Language\LanguageManagerInterface
Chris@0 70 */
Chris@0 71 protected $languageManager;
Chris@0 72
Chris@0 73 /**
Chris@0 74 * The request stack.
Chris@0 75 *
Chris@0 76 * @var \Symfony\Component\HttpFoundation\RequestStack
Chris@0 77 */
Chris@0 78 protected $requestStack;
Chris@0 79
Chris@0 80 /**
Chris@0 81 * Constructs a translator using a string storage.
Chris@0 82 *
Chris@0 83 * @param \Drupal\locale\StringStorageInterface $storage
Chris@0 84 * Storage to use when looking for new translations.
Chris@0 85 * @param \Drupal\Core\Cache\CacheBackendInterface $cache
Chris@0 86 * The cache backend.
Chris@0 87 * @param \Drupal\Core\Lock\LockBackendInterface $lock
Chris@0 88 * The lock backend.
Chris@0 89 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@0 90 * The config factory.
Chris@0 91 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
Chris@0 92 * The language manager.
Chris@0 93 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
Chris@0 94 * The request stack.
Chris@0 95 */
Chris@0 96 public function __construct(StringStorageInterface $storage, CacheBackendInterface $cache, LockBackendInterface $lock, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager, RequestStack $request_stack) {
Chris@0 97 $this->storage = $storage;
Chris@0 98 $this->cache = $cache;
Chris@0 99 $this->lock = $lock;
Chris@0 100 $this->configFactory = $config_factory;
Chris@0 101 $this->languageManager = $language_manager;
Chris@0 102 $this->requestStack = $request_stack;
Chris@0 103 }
Chris@0 104
Chris@0 105 /**
Chris@0 106 * {@inheritdoc}
Chris@0 107 */
Chris@0 108 public function getStringTranslation($langcode, $string, $context) {
Chris@0 109 // If the language is not suitable for locale module, just return.
Chris@0 110 if ($langcode == LanguageInterface::LANGCODE_SYSTEM || ($langcode == 'en' && !$this->canTranslateEnglish())) {
Chris@0 111 return FALSE;
Chris@0 112 }
Chris@0 113 // Strings are cached by langcode, context and roles, using instances of the
Chris@0 114 // LocaleLookup class to handle string lookup and caching.
Chris@0 115 if (!isset($this->translations[$langcode][$context])) {
Chris@0 116 $this->translations[$langcode][$context] = new LocaleLookup($langcode, $context, $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack);
Chris@0 117 }
Chris@0 118 $translation = $this->translations[$langcode][$context]->get($string);
Chris@0 119 return $translation === TRUE ? FALSE : $translation;
Chris@0 120 }
Chris@0 121
Chris@0 122 /**
Chris@0 123 * Gets translate english configuration value.
Chris@0 124 *
Chris@0 125 * @return bool
Chris@0 126 * TRUE if english should be translated, FALSE if not.
Chris@0 127 */
Chris@0 128 protected function canTranslateEnglish() {
Chris@0 129 if (!isset($this->translateEnglish)) {
Chris@0 130 $this->translateEnglish = $this->configFactory->get('locale.settings')->get('translate_english');
Chris@0 131 }
Chris@0 132 return $this->translateEnglish;
Chris@0 133 }
Chris@0 134
Chris@0 135 /**
Chris@0 136 * {@inheritdoc}
Chris@0 137 */
Chris@0 138 public function reset() {
Chris@0 139 unset($this->translateEnglish);
Chris@0 140 $this->translations = [];
Chris@0 141 }
Chris@0 142
Chris@0 143 /**
Chris@0 144 * {@inheritdoc}
Chris@0 145 */
Chris@0 146 public function destruct() {
Chris@0 147 foreach ($this->translations as $context) {
Chris@0 148 foreach ($context as $lookup) {
Chris@0 149 if ($lookup instanceof DestructableInterface) {
Chris@0 150 $lookup->destruct();
Chris@0 151 }
Chris@0 152 }
Chris@0 153 }
Chris@0 154 }
Chris@0 155
Chris@0 156 }