first import

This commit is contained in:
Bachir Soussi Chiadmi
2015-04-08 11:40:19 +02:00
commit 1bc61b12ad
8435 changed files with 1582817 additions and 0 deletions

View File

@@ -0,0 +1,94 @@
<?php
/**
* @file
* Internationalization integration based upon the entity API i18n stuff.
*/
/**
* Rules i18n integration controller.
*/
class RulesI18nStringController extends EntityDefaultI18nStringController {
/**
* Overriden to customize i18n object info.
*
* @see EntityDefaultI18nStringController::hook_object_info()
*/
public function hook_object_info() {
$info = parent::hook_object_info();
$info['rules_config']['class'] = 'RulesI18nStringObjectWrapper';
return $info;
}
/**
* Overriden to customize the used menu wildcard.
*/
protected function menuWildcard() {
return '%rules_config';
}
/**
* Provide the menu base path. We can provide only one though.
*/
protected function menuBasePath() {
return 'admin/config/workflow/rules/reaction';
}
}
/**
* Custom I18n String object wrapper, which register custom properties per config.
*/
class RulesI18nStringObjectWrapper extends i18n_string_object_wrapper {
/**
* Get translatable properties
*/
protected function build_properties() {
$strings = parent::build_properties();
$properties = array();
// Also add in the configuration label, as the i18n String UI requires
// a String to be available always.
$properties['label'] = array(
'title' => t('Configuration name'),
'string' => $this->object->label,
);
$this->buildElementProperties($this->object, $properties);
// Add in translations for all elements.
foreach ($this->object->elements() as $element) {
$this->buildElementProperties($element, $properties);
}
$strings[$this->get_textgroup()]['rules_config'][$this->object->name] = $properties;
return $strings;
}
/**
* Adds in translatable properties of the given element.
*/
protected function buildElementProperties($element, &$properties) {
foreach ($element->pluginParameterInfo() as $name => $info) {
// Add in all directly provided input variables.
if (!empty($info['translatable']) && isset($element->settings[$name])) {
// If its an array of textual values, translate each value on its own.
if (is_array($element->settings[$name])) {
foreach ($element->settings[$name] as $i => $value) {
$properties[$element->elementId() . ':' . $name . ':' . $i] = array(
'title' => t('@plugin "@label" (id @id), @parameter, Value @delta', array('@plugin' => drupal_ucfirst($element->plugin()), '@label' => $element->label(), '@id' => $element->elementId(), '@parameter' => $info['label'], '@delta' => $i + 1)),
'string' => $value,
);
}
}
else {
$properties[$element->elementId() . ':' . $name] = array(
'title' => t('@plugin "@label" (id @id), @parameter', array('@plugin' => drupal_ucfirst($element->plugin()), '@label' => $element->label(), '@id' => $element->elementId(), '@parameter' => $info['label'])),
'string' => $element->settings[$name],
);
}
}
}
}
}

View File

@@ -0,0 +1,15 @@
name = Rules translation
description = Allows translating rules.
dependencies[] = rules
dependencies[] = i18n_string
package = Multilingual - Internationalization
core = 7.x
files[] = rules_i18n.i18n.inc
files[] = rules_i18n.rules.inc
files[] = rules_i18n.test
; Information added by drupal.org packaging script on 2012-10-23
version = "7.x-2.2+5-dev"
core = "7.x"
project = "rules"
datestamp = "1350998486"

View File

