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 
12 
14  \Magento\Framework\Translate\ResourceInterface
15 {
19  protected $scopeResolver;
20 
24  protected $scope;
25 
29  private $appConfig;
30 
34  private $deployedConfig;
35 
42  public function __construct(
43  \Magento\Framework\Model\ResourceModel\Db\Context $context,
44  \Magento\Framework\App\ScopeResolverInterface $scopeResolver,
45  $connectionName = null,
46  $scope = null
47  ) {
48  $this->scopeResolver = $scopeResolver;
49  $this->scope = $scope;
50  parent::__construct($context, $connectionName);
51  }
52 
58  protected function _construct()
59  {
60  $this->_init('translation', 'key_id');
61  }
62 
70  public function getTranslationArray($storeId = null, $locale = null)
71  {
72  if ($storeId === null) {
73  $storeId = $this->getStoreId();
74  }
75  $locale = (string) $locale;
76 
77  $data = $this->getAppConfig()->get(
79  $locale . '/' . $this->getStoreCode($storeId),
80  []
81  );
82  $connection = $this->getConnection();
83  if ($connection) {
84  $select = $connection->select()
85  ->from($this->getMainTable(), ['string', 'translate'])
86  ->where('store_id IN (0 , :store_id)')
87  ->where('locale = :locale')
88  ->order('store_id');
89  $bind = [':locale' => $locale, ':store_id' => $storeId];
90  $dbData = $connection->fetchPairs($select, $bind);
91  $data = array_replace($data, $dbData);
92  }
93  return $data;
94  }
95 
103  public function getTranslationArrayByStrings(array $strings, $storeId = null)
104  {
105  if ($storeId === null) {
106  $storeId = $this->getStoreId();
107  }
108 
109  $connection = $this->getConnection();
110  if (!$connection) {
111  return [];
112  }
113 
114  if (empty($strings)) {
115  return [];
116  }
117 
118  $bind = [':store_id' => $storeId];
119  $select = $connection->select()
120  ->from($this->getMainTable(), ['string', 'translate'])
121  ->where('string IN (?)', $strings)
122  ->where('store_id = :store_id');
123 
124  return $connection->fetchPairs($select, $bind);
125  }
126 
132  public function getMainChecksum()
133  {
134  return $this->getChecksum($this->getMainTable());
135  }
136 
142  public function getConnection()
143  {
144  if (!$this->getDeployedConfig()->isDbAvailable()) {
145  return false;
146  }
147  return parent::getConnection();
148  }
149 
155  protected function getStoreId()
156  {
157  return $this->scopeResolver->getScope($this->scope)->getId();
158  }
159 
166  private function getStoreCode($storeId)
167  {
168  return $this->scopeResolver->getScope($storeId)->getCode();
169  }
170 
175  private function getDeployedConfig()
176  {
177  if ($this->deployedConfig === null) {
178  $this->deployedConfig = ObjectManager::getInstance()->get(DeploymentConfig::class);
179  }
180  return $this->deployedConfig;
181  }
182 
187  private function getAppConfig()
188  {
189  if ($this->appConfig === null) {
190  $this->appConfig = ObjectManager::getInstance()->get(Config::class);
191  }
192  return $this->appConfig;
193  }
194 }
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\App\ScopeResolverInterface $scopeResolver, $connectionName=null, $scope=null)
Definition: Translate.php:42
getTranslationArray($storeId=null, $locale=null)
Definition: Translate.php:70
getTranslationArrayByStrings(array $strings, $storeId=null)
Definition: Translate.php:103
$connection
Definition: bulk.php:13