Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Data Structures | Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Translate Class Reference
Inheritance diagram for Translate:
TranslateInterface

Data Structures

class  InlineTest
 

Public Member Functions

 __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)
 
 loadData ($area=null, $forceReload=false)
 
 getData ()
 
 getLocale ()
 
 setLocale ($locale)
 
 getTheme ()
 

Data Fields

const CONFIG_AREA_KEY = 'area'
 
const CONFIG_LOCALE_KEY = 'locale'
 
const CONFIG_SCOPE_KEY = 'scope'
 
const CONFIG_THEME_KEY = 'theme'
 
const CONFIG_MODULE_KEY = 'module'
 
- Data Fields inherited from TranslateInterface
const DEFAULT_STRING = 'Translate String'
 

Protected Member Functions

 setConfig ($config)
 
 getScope ()
 
 getConfig ($key)
 
 getControllerModuleName ()
 
 _loadModuleTranslation ()
 
 loadModuleTranslationByModulesList (array $modules)
 
 _addData ($data)
 
 _loadThemeTranslation ()
 
 _loadPackTranslation ()
 
 _loadDbTranslation ()
 
 _getModuleTranslationFile ($moduleName, $locale)
 
 _getThemeTranslationFile ($locale)
 
 _getFileData ($file)
 
 getCacheId ()
 
 _loadCache ()
 
 _saveCache ()
 

Protected Attributes

 $_localeCode
 
 $_config
 
 $_cacheId
 
 $_data = []
 
 $_viewDesign
 
 $_cache
 
 $_viewFileSystem
 
 $_moduleList
 
 $_modulesReader
 
 $_scopeResolver
 
 $_translateResource
 
 $_locale
 
 $_appState
 
 $directory
 
 $request
 
 $_csvParser
 
 $packDictionary
 

Detailed Description

Translate library

@SuppressWarnings(PHPMD.CouplingBetweenObjects) @SuppressWarnings(PHPMD.TooManyFields)

Definition at line 20 of file Translate.php.

Constructor & Destructor Documentation

◆ __construct()

Parameters
\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 | null$fileDriver@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 149 of file Translate.php.

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  }
$filesystem

Member Function Documentation

◆ _addData()

_addData (   $data)
protected

Adding translation data

Parameters
array$data
Returns
$this

Definition at line 323 of file Translate.php.

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  }
$value
Definition: gender.phtml:16

◆ _getFileData()

_getFileData (   $file)
protected

Retrieve data from file

Parameters
string$file
Returns
array

Definition at line 412 of file Translate.php.

413  {
414  $data = [];
415  if ($this->fileDriver->isExists($file)) {
416  $this->_csvParser->setDelimiter(',');
417  $data = $this->_csvParser->getDataPairs($file);
418  }
419  return $data;
420  }

◆ _getModuleTranslationFile()

_getModuleTranslationFile (   $moduleName,
  $locale 
)
protected

Retrieve translation file for module

Parameters
string$moduleName
string$locale
Returns
string

Definition at line 385 of file Translate.php.

386  {
387  $file = $this->_modulesReader->getModuleDir(Module\Dir::MODULE_I18N_DIR, $moduleName);
388  $file .= '/' . $locale . '.csv';
389  return $file;
390  }

◆ _getThemeTranslationFile()

_getThemeTranslationFile (   $locale)
protected

Retrieve translation file for theme

Parameters
string$locale
Returns
string

Definition at line 398 of file Translate.php.

399  {
400  return $this->_viewFileSystem->getLocaleFileName(
401  'i18n' . '/' . $locale . '.csv',
402  $this->_config
403  );
404  }

◆ _loadCache()

_loadCache ( )
protected

Loading data cache

Returns
array|bool

Definition at line 498 of file Translate.php.

499  {
500  $data = $this->_cache->load($this->getCacheId());
501  if ($data) {
502  $data = $this->getSerializer()->unserialize($data);
503  }
504  return $data;
505  }

◆ _loadDbTranslation()

_loadDbTranslation ( )
protected

Loading current translation from DB

Returns
$this

Definition at line 371 of file Translate.php.

372  {
373  $data = $this->_translateResource->getTranslationArray(null, $this->getLocale());
374  $this->_addData(array_map('htmlspecialchars_decode', $data));
375  return $this;
376  }

◆ _loadModuleTranslation()

_loadModuleTranslation ( )
protected

Load data from module translation files

Returns
$this

Definition at line 292 of file Translate.php.

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  }
loadModuleTranslationByModulesList(array $modules)
Definition: Translate.php:308

◆ _loadPackTranslation()

_loadPackTranslation ( )
protected

Load translation dictionary from language packages

Returns
void

Definition at line 360 of file Translate.php.

361  {
362  $data = $this->packDictionary->getDictionary($this->getLocale());
363  $this->_addData($data);
364  }

◆ _loadThemeTranslation()

_loadThemeTranslation ( )
protected

Load current theme translation

Returns
$this

Definition at line 346 of file Translate.php.

347  {
348  $file = $this->_getThemeTranslationFile($this->getLocale());
349  if ($file) {
350  $this->_addData($this->_getFileData($file));
351  }
352  return $this;
353  }

◆ _saveCache()

_saveCache ( )
protected

Saving data cache

Returns
$this

Definition at line 512 of file Translate.php.

513  {
514  $this->_cache->save($this->getSerializer()->serialize($this->getData()), $this->getCacheId(), [], false);
515  return $this;
516  }