@@ -0,0 +1,132 @@
<?php
/**
* @file
* Rules i18n integration.
*/
/**
* Implements hook_menu().
*/
function rules_i18n_rules_ui_menu_alter(&$items, $base_path, $base_count) {
$items[$base_path . '/manage/%rules_config/edit'] = array(
'title' => 'Edit',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -100,
);
// For reaction-rules i18n generates the menu items, for the others we provide
// further i18n menu items for all other base paths.
if ($base_path != 'admin/config/workflow/rules/reaction') {
$items[$base_path . '/manage/%rules_config/translate'] = array(
'title' => 'Translate',
'page callback' => 'i18n_page_translate_localize',
'page arguments' => array('rules_config', $base_count + 1),
'access callback' => 'i18n_object_translate_access',
'access arguments' => array('rules_config', $base_count + 1),
'type' => MENU_LOCAL_TASK,
'file' => 'i18n.pages.inc',
'file path' => drupal_get_path('module', 'i18n'),
'weight' => 10,
);
$items[$base_path . '/manage/%rules_config/translate/%i18n_language'] = array(
'title' => 'Translate',
'page callback' => 'i18n_page_translate_localize',
'page arguments' => array('rules_config', $base_count + 1, $base_count + 3),
'access callback' => 'i18n_object_translate_access',
'access arguments' => array('rules_config', $base_count),
'type' => MENU_CALLBACK,
'file' => 'i18n.pages.inc',
'file path' => drupal_get_path('module', 'i18n'),
'weight' => 10,
);
}
}
/**
* Implements hook_entity_info_alter().
*/
function rules_i18n_entity_info_alter(&$info) {
// Enable i18n support via the entity API.
$info['rules_config']['i18n controller class'] = 'RulesI18nStringController';
}
/**
* Implements hook_rules_config_insert().
*/
function rules_i18n_rules_config_insert($rules_config) {
// Do nothing when rebuilding defaults to avoid multiple cache rebuilds.
// @see rules_i18n_rules_config_defaults_rebuild()
if (!empty($rules_config->is_rebuild)) {
return;
}
i18n_string_object_update('rules_config', $rules_config);
}
/**
* Implements hook_rules_config_update().
*/
function rules_i18n_rules_config_update($rules_config, $original = NULL) {
// Do nothing when rebuilding defaults to avoid multiple cache rebuilds.
// @see rules_i18n_rules_config_defaults_rebuild()
if (!empty($rules_config->is_rebuild)) {
return;
}
$original = $original ? $original : $rules_config->original;
// Account for name changes.
if ($original->name != $rules_config->name) {
i18n_string_update_context("rules:rules_config:{$original->name}:*", "rules:rules_config:{$rules_config->name}:*");
}
// We need to remove the strings of any disappeared properties, i.e. strings
// from translatable parameters of deleted actions.
// i18n_object() uses a static cache per config, so bypass it to wrap the
// original entity.
$object_key = i18n_object_key('rules_config', $original);
$old_i18n_object = new RulesI18nStringObjectWrapper('rules_config', $object_key, $original);
$old_strings = $old_i18n_object->get_strings(array('empty' => TRUE));
// Note: For the strings to have updated values, the updated entity needs to
// be handled last due to i18n's cache.
$strings = i18n_object('rules_config', $rules_config)->get_strings(array('empty' => TRUE));
foreach (array_diff_key($old_strings, $strings) as $name => $string) {
$string->remove(array('empty' => TRUE));
}
// Now update the remaining strings.
foreach ($strings as $string) {
$string->update(array('empty' => TRUE, 'update' => TRUE));
}
}
/**
* Implements hook_rules_config_delete().
*/
function rules_i18n_rules_config_delete($rules_config) {
i18n_string_object_remove('rules_config', $rules_config);
}
/**
* Implements hook_rules_config_defaults_rebuild().
*/
function rules_i18n_rules_config_defaults_rebuild($rules_configs, $originals) {
// Once all defaults have been rebuilt, update all i18n strings at once. That
// way we build the rules cache once the rebuild is complete and avoid
// rebuilding caches for each updated rule.
foreach ($rules_configs as $name => $rule_config) {
if (empty($originals[$name])) {
rules_i18n_rules_config_insert($rule_config);
}
else {
rules_i18n_rules_config_update($rule_config, $originals[$name]);
}
}
}

View File

@@ -0,0 +1,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;
}
}
}

View File

