Translate.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Translation\Model\ResourceModel;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\App\DeploymentConfig;
  9. use Magento\Framework\App\Config;
  10. use Magento\Translation\App\Config\Type\Translation;
  11. class Translate extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb implements
  12. \Magento\Framework\Translate\ResourceInterface
  13. {
  14. /**
  15. * @var \Magento\Framework\App\ScopeResolverInterface
  16. */
  17. protected $scopeResolver;
  18. /**
  19. * @var null|string
  20. */
  21. protected $scope;
  22. /**
  23. * @var Config
  24. */
  25. private $appConfig;
  26. /**
  27. * @var DeploymentConfig
  28. */
  29. private $deployedConfig;
  30. /**
  31. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  32. * @param \Magento\Framework\App\ScopeResolverInterface $scopeResolver
  33. * @param string $connectionName
  34. * @param null|string $scope
  35. */
  36. public function __construct(
  37. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  38. \Magento\Framework\App\ScopeResolverInterface $scopeResolver,
  39. $connectionName = null,
  40. $scope = null
  41. ) {
  42. $this->scopeResolver = $scopeResolver;
  43. $this->scope = $scope;
  44. parent::__construct($context, $connectionName);
  45. }
  46. /**
  47. * Define main table
  48. *
  49. * @return void
  50. */
  51. protected function _construct()
  52. {
  53. $this->_init('translation', 'key_id');
  54. }
  55. /**
  56. * Retrieve translation array for store / locale code
  57. *
  58. * @param int $storeId
  59. * @param string $locale
  60. * @return array
  61. */
  62. public function getTranslationArray($storeId = null, $locale = null)
  63. {
  64. if ($storeId === null) {
  65. $storeId = $this->getStoreId();
  66. }
  67. $locale = (string) $locale;
  68. $data = $this->getAppConfig()->get(
  69. Translation::CONFIG_TYPE,
  70. $locale . '/' . $this->getStoreCode($storeId),
  71. []
  72. );
  73. $connection = $this->getConnection();
  74. if ($connection) {
  75. $select = $connection->select()
  76. ->from($this->getMainTable(), ['string', 'translate'])
  77. ->where('store_id IN (0 , :store_id)')
  78. ->where('locale = :locale')
  79. ->order('store_id');
  80. $bind = [':locale' => $locale, ':store_id' => $storeId];
  81. $dbData = $connection->fetchPairs($select, $bind);
  82. $data = array_replace($data, $dbData);
  83. }
  84. return $data;
  85. }
  86. /**
  87. * Retrieve translations array by strings
  88. *
  89. * @param array $strings
  90. * @param int|null $storeId
  91. * @return array
  92. */
  93. public function getTranslationArrayByStrings(array $strings, $storeId = null)
  94. {
  95. if ($storeId === null) {
  96. $storeId = $this->getStoreId();
  97. }
  98. $connection = $this->getConnection();
  99. if (!$connection) {
  100. return [];
  101. }
  102. if (empty($strings)) {
  103. return [];
  104. }
  105. $bind = [':store_id' => $storeId];
  106. $select = $connection->select()
  107. ->from($this->getMainTable(), ['string', 'translate'])
  108. ->where('string IN (?)', $strings)
  109. ->where('store_id = :store_id');
  110. return $connection->fetchPairs($select, $bind);
  111. }
  112. /**
  113. * Retrieve table checksum
  114. *
  115. * @return int
  116. */
  117. public function getMainChecksum()
  118. {
  119. return $this->getChecksum($this->getMainTable());
  120. }
  121. /**
  122. * Get connection
  123. *
  124. * @return \Magento\Framework\DB\Adapter\AdapterInterface|false
  125. */
  126. public function getConnection()
  127. {
  128. if (!$this->getDeployedConfig()->isDbAvailable()) {
  129. return false;
  130. }
  131. return parent::getConnection();
  132. }
  133. /**
  134. * Retrieve current store identifier
  135. *
  136. * @return int
  137. */
  138. protected function getStoreId()
  139. {
  140. return $this->scopeResolver->getScope($this->scope)->getId();
  141. }
  142. /**
  143. * Retrieve store code by store id
  144. *
  145. * @param int $storeId
  146. * @return string
  147. */
  148. private function getStoreCode($storeId)
  149. {
  150. return $this->scopeResolver->getScope($storeId)->getCode();
  151. }
  152. /**
  153. * @deprecated 100.1.2
  154. * @return DeploymentConfig
  155. */
  156. private function getDeployedConfig()
  157. {
  158. if ($this->deployedConfig === null) {
  159. $this->deployedConfig = ObjectManager::getInstance()->get(DeploymentConfig::class);
  160. }
  161. return $this->deployedConfig;
  162. }
  163. /**
  164. * @deprecated 100.1.2
  165. * @return Config
  166. */
  167. private function getAppConfig()
  168. {
  169. if ($this->appConfig === null) {
  170. $this->appConfig = ObjectManager::getInstance()->get(Config::class);
  171. }
  172. return $this->appConfig;
  173. }
  174. }