LocaleTranslation.php 4.4 KB

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