rules_i18n.rules.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * @file
  4. * Internationalization rules integration.
  5. */
  6. /**
  7. * Implements hook_rules_action_info().
  8. */
  9. function rules_i18n_rules_action_info() {
  10. $items['rules_i18n_t'] = array(
  11. 'label' => t('Translate a text'),
  12. 'group' => t('Translation'),
  13. 'parameter' => array(
  14. 'text' => array(
  15. 'type' => 'text',
  16. 'label' => t('Text'),
  17. 'description' => t('The text to translate.'),
  18. 'translatable' => TRUE,
  19. ),
  20. 'language' => array(
  21. 'type' => 'token',
  22. 'label' => t('Language'),
  23. 'description' => t('The language to translate the text into.'),
  24. 'options list' => 'entity_metadata_language_list',
  25. 'default mode' => 'select',
  26. ),
  27. ),
  28. 'provides' => array(
  29. 'text' => array('type' => 'text', 'label' => t('The translated text')),
  30. ),
  31. 'base' => 'rules_i18n_action_t',
  32. 'access callback' => 'rules_i18n_rules_integration_access',
  33. );
  34. $items['rules_i18n_select'] = array(
  35. 'label' => t('Select a translated value'),
  36. 'group' => t('Translation'),
  37. 'parameter' => array(
  38. 'data' => array(
  39. 'type' => '*',
  40. 'label' => t('Data'),
  41. 'description' => t('Select a translated value, e.g. a translatable field. If the selected data is not translatable, the language neutral value will be selected.'),
  42. 'translatable' => TRUE,
  43. 'restrict' => 'select',
  44. ),
  45. 'language' => array(
  46. 'type' => 'token',
  47. 'label' => t('Language'),
  48. 'description' => t('The language to translate the value into.'),
  49. 'options list' => 'entity_metadata_language_list',
  50. ),
  51. ),
  52. 'provides' => array(
  53. 'data_translated' => array('type' => '*', 'label' => t('The translated value')),
  54. ),
  55. 'base' => 'rules_i18n_action_select',
  56. 'access callback' => 'rules_i18n_rules_integration_access',
  57. );
  58. return $items;
  59. }
  60. /**
  61. * Access callback for the rules i18n integration.
  62. */
  63. function rules_i18n_rules_integration_access() {
  64. return user_access('translate interface');
  65. }
  66. /**
  67. * Action callback: Translate a text.
  68. */
  69. function rules_i18n_action_t($text) {
  70. // Nothing to do, as our input evaluator has already translated it.
  71. // @see RulesI18nStringEvaluator
  72. return array('text' => $text);
  73. }
  74. /**
  75. * Implements the form_alter callback for the "Translate a text" action to set a default selector.
  76. */
  77. function rules_i18n_action_t_form_alter(&$form, &$form_state, $options, $element) {
  78. if (isset($form['parameter']['language']['settings']['language:select']) && empty($element->settings['language:select'])) {
  79. $form['parameter']['language']['settings']['language:select']['#default_value'] = 'site:current-page:language';
  80. }
  81. }
  82. /**
  83. * Action callback: Select a translated value.
  84. */
  85. function rules_i18n_action_select($data) {
  86. // Nothing to do, as Rules applies the language to the data selector for us.
  87. return array('data_translated' => $data);
  88. }
  89. /**
  90. * Action "Select a translated value" info_alter callback.
  91. */
  92. function rules_i18n_action_select_info_alter(&$element_info, $element) {
  93. $element->settings += array('data:select' => NULL);
  94. if ($wrapper = $element->applyDataSelector($element->settings['data:select'])) {
  95. $info = $wrapper->info();
  96. // Pass through the data type of the selected data.
  97. $element_info['provides']['data_translated']['type'] = $wrapper->type();
  98. }
  99. }
  100. /**
  101. * Implements hook_rules_evaluator_info().
  102. */
  103. function rules_i18n_rules_evaluator_info() {
  104. return array(
  105. 'i18n' => array(
  106. 'class' => 'RulesI18nStringEvaluator',
  107. 'type' => array('text', 'list<text>', 'token', 'list<token>'),
  108. // Be sure to translate after doing PHP evaluation.
  109. 'weight' => -8,
  110. ),
  111. );
  112. }
  113. /**
  114. * A class implementing a rules input evaluator processing tokens.
  115. */
  116. class RulesI18nStringEvaluator extends RulesDataInputEvaluator {
  117. public static function access() {
  118. return user_access('translate admin strings');
  119. }
  120. public function prepare($text, $var_info, $param_info = NULL) {
  121. if (!empty($param_info['translatable'])) {
  122. $this->setting = TRUE;
  123. }
  124. else {
  125. // Else, skip this evaluator.
  126. $this->setting = NULL;
  127. }
  128. }
  129. /**
  130. * Prepare the i18n-context string.
  131. *
  132. * We have to use process() here instead of evaluate() because we need more
  133. * context than evaluate() provides.
  134. */
  135. public function process($value, $info, RulesState $state, RulesPlugin $element, $options = NULL) {
  136. $options = isset($options) ? $options : $this->getEvaluatorOptions($info, $state, $element);
  137. $value = isset($this->processor) ? $this->processor->process($value, $info, $state, $element, $options) : $value;
  138. if (isset($element->root()->name)) {
  139. $config_name = $element->root()->name;
  140. $id = $element->elementId();
  141. $name = $info['#name'];
  142. $options['i18n context'] = "rules:rules_config:$config_name:$id:$name";
  143. return $this->evaluate($value, $options, $state);
  144. }
  145. return $value;
  146. }
  147. /**
  148. * Translate the value.
  149. *
  150. * If the element provides a language parameter, we are using this target
  151. * language provided via $options['language']. Sanitizing is handled by Rules,
  152. * so disable that for i18n.
  153. */
  154. public function evaluate($value, $options, RulesState $state) {
  155. $langcode = isset($options['language']) ? $options['language']->language : NULL;
  156. if (is_array($value)) {
  157. foreach ($value as $key => $text) {
  158. $value[$key] = i18n_string($options['i18n context'] . ':' . $key, $text, array('langcode' => $langcode, 'sanitize' => FALSE));
  159. }
  160. }
  161. else {
  162. $value = i18n_string($options['i18n context'], $value, array('langcode' => $langcode, 'sanitize' => FALSE));
  163. }
  164. return $value;
  165. }
  166. public static function help($var_info, $param_info = array()) {
  167. if (!empty($param_info['translatable'])) {
  168. if ($param_info['custom translation language']) {
  169. $text = t('Translations can be provided at the %translate tab. The argument value is translated to the configured language.', array('%translate' => t('Translate')));
  170. }
  171. else {
  172. $text = t('Translations can be provided at the %translate tab. The argument value is translated to the current interface language.', array('%translate' => t('Translate')));
  173. }
  174. $render = array(
  175. '#theme' => 'rules_settings_help',
  176. '#text' => $text,
  177. '#heading' => t('Translation'),
  178. );
  179. return $render;
  180. }
  181. }
  182. }