Projet

Général

Profil

Paste
Télécharger (6,47 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / rules_i18n / rules_i18n.rules.inc @ 950416da

1
<?php
2

    
3
/**
4
 * @file
5
 * Internationalization rules integration.
6
 */
7

    
8
/**
9
 * Implements hook_rules_action_info().
10
 */
11
function rules_i18n_rules_action_info() {
12
  $items['rules_i18n_t'] = array(
13
    'label' => t('Translate a text'),
14
    'group' => t('Translation'),
15
    'parameter' => array(
16
      'text' => array(
17
        'type' => 'text',
18
        'label' => t('Text'),
19
        'description' => t('The text to translate.'),
20
        'translatable' => TRUE,
21
      ),
22
      'language' => array(
23
        'type' => 'token',
24
        'label' => t('Language'),
25
        'description' => t('The language to translate the text into.'),
26
        'options list' => 'entity_metadata_language_list',
27
        'default mode' => 'select',
28
      ),
29
    ),
30
    'provides' => array(
31
      'text' => array('type' => 'text', 'label' => t('The translated text')),
32
    ),
33
    'base' => 'rules_i18n_action_t',
34
    'access callback' => 'rules_i18n_rules_integration_access',
35
  );
36
  $items['rules_i18n_select'] = array(
37
    'label' => t('Select a translated value'),
38
    'group' => t('Translation'),
39
    'parameter' => array(
40
      'data' => array(
41
        'type' => '*',
42
        'label' => t('Data'),
43
        '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.'),
44
        'translatable' => TRUE,
45
        'restrict' => 'select',
46
      ),
47
      'language' => array(
48
        'type' => 'token',
49
        'label' => t('Language'),
50
        'description' => t('The language to translate the value into.'),
51
        'options list' => 'entity_metadata_language_list',
52
      ),
53
    ),
54
    'provides' => array(
55
      'data_translated' => array('type' => '*', 'label' => t('The translated value')),
56
    ),
57
    'base' => 'rules_i18n_action_select',
58
    'access callback' => 'rules_i18n_rules_integration_access',
59
  );
60
  return $items;
61
}
62

    
63
/**
64
 * Access callback for the rules i18n integration.
65
 */
66
function rules_i18n_rules_integration_access() {
67
  return user_access('translate interface');
68
}
69

    
70
/**
71
 * Action callback: Translate a text.
72
 */
73
function rules_i18n_action_t($text) {
74
  // Nothing to do, as our input evaluator has already translated it.
75
  // @see RulesI18nStringEvaluator
76
  return array('text' => $text);
77
}
78

    
79
/**
80
 * Implements the form_alter callback for the "Translate a text" action to set a default selector.
81
 */
82
function rules_i18n_action_t_form_alter(&$form, &$form_state, $options, $element) {
83
  if (isset($form['parameter']['language']['settings']['language:select']) && empty($element->settings['language:select'])) {
84
    $form['parameter']['language']['settings']['language:select']['#default_value'] = 'site:current-page:language';
85
  }
86
}
87

    
88
/**
89
 * Action callback: Select a translated value.
90
 */
91
function rules_i18n_action_select($data) {
92
  // Nothing to do, as Rules applies the language to the data selector for us.
93
  return array('data_translated' => $data);
94
}
95

    
96
/**
97
 * Action "Select a translated value" info_alter callback.
98
 */
99
function rules_i18n_action_select_info_alter(&$element_info, $element) {
100
  $element->settings += array('data:select' => NULL);
101
  if ($wrapper = $element->applyDataSelector($element->settings['data:select'])) {
102
    $info = $wrapper->info();
103
    // Pass through the data type of the selected data.
104
    $element_info['provides']['data_translated']['type'] = $wrapper->type();
105
  }
106
}
107

    
108
/**
109
 * Implements hook_rules_evaluator_info().
110
 */
111
function rules_i18n_rules_evaluator_info() {
112
  return array(
113
    'i18n' => array(
114
      'class' => 'RulesI18nStringEvaluator',
115
      'type' => array('text', 'list<text>', 'token', 'list<token>'),
116
      // Be sure to translate after doing PHP evaluation.
117
      'weight' => -8,
118
    ),
119
  );
120
}
121

    
122
/**
123
 * A class implementing a rules input evaluator processing tokens.
124
 */
125
class RulesI18nStringEvaluator extends RulesDataInputEvaluator {
126

    
127
  public static function access() {
128
    return user_access('translate admin strings');
129
  }
130

    
131
  /**
132
   * Overrides RulesDataInputEvaluator::prepare().
133
   */
134
  public function prepare($text, $var_info, $param_info = NULL) {
135
    if (!empty($param_info['translatable'])) {
136
      $this->setting = TRUE;
137
    }
138
    else {
139
      // Else, skip this evaluator.
140
      $this->setting = NULL;
141
    }
142
  }
143

    
144
  /**
145
   * Prepare the i18n-context string.
146
   *
147
   * We have to use process() here instead of evaluate() because we need more
148
   * context than evaluate() provides.
149
   */
150
  public function process($value, $info, RulesState $state, RulesPlugin $element, $options = NULL) {
151
    $options = isset($options) ? $options : $this->getEvaluatorOptions($info, $state, $element);
152
    $value = isset($this->processor) ? $this->processor->process($value, $info, $state, $element, $options) : $value;
153
    if (isset($element->root()->name)) {
154
      $config_name = $element->root()->name;
155
      $id = $element->elementId();
156
      $name = $info['#name'];
157
      $options['i18n context'] = "rules:rules_config:$config_name:$id:$name";
158
      return $this->evaluate($value, $options, $state);
159
    }
160
    return $value;
161
  }
162

    
163
  /**
164
   * Translate the value.
165
   *
166
   * If the element provides a language parameter, we are using this target
167
   * language provided via $options['language']. Sanitizing is handled by Rules,
168
   * so disable that for i18n.
169
   */
170
  public function evaluate($value, $options, RulesState $state) {
171
    $langcode = isset($options['language']) ? $options['language']->language : NULL;
172
    if (is_array($value)) {
173
      foreach ($value as $key => $text) {
174
        $value[$key] = i18n_string($options['i18n context'] . ':' . $key, $text, array('langcode' => $langcode, 'sanitize' => FALSE));
175
      }
176
    }
177
    else {
178
      $value = i18n_string($options['i18n context'], $value, array('langcode' => $langcode, 'sanitize' => FALSE));
179
    }
180
    return $value;
181
  }
182

    
183
  /**
184
   * Overrides RulesDataInputEvaluator::help().
185
   */
186
  public static function help($var_info, $param_info = array()) {
187
    if (!empty($param_info['translatable'])) {
188
      if (!empty($param_info['custom translation language'])) {
189
        $text = t('Translations can be provided at the %translate tab. The argument value is translated to the configured language.', array('%translate' => t('Translate')));
190
      }
191
      else {
192
        $text = t('Translations can be provided at the %translate tab. The argument value is translated to the current interface language.', array('%translate' => t('Translate')));
193
      }
194
      $render = array(
195
        '#theme' => 'rules_settings_help',
196
        '#text' => $text,
197
        '#heading' => t('Translation'),
198
      );
199
      return $render;
200
    }
201
  }
202

    
203
}