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