6

I have a multilingual website (English, Spanish).

Is there any way to create a rule condition to compare the language used by the visitor against a fixed value?

The condition would something like: if language is English then go ahead with the actions.

13
+50

Seemed like a useful thing to have.

I created a patch for Rules, adding this functionality. I posted it in the Rules issue queue. That seemed better than providing the code directly here, as this way you can still use it anyway that suits you, yet it may actually get into core Rules.

Also see this question, "How do I add a new condition in Rules 2?".

| improve this answer | |
  • I was about to do a quick write up for this functionality, yours was pretty much what i was aiming for. I'd do a +2 for adding it as a patch for Rules if I could. – googletorp Mar 7 '12 at 21:38
  • Awesome that you added this as a patch to Rules. – Jay Mar 12 '12 at 12:58
1

One of the rules maintainers, fago, has a patch under development (started recently and marked as "needs work") that adds a variety of internationalization features to rules, including functions for checking the language of users as well as setting languages for rules themselves.

| improve this answer | |
1

You can implement a new condition in a module, using the following implementation of hook_rules_condition_info().

function mymodulename_rules_condition_info() {
  return array(
    'check_current_user_language' => array(
      'label' => t('Language for current user'),
      'parameter' => array(
        'string' => array(
          'type' => 'text',
          'label' => t('Language to check'),
        ),
      ),
    ),
  ); 
}

function mymodule_check_current_user_language($string) {
  global $user, $language_content;

  return ($user->language == $string);
}
| improve this answer | |
  • 1
    And what do you do when the german language is added? – googletorp Mar 7 '12 at 22:52
0

You can find this function in the I18n bundle for translating. In rules use date comparison

| improve this answer | |
0

Actually there are two ways for that:

1- The easy one :

Add new condition select Data comparison and in the parameter:

Parameter: Data to compare: [site:current-page:language], Data value: English

enter image description here


2- The Hard one:

/*
* Implements hook_rules_condition_info().
*/

function MODULE_NAME_rules_condition_info() {
 $conditions = array();

 $conditions['MODULE_NAME_check_current_user_language'] = array(
   'label' => t('Current user language'),
   'group' => t('YOUR GROUP NAME'),
   'parameter' => array(
     'language' => array(
       'type' => 'token',
       'label' => t('Language'),
       'description' => t('Select language that will be compared against the
user current language.'),
       'options list' => 'entity_metadata_language_list',
       'default value' => LANGUAGE_NONE,
     ),
   ),
 );

 return $conditions;
}

Then:

/*
*  Compare site current language with required language
*/

function MODULE_NAME_check_current_user_language($required_language) {
 global $language;

 return $language->language == $required_language;
}

Don't forget to clear the cache.

enter image description here

| improve this answer | |
-1

You can try to accomplish this adding additional field to profile and using 2 different rules.

1st will set the value of the field, using this functions

2nd will compare the profile field value using account:profile_lang data selector

| improve this answer | |
  • 1
    I assume this requires use of eval()? – Letharion Mar 7 '12 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.