0

Is there a way when a user login to automatically switch him in then language setup in his user profile.

1 Answer 1

2

In "Configuration -> Regional and language -> Languages -> Detection and Selection" enable detection method "User -> Follow the user's language preference." and drag it to the top of the list. Do same in both sections: User interface & Content language detection.

UPDATE: As an alternative to GUI method you can add the code pasted below in your theme's template.php file (but first, disable detection of user language in Language settings).

The code below uses hook_user_login which runs when the user successfully logs in and passes the user object as an argument. User language is then used for a redirect (drupal_goto).

function yourthemename_user_login(&$edit, $account)
    {
      // get current user's language from the user account
      $user_language = $account->language;

      // if account has no preferred language set - do nothing
      if (!$user_language) {
          return;
      }
      // if the language is set - do additional checks and then redirect
      else {
        // get a list of installed languages
        $languages = language_list();
        // check if language retrieved from user account exists 
        if (!isset($languages[$user_language])) {
            return;
        }
        else {
          // verify if language is enabled
          if (!$languages[$user_language]->enabled) {
              return;
          }
          else {
            // if enabled - redirect to user profile in preferred language
            $user_profile_url = drupal_get_path_alias('user/' . $account->uid);
            drupal_goto($user_profile_url, array(
                'language' => $languages[$user_language],
            ));
          }
        } 
      }
    }
3
  • I tried that and it's not working at 100%. When the user login, yes it's switch the language to his language setup in his profile. The problem is if the user use the Language Switcher to see the website in an other language, he cannot since now the system use the language setup in his profile. I only want the user be redirecting in his language setup in his profile while he is in the login process. After login, the user can use the language switcher widget to see the website in an other language.Thanks hope there is a way to do that. Jan 14, 2016 at 13:45
  • @Jean-Francois check the updated answer. Hope it helps you reach your goals.
    – oksana-c
    Feb 9, 2016 at 1:12
  • @Jean-Francois were you able to setup your redirect?
    – oksana-c
    Feb 14, 2016 at 2:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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