rules_i18n.rules.inc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. /**
  121. * Overrides RulesDataInputEvaluator::prepare().
  122. */
  123. public function prepare($text, $var_info, $param_info = NULL) {
  124. if (!empty($param_info['translatable'])) {
  125. $this->setting = TRUE;
  126. }
  127. else {
  128. // Else, skip this evaluator.
  129. $this->setting = NULL;
  130. }
  131. }
  132. /**
  133. * Prepare the i18n-context string.
  134. *
  135. * We have to use process() here instead of evaluate() because we need more
  136. * context than evaluate() provides.
  137. */
  138. public function process($value, $info, RulesState $state, RulesPlugin $element, $options = NULL) {
  139. $options = isset($options) ? $options : $this->getEvaluatorOptions($info, $state, $element);
  140. $value = isset($this->processor) ? $this->processor->process($value, $info, $state, $element, $options) : $value;
  141. if (isset($element->root()->name)) {
  142. $config_name = $element->root()->name;
  143. $id = $element->elementId();
  144. $name = $info['#name'];
  145. $options['i18n context'] = "rules:rules_config:$config_name:$id:$name";
  146. return $this->evaluate($value, $options, $state);
  147. }
  148. return $value;
  149. }
  150. /**
  151. * Translate the value.
  152. *
  153. * If the element provides a language parameter, we are using this target
  154. * language provided via $options['language']. Sanitizing is handled by Rules,
  155. * so disable that for i18n.
  156. */
  157. public function evaluate($value, $options, RulesState $state) {
  158. $langcode = isset($options['language']) ? $options['language']->language : NULL;
  159. if (is_array($value)) {
  160. foreach ($value as $key => $text) {
  161. $value[$key] = i18n_string($options['i18n context'] . ':' . $key, $text, array('langcode' => $langcode, 'sanitize' => FALSE));
  162. }
  163. }
  164. else {
  165. $value = i18n_string($options['i18n context'], $value, array('langcode' => $langcode, 'sanitize' => FALSE));
  166. }
  167. return $value;
  168. }
  169. /**
  170. * Overrides RulesDataInputEvaluator::help().
  171. */
  172. public static function help($var_info, $param_info = array()) {
  173. if (!empty($param_info['translatable'])) {
  174. if (!empty($param_info['custom translation language'])) {
  175. $text = t('Translations can be provided at the %translate tab. The argument value is translated to the configured language.', array('%translate' => t('Translate')));
  176. }
  177. else {
  178. $text = t('Translations can be provided at the %translate tab. The argument value is translated to the current interface language.', array('%translate' => t('Translate')));
  179. }
  180. $render = array(
  181. '#theme' => 'rules_settings_help',
  182. '#text' => $text,
  183. '#heading' => t('Translation'),
  184. );
  185. return $render;
  186. }
  187. }
  188. }