Identify language from the user preferences.
$languages: An array of valid language objects.
A valid language code on success, FALSE otherwise.
function language_from_user($languages) {
// User preference (only for authenticated users).
global $user;
if ($user->uid && !empty($user->preferred_langcode) && isset($languages[$user->preferred_langcode])) {
return $user->preferred_langcode;
}
// No language preference from the user.
return FALSE;
}