tmgmt.entity.translator.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /*
  3. * @file
  4. * Contains translator entity class.
  5. */
  6. /**
  7. * Entity class for the tmgmt_translator entity.
  8. *
  9. * @ingroup tmgmt_translator
  10. */
  11. class TMGMTTranslator extends Entity {
  12. /**
  13. * The ID of the translator.
  14. *
  15. * @var integer
  16. */
  17. public $tid;
  18. /**
  19. * Machine readable name of the translator.
  20. *
  21. * @var string
  22. */
  23. public $name;
  24. /**
  25. * Label of the translator.
  26. *
  27. * @var string
  28. */
  29. public $label;
  30. /**
  31. * Description of the translator.
  32. *
  33. * @var string
  34. */
  35. public $description;
  36. /**
  37. * Weight of the translator.
  38. *
  39. * @var int
  40. */
  41. public $weight;
  42. /**
  43. * Plugin name of the translator.
  44. *
  45. * @type string
  46. */
  47. public $plugin;
  48. /**
  49. * Translator type specific settings.
  50. *
  51. * @var array
  52. */
  53. public $settings;
  54. /**
  55. * The supported target languages caches.
  56. *
  57. * @var array
  58. */
  59. protected $languageCache;
  60. /**
  61. * The supported language pairs caches.
  62. *
  63. * @var array
  64. */
  65. protected $languagePairsCache;
  66. /**
  67. * Whether the language cache in the database is outdated.
  68. *
  69. * @var boolean
  70. */
  71. protected $languageCacheOutdated;
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function __construct(array $values = array()) {
  76. parent::__construct($values, 'tmgmt_translator');
  77. }
  78. /**
  79. * Returns the translator plugin controller of this translator.
  80. *
  81. * @return TMGMTTranslatorPluginControllerInterface
  82. */
  83. public function getController() {
  84. if (!empty($this->plugin)) {
  85. return tmgmt_translator_plugin_controller($this->plugin);
  86. }
  87. return FALSE;
  88. }
  89. /**
  90. * Returns the supported target languages for this translator.
  91. *
  92. * @return array
  93. * An array of supported target languages in ISO format.
  94. */
  95. public function getSupportedTargetLanguages($source_language) {
  96. if ($controller = $this->getController()) {
  97. if (isset($this->pluginInfo['cache languages']) && empty($this->pluginInfo['cache languages'])) {
  98. // This plugin doesn't support language caching.
  99. return $controller->getSupportedTargetLanguages($this, $source_language);
  100. }
  101. else {
  102. // Retrieve the supported languages from the cache.
  103. if (empty($this->languageCache) && $cache = cache_get('languages:' . $this->name, 'cache_tmgmt')) {
  104. $this->languageCache = $cache->data;
  105. }
  106. // Even if we successfully queried the cache it might not have an entry
  107. // for our source language yet.
  108. if (!isset($this->languageCache[$source_language])) {
  109. $this->languageCache[$source_language] = $controller->getSupportedTargetLanguages($this, $source_language);
  110. $this->languageCacheOutdated = TRUE;
  111. }
  112. }
  113. return $this->languageCache[$source_language];
  114. }
  115. }
  116. /**
  117. * Gets the supported language pairs for this translator.
  118. *
  119. * @return array
  120. * List of language pairs where a pair is an associative array of
  121. * source_language and target_language.
  122. * Example:
  123. * array(
  124. * array('source_language' => 'en-US', 'target_language' => 'de-DE'),
  125. * array('source_language' => 'en-US', 'target_language' => 'de-CH'),
  126. * )
  127. */
  128. public function getSupportedLanguagePairs() {
  129. if ($controller = $this->getController()) {
  130. if (isset($this->pluginInfo['cache languages']) && empty($this->pluginInfo['cache languages'])) {
  131. // This plugin doesn't support language caching.
  132. return $controller->getSupportedLanguagePairs($this);
  133. }
  134. else {
  135. // Retrieve the supported languages from the cache.
  136. if (empty($this->languagePairsCache) && $cache = cache_get('language_pairs:' . $this->name, 'cache_tmgmt')) {
  137. $this->languagePairsCache = $cache->data;
  138. }
  139. // Even if we successfully queried the cache data might not be yet
  140. // available.
  141. if (empty($this->languagePairsCache)) {
  142. $this->languagePairsCache = $controller->getSupportedLanguagePairs($this);
  143. $this->languageCacheOutdated = TRUE;
  144. }
  145. }
  146. return $this->languagePairsCache;
  147. }
  148. }
  149. /**
  150. * Clears the language cache for this translator.
  151. */
  152. public function clearLanguageCache() {
  153. cache_clear_all('languages:' . $this->name, 'cache_tmgmt');
  154. cache_clear_all('language_pairs:' . $this->name, 'cache_tmgmt');
  155. }
  156. /**
  157. * Check whether this translator can handle a particular translation job.
  158. *
  159. * @param $job
  160. * The TMGMTJob entity that should be translated.
  161. *
  162. * @return boolean
  163. * TRUE if the job can be processed and translated, FALSE otherwise.
  164. */
  165. public function canTranslate(TMGMTJob $job) {
  166. if ($controller = $this->getController()) {
  167. return $controller->canTranslate($this, $job);
  168. }
  169. return FALSE;
  170. }
  171. /**
  172. * Checks whether a translator is available.
  173. *
  174. * @return boolean
  175. * TRUE if the translator plugin is available, FALSE otherwise.
  176. */
  177. public function isAvailable() {
  178. if ($controller = $this->getController()) {
  179. return $controller->isAvailable($this);
  180. }
  181. return FALSE;
  182. }
  183. /**
  184. * Returns if the plugin has any settings for this job.
  185. */
  186. public function hasCheckoutSettings(TMGMTJob $job) {
  187. if ($controller = $this->getController()) {
  188. return $controller->hasCheckoutSettings($job);
  189. }
  190. return FALSE;
  191. }
  192. /**
  193. * @todo Remove this once http://drupal.org/node/1420364 is done.
  194. */
  195. public function getNotAvailableReason() {
  196. if ($controller = $this->getController()) {
  197. return $controller->getNotAvailableReason($this);
  198. }
  199. return FALSE;
  200. }
  201. /**
  202. * @todo Remove this once http://drupal.org/node/1420364 is done.
  203. */
  204. public function getNotCanTranslateReason(TMGMTJob $job) {
  205. if ($controller = $this->getController()) {
  206. return $controller->getNotCanTranslateReason($job);
  207. }
  208. return FALSE;
  209. }
  210. /**
  211. * Retrieves a setting value from the translator settings. Pulls the default
  212. * values (if defined) from the plugin controller.
  213. *
  214. * @param $name
  215. * The name of the setting.
  216. *
  217. * @return
  218. * The setting value or $default if the setting value is not set. Returns
  219. * NULL if the setting does not exist at all.
  220. */
  221. public function getSetting($name) {
  222. if (isset($this->settings[$name])) {
  223. return $this->settings[$name];
  224. }
  225. elseif ($controller = $this->getController()) {
  226. $defaults = $controller->defaultSettings();
  227. if (isset($defaults[$name])) {
  228. return $defaults[$name];
  229. }
  230. }
  231. }
  232. /**
  233. * Maps local language to remote language.
  234. *
  235. * @param $language
  236. * Local language code.
  237. *
  238. * @return string
  239. * Remote language code.
  240. *
  241. * @ingroup tmgmt_remote_languages_mapping
  242. */
  243. public function mapToRemoteLanguage($language) {
  244. return $this->getController()->mapToRemoteLanguage($this, $language);
  245. }
  246. /**
  247. * Maps remote language to local language.
  248. *
  249. * @param $language
  250. * Remote language code.
  251. *
  252. * @return string
  253. * Local language code.
  254. *
  255. * @ingroup tmgmt_remote_languages_mapping
  256. */
  257. public function mapToLocalLanguage($language) {
  258. return $this->getController()->mapToLocalLanguage($this, $language);
  259. }
  260. /**
  261. * Updates the language cache if it has changed.
  262. */
  263. public function __destruct() {
  264. if ($controller = $this->getController()) {
  265. $info = $controller->pluginInfo();
  266. if (!isset($info['language cache']) || !empty($info['language cache']) && !empty($this->languageCacheOutdated)) {
  267. cache_set('languages:' . $this->name, $this->languageCache, 'cache_tmgmt');
  268. cache_set('language_pairs:' . $this->name, $this->languagePairsCache, 'cache_tmgmt');
  269. }
  270. }
  271. }
  272. }