@@ -0,0 +1,183 @@
<?php
/**
* @file
* Rules i18n tests.
*/
/**
* Test the i18n integration.
*/
class RulesI18nTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Rules I18n',
'description' => 'Tests translating Rules configs.',
'group' => 'Rules',
'dependencies' => array('i18n_string'),
);
}
public function setUp() {
parent::setUp('rules_i18n');
$this->admin_user = $this->drupalCreateUser(array('bypass node access', 'administer nodes', 'administer languages', 'administer content types', 'administer blocks', 'access administration pages'));
$this->drupalLogin($this->admin_user);
$this->addLanguage('de');
}
/**
* Copied from i18n module (class Drupali18nTestCase).
*
* We cannot extend from Drupali18nTestCase as else the test-bot would die.
*/
public function addLanguage($language_code) {
// Check to make sure that language has not already been installed.
$this->drupalGet('admin/config/regional/language');
if (strpos($this->drupalGetContent(), 'enabled[' . $language_code . ']') === FALSE) {
// Doesn't have language installed so add it.
$edit = array();
$edit['langcode'] = $language_code;
$this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
// Make sure we are not using a stale list.
drupal_static_reset('language_list');
$languages = language_list('language');
$this->assertTrue(array_key_exists($language_code, $languages), t('Language was installed successfully.'));
if (array_key_exists($language_code, $languages)) {
$this->assertRaw(t('The language %language has been created and can now be used. More information is available on the <a href="@locale-help">help screen</a>.', array('%language' => $languages[$language_code]->name, '@locale-help' => url('admin/help/locale'))), t('Language has been created.'));
}
}
elseif ($this->xpath('//input[@type="checkbox" and @name=:name and @checked="checked"]', array(':name' => 'enabled[' . $language_code . ']'))) {
// It's installed and enabled. No need to do anything.
$this->assertTrue(true, 'Language [' . $language_code . '] already installed and enabled.');
}
else {
// It's installed but not enabled. Enable it.
$this->assertTrue(true, 'Language [' . $language_code . '] already installed.');
$this->drupalPost(NULL, array('enabled[' . $language_code . ']' => TRUE), t('Save configuration'));
$this->assertRaw(t('Configuration saved.'), t('Language successfully enabled.'));
}
}
/**
* Tests translating rules configs.
*/
public function testRulesConfigTranslation() {
// Create a rule and translate it.
$rule = rule();
$rule->label = 'label-en';
$rule->action('drupal_message', array('message' => 'English message for [site:current-user].'));
$rule->save();
$actions = $rule->actions();
$id = $actions[0]->elementId();
// Add a translation.
i18n_string_textgroup('rules')->update_translation("rules_config:{$rule->name}:label", 'de', 'label-de');
i18n_string_textgroup('rules')->update_translation("rules_config:{$rule->name}:$id:message", 'de', 'German message für [site:current-user].');
// Execute the Rule and make sure the translated message has been output.
// To do so, set the global language to German.
$languages = language_list();
$GLOBALS['language'] = $languages['de'];
// Clear messages and execute the rule.
i18n_string_textgroup('rules')->cache_reset();
drupal_get_messages();
$rule->execute();
$messages = drupal_get_messages();
$this->assertEqual($messages['status'][0], 'German message für ' . $GLOBALS['user']->name . '.', 'Translated message has been output.');
// Test re-naming the rule.
$rule->name = 'rules_i18n_name_2';
$rule->save();
$translation = entity_i18n_string("rules:rules_config:{$rule->name}:label", $rule->label, 'de');
$this->assertEqual($translation, 'label-de', 'Translation survives a name change.');
// Test updating and make sure the translation stays.
$rule->label = 'Label new';
$rule->save();
$translation = entity_i18n_string("rules:rules_config:{$rule->name}:label", $rule->label, 'de');
$this->assertEqual($translation, 'label-de', 'Translation survives an update.');
// Test deleting the action and make sure the string is deleted too.
$actions[0]->delete();
$rule->save();
$translation = entity_i18n_string("rules_config:{$rule->name}:$id:message", 'English message for [site:current-user].', 'de');
$this->assertEqual($translation, 'English message for [site:current-user].', 'Translation of deleted action has been deleted.');
// Now delete the whole config and make sure all translations are deleted.
$rule->delete();
$translation = entity_i18n_string("rules_config:{$rule->name}:label", 'label-en', 'de');
$this->assertEqual($translation, 'label-en', 'Translation of deleted config has been deleted.');
}
/**
* Tests the "Translate a text" action.
*/
public function testI18nActionT() {
$set = rules_action_set(array());
$set->action('rules_i18n_t', array(
'text' => 'untranslated',
'language' => 'de',
));
$set->action('drupal_message', array('message:select' => 'text'));
$set->save('rules_i18n_test');
// Add a translation.
$actions = $set->getIterator();
$id = $actions[0]->elementId();
i18n_string_textgroup('rules')->update_translation("rules_config:{$set->name}:$id:text", 'de', 'text-de');
// Clear messages and execute it.
drupal_get_messages();
$set->execute();
$messages = drupal_get_messages();
$this->assertEqual($messages['status'][0], 'text-de', 'Text has been successfully translated.');
// Enable the PHP module and make sure PHP in translations is not evaluted.
module_enable(array('php'));
i18n_string_textgroup('rules')->update_translation("rules_config:{$set->name}:$id:text", 'de', 'text <?php echo "eval";?>');
// Clear messages and execute it.
drupal_get_messages();
$set->execute();
$messages = drupal_get_messages();
$this->assertEqual($messages['status'][0], check_plain('text <?php echo "eval";?>'), 'PHP in translated text is not executed.');
}
/**
* Tests the "Select a translated value" action.
*/
public function testI18nActionSelect() {
// Make the body field and the node type 'page' translatable.
$field = field_info_field('body');
$field['translatable'] = TRUE;
field_update_field($field);
variable_set('language_content_type_page', 1);
$set = rules_action_set(array('node' => array('type' => 'node')));
$set->action('rules_i18n_select', array(
'data:select' => 'node:body:value',
'language' => 'de',
'data_translated:var' => 'body',
));
$set->action('drupal_message', array('message:select' => 'body'));
$set->save();
$body['en'][0] = array('value' => 'English body.');
$body['de'][0] = array('value' => 'German body.');
$node = $this->drupalCreateNode(array('language' => 'en', 'body' => $body));
// Clear messages and execute it.
drupal_get_messages();
$set->execute($node);
$messages = drupal_get_messages();
$this->assertEqual($messages['status'][0], "German body.\n", 'Translated text has been selected.');
}
}