Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Translate.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Framework;
8 
13 
21 {
22  const CONFIG_AREA_KEY = 'area';
23  const CONFIG_LOCALE_KEY = 'locale';
24  const CONFIG_SCOPE_KEY = 'scope';
25  const CONFIG_THEME_KEY = 'theme';
26  const CONFIG_MODULE_KEY = 'module';
27 
33  protected $_localeCode;
34 
40  protected $_config;
41 
47  protected $_cacheId;
48 
54  protected $_data = [];
55 
59  protected $_viewDesign;
60 
64  protected $_cache;
65 
69  protected $_viewFileSystem;
70 
74  protected $_moduleList;
75 
79  protected $_modulesReader;
80 
84  protected $_scopeResolver;
85 
90 
94  protected $_locale;
95 
99  protected $_appState;
100 
104  protected $directory;
105 
109  protected $request;
110 
114  protected $_csvParser;
115 
119  protected $packDictionary;
120 
124  private $serializer;
125 
129  private $fileDriver;
130 
149  public function __construct(
150  \Magento\Framework\View\DesignInterface $viewDesign,
151  \Magento\Framework\Cache\FrontendInterface $cache,
152  \Magento\Framework\View\FileSystem $viewFileSystem,
153  \Magento\Framework\Module\ModuleList $moduleList,
154  \Magento\Framework\Module\Dir\Reader $modulesReader,
155  \Magento\Framework\App\ScopeResolverInterface $scopeResolver,
156  \Magento\Framework\Translate\ResourceInterface $translate,
157  \Magento\Framework\Locale\ResolverInterface $locale,
158  \Magento\Framework\App\State $appState,
159  \Magento\Framework\Filesystem $filesystem,
160  \Magento\Framework\App\RequestInterface $request,
161  \Magento\Framework\File\Csv $csvParser,
162  \Magento\Framework\App\Language\Dictionary $packDictionary,
163  DriverInterface $fileDriver = null
164  ) {
165  $this->_viewDesign = $viewDesign;
166  $this->_cache = $cache;
167  $this->_viewFileSystem = $viewFileSystem;
168  $this->_moduleList = $moduleList;
169  $this->_modulesReader = $modulesReader;
170  $this->_scopeResolver = $scopeResolver;
171  $this->_translateResource = $translate;
172  $this->_locale = $locale;
173  $this->_appState = $appState;
174  $this->request = $request;
175  $this->directory = $filesystem->getDirectoryRead(DirectoryList::ROOT);
176  $this->_csvParser = $csvParser;
177  $this->packDictionary = $packDictionary;
178  $this->fileDriver = $fileDriver
179  ?? ObjectManager::getInstance()->get(File::class);
180 
181  $this->_config = [
182  self::CONFIG_AREA_KEY => null,
183  self::CONFIG_LOCALE_KEY => null,
184  self::CONFIG_SCOPE_KEY => null,
185  self::CONFIG_THEME_KEY => null,
186  self::CONFIG_MODULE_KEY => null,
187  ];
188  }
189 
197  public function loadData($area = null, $forceReload = false)
198  {
199  $this->_data = [];
200  if ($area === null) {
201  $area = $this->_appState->getAreaCode();
202  }
203  $this->setConfig(
204  [
205  self::CONFIG_AREA_KEY => $area,
206  ]
207  );
208 
209  if (!$forceReload) {
210  $data = $this->_loadCache();
211  if (false !== $data) {
212  $this->_data = $data;
213  return $this;
214  }
215  }
216 
217  $this->_loadModuleTranslation();
218  $this->_loadPackTranslation();
219  $this->_loadThemeTranslation();
220  $this->_loadDbTranslation();
221 
222  if (!$forceReload) {
223  $this->_saveCache();
224  }
225 
226  return $this;
227  }
228 
235  protected function setConfig($config)
236  {
237  $this->_config = $config;
238  if (!isset($this->_config[self::CONFIG_LOCALE_KEY])) {
239  $this->_config[self::CONFIG_LOCALE_KEY] = $this->getLocale();
240  }
241  if (!isset($this->_config[self::CONFIG_SCOPE_KEY])) {
242  $this->_config[self::CONFIG_SCOPE_KEY] = $this->getScope();
243  }
244  if (!isset($this->_config[self::CONFIG_THEME_KEY])) {
245  $this->_config[self::CONFIG_THEME_KEY] = $this->_viewDesign->getDesignTheme()->getThemePath();
246  }
247  if (!isset($this->_config[self::CONFIG_MODULE_KEY])) {
248  $this->_config[self::CONFIG_MODULE_KEY] = $this->getControllerModuleName();
249  }
250  return $this;
251  }
252 
258  protected function getScope()
259  {
260  $scope = ($this->getConfig(self::CONFIG_AREA_KEY) === 'adminhtml') ? 'admin' : null;
261  return $this->_scopeResolver->getScope($scope)->getCode();
262  }
263 
270  protected function getConfig($key)
271  {
272  if (isset($this->_config[$key])) {
273  return $this->_config[$key];
274  }
275  return null;
276  }
277 
282  protected function getControllerModuleName()
283  {
284  return $this->request->getControllerModule();
285  }
286 
292  protected function _loadModuleTranslation()
293  {
294  $currentModule = $this->getControllerModuleName();
295  $allModulesExceptCurrent = array_diff($this->_moduleList->getNames(), [$currentModule]);
296 
297  $this->loadModuleTranslationByModulesList($allModulesExceptCurrent);
298  $this->loadModuleTranslationByModulesList([$currentModule]);
299  return $this;
300  }
301 
308  protected function loadModuleTranslationByModulesList(array $modules)
309  {
310  foreach ($modules as $module) {
311  $moduleFilePath = $this->_getModuleTranslationFile($module, $this->getLocale());
312  $this->_addData($this->_getFileData($moduleFilePath));
313  }
314  return $this;
315  }
316 
323  protected function _addData($data)
324  {
325  foreach ($data as $key => $value) {
326  if ($key === $value) {
327  if (isset($this->_data[$key])) {
328  unset($this->_data[$key]);
329  }
330  continue;
331  }
332 
333  $key = str_replace('""', '"', $key);
334  $value = str_replace('""', '"', $value);
335 
336  $this->_data[$key] = $value;
337  }
338  return $this;
339  }
340 
346  protected function _loadThemeTranslation()
347  {
348  $file = $this->_getThemeTranslationFile($this->getLocale());
349  if ($file) {
350  $this->_addData($this->_getFileData($file));
351  }
352  return $this;
353  }
354 
360  protected function _loadPackTranslation()
361  {
362  $data = $this->packDictionary->getDictionary($this->getLocale());
363  $this->_addData($data);
364  }
365 
371  protected function _loadDbTranslation()
372  {
373  $data = $this->_translateResource->getTranslationArray(null, $this->getLocale());
374  $this->_addData(array_map('htmlspecialchars_decode', $data));
375  return $this;
376  }
377 
385  protected function _getModuleTranslationFile($moduleName, $locale)
386  {
387  $file = $this->_modulesReader->getModuleDir(Module\Dir::MODULE_I18N_DIR, $moduleName);
388  $file .= '/' . $locale . '.csv';
389  return $file;
390  }
391 
398  protected function _getThemeTranslationFile($locale)
399  {
400  return $this->_viewFileSystem->getLocaleFileName(
401  'i18n' . '/' . $locale . '.csv',
402  $this->_config
403  );
404  }
405 
412  protected function _getFileData($file)
413  {
414  $data = [];
415  if ($this->fileDriver->isExists($file)) {
416  $this->_csvParser->setDelimiter(',');
417  $data = $this->_csvParser->getDataPairs($file);
418  }
419  return $data;
420  }
421 
427  public function getData()
428  {
429  if ($this->_data === null) {
430  return [];
431  }
432  return $this->_data;
433  }
434 
440  public function getLocale()
441  {
442  if (null === $this->_localeCode) {
443  $this->_localeCode = $this->_locale->getLocale();
444  }
445  return $this->_localeCode;
446  }
447 
454  public function setLocale($locale)
455  {
456  $this->_localeCode = $locale;
457  $this->_config[self::CONFIG_LOCALE_KEY] = $locale;
458  return $this;
459  }
460 
466  public function getTheme()
467  {
468  $theme = $this->request->getParam(self::CONFIG_THEME_KEY);
469  if (empty($theme)) {
470  return self::CONFIG_THEME_KEY . $this->getConfig(self::CONFIG_THEME_KEY);
471  }
472  return self::CONFIG_THEME_KEY . $theme['theme_title'];
473  }
474 
480  protected function getCacheId()
481  {
483  $_cacheId .= '_' . $this->_config[self::CONFIG_LOCALE_KEY];
484  $_cacheId .= '_' . $this->_config[self::CONFIG_AREA_KEY];
485  $_cacheId .= '_' . $this->_config[self::CONFIG_SCOPE_KEY];
486  $_cacheId .= '_' . $this->_config[self::CONFIG_THEME_KEY];
487  $_cacheId .= '_' . $this->_config[self::CONFIG_MODULE_KEY];
488 
489  $this->_cacheId = $_cacheId;
490  return $_cacheId;
491  }
492 
498  protected function _loadCache()
499  {
500  $data = $this->_cache->load($this->getCacheId());
501  if ($data) {
502  $data = $this->getSerializer()->unserialize($data);
503  }
504  return $data;
505  }
506 
512  protected function _saveCache()
513  {
514  $this->_cache->save($this->getSerializer()->serialize($this->getData()), $this->getCacheId(), [], false);
515  return $this;
516  }
517 
524  private function getSerializer()
525  {
526  if ($this->serializer === null) {
528  ->get(Serialize\SerializerInterface::class);
529  }
530  return $this->serializer;
531  }
532 }
$config
Definition: fraud_order.php:17
__construct(\Magento\Framework\View\DesignInterface $viewDesign, \Magento\Framework\Cache\FrontendInterface $cache, \Magento\Framework\View\FileSystem $viewFileSystem, \Magento\Framework\Module\ModuleList $moduleList, \Magento\Framework\Module\Dir\Reader $modulesReader, \Magento\Framework\App\ScopeResolverInterface $scopeResolver, \Magento\Framework\Translate\ResourceInterface $translate, \Magento\Framework\Locale\ResolverInterface $locale, \Magento\Framework\App\State $appState, \Magento\Framework\Filesystem $filesystem, \Magento\Framework\App\RequestInterface $request, \Magento\Framework\File\Csv $csvParser, \Magento\Framework\App\Language\Dictionary $packDictionary, DriverInterface $fileDriver=null)
Definition: Translate.php:149
_getModuleTranslationFile($moduleName, $locale)
Definition: Translate.php:385
loadModuleTranslationByModulesList(array $modules)
Definition: Translate.php:308
$value
Definition: gender.phtml:16
loadData($area=null, $forceReload=false)
Definition: Translate.php:197
$theme
$filesystem