◆ getCacheId()

getCacheId ( )
protected

Retrieve cache identifier

Returns
string

Definition at line 480 of file Translate.php.

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  }

◆ getConfig()

getConfig (   $key)
protected

Retrieve config value by key

Parameters
string$key
Returns
mixed

Definition at line 270 of file Translate.php.

271  {
272  if (isset($this->_config[$key])) {
273  return $this->_config[$key];
274  }
275  return null;
276  }

◆ getControllerModuleName()

getControllerModuleName ( )
protected

Retrieve name of the current module

Returns
mixed

Definition at line 282 of file Translate.php.

283  {
284  return $this->request->getControllerModule();
285  }

◆ getData()

getData ( )

Retrieve translation data

Returns
array

Implements TranslateInterface.

Definition at line 427 of file Translate.php.

428  {
429  if ($this->_data === null) {
430  return [];
431  }
432  return $this->_data;
433  }

◆ getLocale()

getLocale ( )

Retrieve locale

Returns
string

Implements TranslateInterface.

Definition at line 440 of file Translate.php.

441  {
442  if (null === $this->_localeCode) {
443  $this->_localeCode = $this->_locale->getLocale();
444  }
445  return $this->_localeCode;
446  }

◆ getScope()

getScope ( )
protected

Retrieve scope code

Returns
string

Definition at line 258 of file Translate.php.

259  {
260  $scope = ($this->getConfig(self::CONFIG_AREA_KEY) === 'adminhtml') ? 'admin' : null;
261  return $this->_scopeResolver->getScope($scope)->getCode();
262  }

◆ getTheme()

getTheme ( )

Retrieve theme code

Returns
string

Implements TranslateInterface.

Definition at line 466 of file Translate.php.

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  }
$theme

◆ loadData()

loadData (   $area = null,
  $forceReload = false 
)

Initialize translation data

Parameters
string | null$area
bool$forceReload
Returns
$this

Implements TranslateInterface.

Definition at line 197 of file Translate.php.

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  }

◆ loadModuleTranslationByModulesList()

loadModuleTranslationByModulesList ( array  $modules)
protected

Load data from module translation files by list of modules

Parameters
array$modules
Returns
$this

Definition at line 308 of file Translate.php.

309  {
310  foreach ($modules as $module) {
311  $moduleFilePath = $this->_getModuleTranslationFile($module, $this->getLocale());
312  $this->_addData($this->_getFileData($moduleFilePath));
313  }
314  return $this;
315  }
_getModuleTranslationFile($moduleName, $locale)
Definition: Translate.php:385

◆ setConfig()

setConfig (   $config)
protected

Initialize configuration

Parameters
array$config
Returns
$this

Definition at line 235 of file Translate.php.

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  }
$config
Definition: fraud_order.php:17

◆ setLocale()

setLocale (   $locale)

Set locale

Parameters
string$locale
Returns
\Magento\Framework\TranslateInterface

Implements TranslateInterface.

Definition at line 454 of file Translate.php.

455  {
456  $this->_localeCode = $locale;
457  $this->_config[self::CONFIG_LOCALE_KEY] = $locale;
458  return $this;
459  }

Field Documentation

◆ $_appState

$_appState
protected

Definition at line 99 of file Translate.php.

◆ $_cache

$_cache
protected

Definition at line 64 of file Translate.php.

◆ $_cacheId

$_cacheId
protected

Definition at line 47 of file Translate.php.

◆ $_config

$_config
protected

Definition at line 40 of file Translate.php.

◆ $_csvParser

$_csvParser
protected

Definition at line 114 of file Translate.php.

◆ $_data

$_data = []
protected

Definition at line 54 of file Translate.php.

◆ $_locale

$_locale
protected

Definition at line 94 of file Translate.php.

◆ $_localeCode

$_localeCode
protected

Definition at line 33 of file Translate.php.

◆ $_moduleList

$_moduleList
protected

Definition at line 74 of file Translate.php.

◆ $_modulesReader

$_modulesReader
protected

Definition at line 79 of file Translate.php.

◆ $_scopeResolver

$_scopeResolver
protected

Definition at line 84 of file Translate.php.

◆ $_translateResource

$_translateResource
protected

Definition at line 89 of file Translate.php.

◆ $_viewDesign

$_viewDesign
protected

Definition at line 59 of file Translate.php.

◆ $_viewFileSystem

$_viewFileSystem
protected

Definition at line 69 of file Translate.php.

◆ $directory

$directory
protected

Definition at line 104 of file Translate.php.

◆ $packDictionary

$packDictionary
protected

Definition at line 119 of file Translate.php.

◆ $request

$request
protected

Definition at line 109 of file Translate.php.

◆ CONFIG_AREA_KEY

const CONFIG_AREA_KEY = 'area'

Definition at line 22 of file Translate.php.

◆ CONFIG_LOCALE_KEY

const CONFIG_LOCALE_KEY = 'locale'

Definition at line 23 of file Translate.php.

◆ CONFIG_MODULE_KEY

const CONFIG_MODULE_KEY = 'module'

Definition at line 26 of file Translate.php.

◆ CONFIG_SCOPE_KEY

const CONFIG_SCOPE_KEY = 'scope'

Definition at line 24 of file Translate.php.

◆ CONFIG_THEME_KEY

const CONFIG_THEME_KEY = 'theme'

Definition at line 25 of file Translate.php.


The documentation for this class was generated from the following file: