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