I am using Commerce, Feed Import, Entity Translation, and title to try and import a multi language CSV file with my products.

I have one csv file with title-en, title-fr, title-sp - but cannot import them to create a Product with three translations. I have also tried to do this with a Product Display (which is a normal content entity).

Any successes? Solutions?

Comments

Sorin Sarca’s picture

StatusFileSize
new18.8 KB
new14.65 KB

Is not easy to translate title because is not a field. On Entity Translation module it says

The Title project aims to provide a field replacing node titles and thus make them translatable.

. Download Title module and follow instructions for converting title into a field.

After enabling the module as usual, visit the Manage fields page of the content type, taxonomy vocabulary or "comment type" which you wish to replace fields of and click the replace link. That's all.

For this example I'll use next csv content:

title_en,title_fr,title_es,price
bread,pain,pan,1$
milk,lait,leche,0.5$
sugar,sucre,azúcar,1.5$

Please note that first line contains only column names and will not be imported.
Go to Feed Import and add new one which points to csv file and has process function processCSV. Go to edit it:
-click on Show Process function settings and set for Use column names value of 1. This is very important otherwise import will fail.
-add field type with no xpath but default value article
-add field body with xpath column[@name='price'] because body will contain product price
Now we will use fake fields for multilanguage title: uncheck Use defined fields checkbox and manually add three fields: _title_en, _title_fr and _title_es with the following xpaths: column[@name='title_en'], column[@name='title_fr'] and column[@name='title_es']
Fake fileds
Save feed configuration.
Now create a new module (or use an existing one) and add a node_presave hook (this hook is for a module named test):

/**
 * Implements hook_node_presave().
 */
function test_node_presave($node) {
  // Check content type and fake field.
  if ($node->type == 'article' && isset($node->_title_en)) {
    // Default title is in en.
    $node->title = $node->_title_en['und'][0]['_title_en'];
    // List of other langs.
    $langs = array('en' => array(), 'fr' => array(), 'es' => array());
    // Build multilang array.
    foreach ($langs as $lang => &$value) {
      $lang = '_title_' . $lang;
      $value[]['value'] = $node->{$lang}['und'][0][$lang];
      // Remove fake field.
      unset($node->{$lang});
    }
    // Add to title field.
    $node->title_field = $langs;
  }
}

Save module, clear cache and now you can run import.
If you check with devel imported items you will see something like image below
Devel title field

rtdean93’s picture

Thanks for your input... This is promising, but it appears you have more features / options than I do.

The Parser selected is CSV Parser (not processCSV) and I do not have any XPath options.

I am using 7.x-2.5 for Feed Importer.

rtdean93’s picture

Wow - sorry - I now notice there is a feed_import module (this one) and a feeds_import module which is part of the feeds module.

I'll give your instructions a try.

Thanks

Sorin Sarca’s picture

StatusFileSize
new8.03 KB

If you decide to use this module I'll show you another method (much simpler):
Do anything I said in my previous post until this (inclusive):

-add field body with xpath column[@name='price'] because body will contain product price

(or you can simply remove _title_en, title_fr and _title_es fields and save it)
Now add just one field _title with the following xpath column[@name='title_en'] | column[@name='title_fr'] | column[@name='title_es']

http://drupal.org/files/csv-title-langs.png
Also we have to modify node_presave hook:


/**
 * Implements hook_node_presave().
 */
function test_node_presave($node) {
  if ($node->type == 'article' && isset($node->_title)) {
    // Get all values.
    $node->_title = reset($node->_title);
    // Default title is in en.
    $node->title = $node->_title[0]['_title'];
    // List of other langs.
    $langs = array('en' => array(), 'fr' => array(), 'es' => array());
    $i = 0;
    foreach ($langs as &$value) {
      $value[]['value'] = $node->_title[$i]['_title'];
      $i++;
    }
    // Remove fake field.
    unset($node->_title);
    // Add to title field.
    $node->title_field = $langs;
  }
}

Save, clear cache and run import. Does the same but in a simpler and faster way.

Sorin Sarca’s picture

Status: Active » Closed (works as designed)