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