It may be obvious but I cannot find any example... I have a destination text field (attached to a node) that is translatable with Entity Translation. My source data is coming from CSV, each language having its column. I'm trying to import each translation in the right place.

Sample CSV row:

"id","name_en","name_fr"
1234,"Peter","Pierre" 

I want to insert this in a Field API field as follows:

$node->field_name['en'][0]['value'] = 'Peter';
$node->field_name['fr'][0]['value'] = 'Pierre';

I tried the next mapping:

$this->addFieldMapping('field_name', 'name_en')->arguments(array('language' => 'en'));
$this->addFieldMapping('field_name', 'name_fr')->arguments(array('language' => 'fr'));

It's not working... What is wrong here?

Comments

mikeryan’s picture

Status: Active » Postponed (maintainer needs more info)

First off, you can't have multiple mappings to the same destination field - the second one will override the first (you should be seeing warnings about it).

Secondly, using the arguments() method you can only set one value per field.

Fortunately, just recently #1279778: Improve argument passing to fields was committed, providing a subfield syntax for mappings that is much cleaner and more flexible. If you have a -dev release since April 5, you should be able to do:

$this->addFieldMapping('field_name', 'name');
$this->addFieldMapping('field_name:language', 'language');
...
public function prepareRow($row) {
  $row->name = array($row->name_en, $row->name_fr);
  $row->language = array('en', 'fr');
}

I haven't tested the subfield stuff in a multi-language scenario, so please let me know if this approach works for you.

claudiu.cristea’s picture

I have to show the real case (the above one was simplified for readability). Tried your approach and I got error:

Illegal offset type File sites/all/modules/migrate/plugins/destinations/fields.inc, line 266

My code (a little different from above...):

$this->addFieldMapping('title_field', 'title');
$this->addFieldMapping('title_field:language', 'language');
...
public function prepareRow($row) {
  $row->title = array($row->title_nl, $row->title_en, $row->title_fr);
  $row->language = array('nl', 'en', 'fr');
}

Inspecting the offset in sites/all/modules/migrate/plugins/destinations/fields.inc, line 266:

      $return[$language][$delta] = $item;

I found that $language is an array rather than a string with value:

Array
(
    [0] => nl
    [1] => en
    [2] => fr
)

Is this helpful?

claudiu.cristea’s picture

Status: Postponed (maintainer needs more info) » Active
mikeryan’s picture

Title: Multilanguage mapping » Handle language subfield on a value-by-value basis
Component: Documentation » Code
Assigned: Unassigned » mikeryan
Category: support » bug

OK, I should've looked at the code first - since language was previously treated like a field option (one language per field), the subfield changes continued to treat it that way, so it isn't being assigned per value. That's a bug for me to fix...

Thanks.

mikeryan’s picture

Status: Active » Needs review
StatusFileSize
new1.66 KB

Can you try the attached patch and see if it gives you the multi-language support you need?

Thanks.

jgreidy’s picture

A workaround for multilingual entity translation migrate, probably not the best thing, but it works.

Do the migrate as normal in the default language ('en' for me), don't map the other language fields (eg. title_deu),

$this->addFieldMapping('title_field', 'title_eng');
$this->addFieldMapping('title', 'title_eng');
$this->addFieldMapping('body', 'description_eng');

then use the prepare() method to set up the other language version:

public function prepare($node, stdClass $row) {
  	// clone the english and replace german text only
  	$node->title_field['de'] = $node->title_field['en'];	// from entity translate & title modules
  	$node->title_field['de'][0]['value'] = $row->title_deu;
  	
  	$node->body['de'] = $node->body['en'];
  	$node->body['de'][0]['value'] = $row->description_deu;
    }
mikeryan’s picture

Status: Needs review » Fixed

Committed the patch in #5.

claudiu.cristea’s picture

Status: Fixed » Needs work
StatusFileSize
new59.47 KB
new48.97 KB
new26.95 KB

I patched with #5 and defined mappings as follows:

$this->addFieldMapping('title_field', 'title');
$this->addFieldMapping('title_field:language', 'language');
...
public function prepareRow($row) {
  $row->title = array($row->title_nl, $row->title_en, $row->title_fr);
  $row->language = array('nl', 'en', 'fr');
}

Here's what I got...

  1. No import errors.
  2. Using Devel node load UI (node/%node/devel), translations are OK. See http://drupal.org/files/devel_0.jpg.
  3. Translations are stored in the field data table field_data_title_field but with incremental deltas. Delta should be "0" for all translations. See http://drupal.org/files/db.jpg.
  4. The resulting node seems untranslated in the Entity Translation UI. See http://drupal.org/files/trans_ui.jpg. It might be due to "deltas issue"?

Overall is working. Not sure if the UI issue is related to Migrate or Entity Translation. Anyway, the "delta" issue still remain. Switching to "needs work".

mikeryan’s picture

Status: Needs work » Fixed

OK, I've changed it to not use the explicit delta, each language should increment independently.

Thanks.

claudiu.cristea’s picture

StatusFileSize
new37.78 KB

Thank you... I tested the latest patch and works. Not sure that this is necessarily related to this issue but now I see that the "format" of first row, of default language, of the translated field is set to NULL.

See the attached screenshot.

mikeryan’s picture

And you're not mapping title_field:format, or passing a format option to MigrateDestinationNode()? It should be defaulting to filter_fallback_format(), I don't see why that would be missed the first time through...

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

bachbach’s picture

Status: Closed (fixed) » Active
StatusFileSize
new1.04 KB

Hi,

i ran through the same issue with file field, and this improvement works for me, here is a patch against 7.x-2.5+32-dev (2013-Jan-22)

mikeryan’s picture

Status: Active » Closed (fixed)

Please don't reopen long-closed issues, open a new issue.

Also, when submitting patches please be sure to set the status to "needs review".

joseph.olstad’s picture

Works fine with the body and custom fields, but not for the entity title

I've added some notes/documentation here:
#2091029: Entity translations are not migrated when using *:language subfields

villette’s picture

I made a reroll of the patch in #13
It should work with both dev and 2.6-rc1 versions

villette’s picture

Status: Closed (fixed) » Needs review

The last submitted patch, 13: patch_commit_c4f68ad4f4fd.patch, failed testing.

The last submitted patch, 5: migrate-multi_language-1537076-5.patch, failed testing.

mikeryan’s picture

Status: Needs review » Closed (fixed)

Again - please don't reopen long-closed issues, open a new issue.