The more elegant and Drupal way is to modify the $form_state['redirect']
variable.
From Drupal core code comments (in includes/form.inc):
$form_state['redirect']
determines where to redirect the user. This
can be set either to a string (the path to redirect to), or an array
of arguments for drupal_goto()
.
The key part of the solution is to pass a language argument for drupal_goto()
. First add an additional form submit handler for the login form, and modify the redirect variable in this handler. Example:
/**
* Implements hook_form_alter().
*/
function mymodule_form_user_login_alter(&$form, $form_state, $form_id) {
$form['#submit'][] = 'mymodule_user_login_form_submit';
}
/**
* Switches language according to user profile.
*/
function mymodule_user_login_form_submit($form, &$form_state) {
global $user;
global $language;
// Exit quickly if UI is already in correct language.
if (!(isset($user->language) && isset($language->language) &&
$user->language != $language->language)) {
return;
}
// Verify user language exists and is enabled.
$languages = language_list();
if (!isset($languages[$user->language]) ||
!$languages[$user->language]->enabled) {
return;
}
// Change destination to translated equivalent, if applicable.
if (isset($_GET['destination'])) {
$translations = translation_path_get_translations($_GET['destination']);
if (array_key_exists($user->language, $translations)) {
$_GET['destination'] = $translations[$user->language];
}
}
if (isset($form_state['redirect'])) {
if (is_array($form_state['redirect'])) {
$form_state['redirect'][1]['language'] = $languages[$user->language];
} else {
$form_state['redirect'] = [$form_state['redirect'], [
'language' => $languages[$user->language],
]];
}
} else {
$form_state['redirect'] = [sprintf('user/%d', $user->uid), [
'language' => $languages[$user->language],
]];
}
}
For further reading, there is a fair amount of information about this topic on this community documentation page from Drupal: How to redirect users after login in drupal?
admin/config/regional/language/configure
?