tmgmt.plugin.translator.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * @file
  4. * Contains the abstract translator base plugin class.
  5. */
  6. /**
  7. * Default controller class for service plugins.
  8. *
  9. * @ingroup tmgmt_translator
  10. */
  11. abstract class TMGMTDefaultTranslatorPluginController extends TMGMTPluginBase implements TMGMTTranslatorPluginControllerInterface {
  12. protected $supportedRemoteLanguages = array();
  13. protected $remoteLanguagesMappings = array();
  14. /**
  15. * Characters that indicate the beginning of an escaped string.
  16. *
  17. * @var string
  18. */
  19. protected $escapeStart = '';
  20. /**
  21. * Characters that indicate the end of an escaped string.
  22. *
  23. * @var string
  24. */
  25. protected $escapeEnd = '';
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function isAvailable(TMGMTTranslator $translator) {
  30. // Assume that the translation service is always available.
  31. return TRUE;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function canTranslate(TMGMTTranslator $translator, TMGMTJob $job) {
  37. // The job is only translatable if the translator is available too.
  38. if ($this->isAvailable($translator) && array_key_exists($job->target_language, $translator->getSupportedTargetLanguages($job->source_language))) {
  39. // We can only translate this job if the target language of the job is in
  40. // one of the supported languages.
  41. return TRUE;
  42. }
  43. return FALSE;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function abortTranslation(TMGMTJob $job) {
  49. // Assume that we can abort a translation job at any time.
  50. $job->aborted();
  51. return TRUE;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function getDefaultRemoteLanguagesMappings() {
  57. return array();
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getSupportedRemoteLanguages(TMGMTTranslator $translator) {
  63. return array();
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function getRemoteLanguagesMappings(TMGMTTranslator $translator) {
  69. if (!empty($this->remoteLanguagesMappings)) {
  70. return $this->remoteLanguagesMappings;
  71. }
  72. foreach (language_list() as $language => $info) {
  73. $this->remoteLanguagesMappings[$language] = $this->mapToRemoteLanguage($translator, $language);
  74. }
  75. return $this->remoteLanguagesMappings;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function mapToRemoteLanguage(TMGMTTranslator $translator, $language) {
  81. if (!tmgmt_provide_remote_languages_mappings($translator)) {
  82. return $language;
  83. }
  84. if (!empty($translator->settings['remote_languages_mappings'][$language])) {
  85. return $translator->settings['remote_languages_mappings'][$language];
  86. }
  87. $default_mappings = $this->getDefaultRemoteLanguagesMappings();
  88. if (isset($default_mappings[$language])) {
  89. return $default_mappings[$language];
  90. }
  91. return $language;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function mapToLocalLanguage(TMGMTTranslator $translator, $language) {
  97. if (!tmgmt_provide_remote_languages_mappings($translator)) {
  98. return $language;
  99. }
  100. if (isset($translator->settings['remote_languages_mappings']) && is_array($translator->settings['remote_languages_mappings'])) {
  101. $mappings = $translator->settings['remote_languages_mappings'];
  102. }
  103. else {
  104. $mappings = $this->getDefaultRemoteLanguagesMappings();
  105. }
  106. if ($remote_language = array_search($language, $mappings)) {
  107. return $remote_language;
  108. }
  109. return $language;
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function getSupportedTargetLanguages(TMGMTTranslator $translator, $source_language) {
  115. $languages = entity_metadata_language_list();
  116. unset($languages[LANGUAGE_NONE], $languages[$source_language]);
  117. return drupal_map_assoc(array_keys($languages));
  118. }
  119. /**
  120. * {@inheritdoc}
  121. *
  122. * Default implementation that gets target languages for each remote language.
  123. * This approach is ineffective and therefore it is advised that a plugin
  124. * should provide own implementation.
  125. */
  126. public function getSupportedLanguagePairs(TMGMTTranslator $translator) {
  127. $language_pairs = array();
  128. foreach ($this->getSupportedRemoteLanguages($translator) as $source_language) {
  129. foreach ($this->getSupportedTargetLanguages($translator, $source_language) as $target_language) {
  130. $language_pairs[] = array('source_language' => $source_language, 'target_language' => $target_language);
  131. }
  132. }
  133. return $language_pairs;
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function getNotCanTranslateReason(TMGMTJob $job) {
  139. $wrapper = entity_metadata_wrapper('tmgmt_job', $job);
  140. return t('@translator can not translate from @source to @target.', array('@translator' => $job->getTranslator()->label(), '@source' => $wrapper->source_language->label(), '@target' => $wrapper->target_language->label()));
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function getNotAvailableReason(TMGMTTranslator $translator) {
  146. return t('@translator is not available. Make sure it is properly !configured.', array('@translator' => $this->pluginInfo['label'], '!configured' => l(t('configured'), 'admin/config/regional/tmgmt_translator/manage/' . $translator->name)));
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function defaultSettings() {
  152. $defaults = array('auto_accept' => FALSE);
  153. // Check if any default settings are defined in the plugin info.
  154. if (isset($this->pluginInfo['default settings'])) {
  155. return array_merge($defaults, $this->pluginInfo['default settings']);
  156. }
  157. return $defaults;
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function hasCheckoutSettings(TMGMTJob $job) {
  163. return TRUE;
  164. }
  165. /**
  166. * {@inheritdoc}
  167. */
  168. public function acceptetDataItem(TMGMTJobItem $job_item, array $key) {
  169. return TRUE;
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. public function escapeText(array $data_item) {
  175. if (empty($data_item['#escape'])) {
  176. return $data_item['#text'];
  177. }
  178. $text = $data_item['#text'];
  179. $escape = $data_item['#escape'];
  180. // Sort them in reverse order based/ on the position and process them,
  181. // so that positions don't change.
  182. krsort($escape, SORT_NUMERIC);
  183. foreach ($escape as $position => $info) {
  184. $text = substr_replace($text, $this->getEscapedString($info['string']), $position, strlen($info['string']));
  185. }
  186. return $text;
  187. }
  188. /**
  189. * Returns the escaped string.
  190. *
  191. * Defaults to use the escapeStart and escapeEnd properties but can be
  192. * overriden if a non-static replacement pattern is used.
  193. *
  194. * @param string $string
  195. * String that should be escaped.
  196. * @return string
  197. */
  198. protected function getEscapedString($string) {
  199. return $this->escapeStart . $string . $this->escapeEnd;
  200. }
  201. /**
  202. * {@inheritdoc}
  203. */
  204. public function unescapeText($text) {
  205. return preg_replace('/' . preg_quote($this->escapeStart, '/') . '(.+)' . preg_quote($this->escapeEnd, '/') . '/U', '$1', $text);
  206. }
  207. }