When using the Entity translations relationship in a view (without a language filter) the content outputted is always in the users default language. Let's say I have a view with three columns: Title, Language, and Body. I have a node with translation enabled on the body field that has been translated three times (English, French and Spanish).

When viewing the results, I see three rows (one for each translation). The issue is that the body field always displays content in the users default language, NOT the language of the Entity it fetched. So my results look like this:
Node Title | English | English Body
Node Title | French | English Body
Node Title | Spanish | English Body

Desired:
Node Title | English | English Body
Node Title | French | French Body
Node Title | Spanish | Spanish Body

Does anyone have a solution for this?

Comments

jdanthinne’s picture

Issue summary: View changes

Anyone with a solution?
I have the same problem, trying to export multiple versions of the same content in XML, and fields need to be translated so I can have one row per language.
I think this is a major++ problem, and don't want to go back to the old content translation.

BDuell’s picture

We wound up using hook_views_pre_render for a proper rendering of the translated field.
The main thing to look at is the switch statement - by "default" almost every field maps up the same, but we had two fields that had a different mapping (field_field_market and field_field_workflow_state). If the function cannot map the field to it's proper translation it will show the values in watchdog (or you can use dpm to show you what needs to be mapped as well):

/**
* Implementation of hook_views_pre_render
* @param type $view
*
* This function examines a view's results, and for any rows that has a '_language' field type (single or combined,
* but they MUST be the same language for the replacement to occur) the row value will be replaced by it's views calculated value
*
* If a view runs that a field *should* perhaps be processed but is not, the logfile will show a 'Views translation needed for'
* message
*
* Any new calculatable values for view fields that need to be translated can be added to the switch below...
*/
function MODULENAME_views_pre_render(&$view) {
  foreach ($view->result as $key => $value) {
    $entity_type = current(array_keys(get_object_vars($value)));
    if (isset($value->_field_data[$entity_type]['entity']->language)) {
      // This result row has a language... Let's try to choose this result row's true language...
      $rowlangcodes = array();
      foreach ($value as $key2 => $value2) {
        if (strpos($key2, '_language') !== FALSE) {
          // Save the value (or one of the values) for a _language var that is carrying a language...
          $rowlangcodes[] = $value2;
        }
      }
      $rowlangcode = '';
      if (count(array_count_values($rowlangcodes))==1) {
        // Any and all _language vars equal the same, so it's a true language...
        $rowlangcode = $rowlangcodes[0];
      }
      if ($rowlangcode!='') {
        // This rows fields need to be translated...
        foreach ($value->_field_data[$entity_type]['entity'] as $key2 => $value2) {
          $fieldname = 'field_'.$key2;
          if (isset($value->_field_data[$entity_type]['entity']->{$key2}) && is_array($value->_field_data[$entity_type]['entity']->{$key2}) && isset($value->_field_data[$entity_type]['entity']->{$key2}[$rowlangcode]) && isset($value) && is_object($value) && isset($value->{$fieldname}) && is_array($value->{$fieldname})) {
            switch ($fieldname) {
              case 'field_field_market':
                $value->{$fieldname}[0]['rendered']['#markup'] = db_query("SELECT title FROM {node} WHERE nid = :nid", array(':nid' => $value->_field_data[$entity_type]['entity']->{$key2}[$rowlangcode][0]['target_id']))->fetchField();
                break;
              case 'field_field_workflow_state':
                $workflow_id = $value->_field_data[$entity_type]['entity']->{$key2}[$rowlangcode][0]['value'];
                $value->{$fieldname}[0]['rendered']['#markup'] = workflow_get_sid_label($workflow_id);
                $value->{$fieldname}[0]['raw']['#value'] = $workflow_id;
                break;
              default:
                if (isset($value->_field_data[$entity_type]['entity']->{$key2}[$rowlangcode][0]['safe_value'])) {
                  $translation = $value->_field_data[$entity_type]['entity']->{$key2}[$rowlangcode][0]['safe_value'];;
                  $value->{$fieldname}[0]['rendered']['#markup'] = $translation;
                  $value->{$fieldname}[0]['raw']['value'] = $translation;
                  $value->{$fieldname}[0]['raw']['safe_value'] = $translation;
                } else {
                  // Can also use dpm here....
                  watchdog('Views field translation needed...', 'Language: '.var_export($rowlangcode, TRUE).'<br>Fieldname: '.var_export($fieldname, TRUE).'<br>'.var_export($value->{$fieldname}, TRUE).'<br>Needs mapping to<br>'.var_export($value->_field_data[$entity_type]['entity']->{$key2}[$rowlangcode], TRUE));
                  $value->{$fieldname}[0]['rendered']['#markup'] = '<font color="red">'.t('Needs Translation Mapping').'</font>';
                }
            }
          }
        }
      }
    }
  }
}

This allowed our views to show the proper translation for fields.