3 namespace Drupal\locale;
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\DestructableInterface;
8 use Drupal\Core\Language\LanguageInterface;
9 use Drupal\Core\Language\LanguageManagerInterface;
10 use Drupal\Core\Lock\LockBackendInterface;
11 use Drupal\Core\StringTranslation\Translator\TranslatorInterface;
12 use Symfony\Component\HttpFoundation\RequestStack;
15 * String translator using the locale module.
17 * Full featured translation system using locale's string storage and
20 class LocaleTranslation implements TranslatorInterface, DestructableInterface {
23 * Storage for strings.
25 * @var \Drupal\locale\StringStorageInterface
30 * The configuration factory.
32 * @var \Drupal\Core\Config\ConfigFactoryInterface
34 protected $configFactory;
37 * Cached translations.
40 * Array of \Drupal\locale\LocaleLookup objects indexed by language code
43 protected $translations = [];
46 * The cache backend that should be used.
48 * @var \Drupal\Core\Cache\CacheBackendInterface
53 * The lock backend that should be used.
55 * @var \Drupal\Core\Lock\LockBackendInterface
60 * The translate english configuration value.
64 protected $translateEnglish;
67 * The language manager.
69 * @var \Drupal\Core\Language\LanguageManagerInterface
71 protected $languageManager;
76 * @var \Symfony\Component\HttpFoundation\RequestStack
78 protected $requestStack;
81 * Constructs a translator using a string storage.
83 * @param \Drupal\locale\StringStorageInterface $storage
84 * Storage to use when looking for new translations.
85 * @param \Drupal\Core\Cache\CacheBackendInterface $cache
87 * @param \Drupal\Core\Lock\LockBackendInterface $lock
89 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
91 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
92 * The language manager.
93 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
96 public function __construct(StringStorageInterface $storage, CacheBackendInterface $cache, LockBackendInterface $lock, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager, RequestStack $request_stack) {
97 $this->storage = $storage;
98 $this->cache = $cache;
100 $this->configFactory = $config_factory;
101 $this->languageManager = $language_manager;
102 $this->requestStack = $request_stack;
108 public function getStringTranslation($langcode, $string, $context) {
109 // If the language is not suitable for locale module, just return.
110 if ($langcode == LanguageInterface::LANGCODE_SYSTEM || ($langcode == 'en' && !$this->canTranslateEnglish())) {
113 // Strings are cached by langcode, context and roles, using instances of the
114 // LocaleLookup class to handle string lookup and caching.
115 if (!isset($this->translations[$langcode][$context])) {
116 $this->translations[$langcode][$context] = new LocaleLookup($langcode, $context, $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack);
118 $translation = $this->translations[$langcode][$context]->get($string);
119 return $translation === TRUE ? FALSE : $translation;
123 * Gets translate english configuration value.
126 * TRUE if english should be translated, FALSE if not.
128 protected function canTranslateEnglish() {
129 if (!isset($this->translateEnglish)) {
130 $this->translateEnglish = $this->configFactory->get('locale.settings')->get('translate_english');
132 return $this->translateEnglish;
138 public function reset() {
139 unset($this->translateEnglish);
140 $this->translations = [];
146 public function destruct() {
147 foreach ($this->translations as $context) {
148 foreach ($context as $lookup) {
149 if ($lookup instanceof DestructableInterface) {