How can I render a view in different languages during queue process, using PHP? I am putting views into emails using a Drupal 7 queue of user ids.

In the queue, I load the view and attempt to render it after switching $GLOBALS['language'] to the user's language, and then back.

However, only the first view is rendered with the correct language.

The rest are all English.

_switch_language($language);
$view = views_get_view($view_name, TRUE);

if (!is_object($view)) {
  _revert_language();
  return FALSE;
}

if (!empty($display_id)) {
  $view-&gt;set_display($display_id);
}

_impersonate_user($account);

$view-&gt;pre_execute();
$view-&gt;hide_admin_links = TRUE;
$output = $view-&gt;display_handler-&gt;execute();
$view-&gt;post_execute();

_revert_user();
_revert_language();


Language switching function:

function _switch_language($new_language = NULL) {
  global $language;
  $language_original = &amp;drupal_static(__FUNCTION__);

  if (!isset($new_language)) {
    if (isset($language_original) &amp;&amp; (!empty($language_original))) {
      $language = array_pop($language_original);
    }
  }
  else {
    $language_original[] = $language;
    if (is_string($new_language)) {
      $languages = language_list();
      if (isset($languages[$new_language])) {
        $language = $languages[$new_language];
      }
    }
  }

  return $language;
}


The user switching function is similar, but calls drupal_save_session() twice to disable and re-enable session saving.

Any ideas? Thanks. :-)
I do not yet understand why, but $view-&gt;preview($display_id, $args) allows the language-switching to work.

It is very similar to my view execution code above, but instead of calling execute() on $view-&gt;display_handler, I call preview() and that works as desired.

$view-&gt;pre_execute();
$view-&gt;hide_admin_links = TRUE;
$view-&gt;preview = TRUE;
$output = $view-&gt;display_handler-&gt;preview();
$view-&gt;post_execute();


I generate the view for each $user-&gt;language and insert that into the email.

Perhaps this will help someone else.