123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace Drupal\locale;
- use Drupal\Core\Cache\CacheBackendInterface;
- use Drupal\Core\Config\ConfigFactoryInterface;
- use Drupal\Core\DestructableInterface;
- use Drupal\Core\Language\LanguageInterface;
- use Drupal\Core\Language\LanguageManagerInterface;
- use Drupal\Core\Lock\LockBackendInterface;
- use Drupal\Core\StringTranslation\Translator\TranslatorInterface;
- use Symfony\Component\HttpFoundation\RequestStack;
- /**
- * String translator using the locale module.
- *
- * Full featured translation system using locale's string storage and
- * database caching.
- */
- class LocaleTranslation implements TranslatorInterface, DestructableInterface {
- /**
- * Storage for strings.
- *
- * @var \Drupal\locale\StringStorageInterface
- */
- protected $storage;
- /**
- * The configuration factory.
- *
- * @var \Drupal\Core\Config\ConfigFactoryInterface
- */
- protected $configFactory;
- /**
- * Cached translations.
- *
- * @var array
- * Array of \Drupal\locale\LocaleLookup objects indexed by language code
- * and context.
- */
- protected $translations = [];
- /**
- * 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 translate english configuration value.
- *
- * @var bool
- */
- protected $translateEnglish;
- /**
- * The language manager.
- *
- * @var \Drupal\Core\Language\LanguageManagerInterface
- */
- protected $languageManager;
- /**
- * The request stack.
- *
- * @var \Symfony\Component\HttpFoundation\RequestStack
- */
- protected $requestStack;
- /**
- * Constructs a translator using a string storage.
- *
- * @param \Drupal\locale\StringStorageInterface $storage
- * Storage to use when looking for new translations.
- * @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(StringStorageInterface $storage, CacheBackendInterface $cache, LockBackendInterface $lock, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager, RequestStack $request_stack) {
- $this->storage = $storage;
- $this->cache = $cache;
- $this->lock = $lock;
- $this->configFactory = $config_factory;
- $this->languageManager = $language_manager;
- $this->requestStack = $request_stack;
- }
- /**
- * {@inheritdoc}
- */
- public function getStringTranslation($langcode, $string, $context) {
- // If the language is not suitable for locale module, just return.
- if ($langcode == LanguageInterface::LANGCODE_SYSTEM || ($langcode == 'en' && !$this->canTranslateEnglish())) {
- return FALSE;
- }
- // Strings are cached by langcode, context and roles, using instances of the
- // LocaleLookup class to handle string lookup and caching.
- if (!isset($this->translations[$langcode][$context])) {
- $this->translations[$langcode][$context] = new LocaleLookup($langcode, $context, $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack);
- }
- $translation = $this->translations[$langcode][$context]->get($string);
- return $translation === TRUE ? FALSE : $translation;
- }
- /**
- * Gets translate english configuration value.
- *
- * @return bool
- * TRUE if english should be translated, FALSE if not.
- */
- protected function canTranslateEnglish() {
- if (!isset($this->translateEnglish)) {
- $this->translateEnglish = $this->configFactory->get('locale.settings')->get('translate_english');
- }
- return $this->translateEnglish;
- }
- /**
- * {@inheritdoc}
- */
- public function reset() {
- unset($this->translateEnglish);
- $this->translations = [];
- }
- /**
- * {@inheritdoc}
- */
- public function destruct() {
- foreach ($this->translations as $context) {
- foreach ($context as $lookup) {
- if ($lookup instanceof DestructableInterface) {
- $lookup->destruct();
- }
- }
- }
- }
- }
|