3

I am currently using an import plugin to import csv data into a channel section and it works just fine. Now I want to import the entries in a second language. Does anyone have any experiences with this? Since the import works great, I could set up a new channel for the second language, but I wonder if there's a chance to import translations for existing entries.

3

We recently built our first multi lingual site on craft and used FeedMe to import our entries. What we did was to set up fields such as enDesc, deDesc, svDesc. Then when we imported our entries into craft mapping all of our multi lingual content to be spread across these fields in a single locale. Once this was done we then ran a script across the database to copy the content from deDesc into enDesc on the entry with the locale set to de, and then the same for sv also. Here is the script we built to get this job done. We ran this script from a program called CodeRunner on Mac, but you could equally FTP this up to the server and navigate your browser to it to run the script.

<?php
$servername = "XX.XXX.XXX.XXX";
$username = "db_username";
$password = "db_password";
$dbname = "db_name";
date_default_timezone_set('GMT');

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$fieldName = "Desc";
$failed = 0;
$result = $conn->query("SELECT * FROM craft_content WHERE field_de".$fieldName." <> '' AND locale = 'en';");
while($row = $result->fetch_assoc()) {
    $elementId = $row['elementId'];
    $desc = $row['field_de'.$fieldName];
    if ($conn->query("UPDATE craft_content SET field_en".$fieldName."='".$desc."', field_de".$fieldName."=NULL WHERE elementId='".$elementId."' AND locale = 'de';") === TRUE) {
        echo "Updated ".$row['title']." - de";
    } else {
        echo "Failed ".$row['title']." - de";
        $failed++;
    }
}
$result = $conn->query("SELECT * FROM craft_content WHERE field_swe".$fieldName." <> '' AND locale = 'en';");
while($row = $result->fetch_assoc()) {
    $elementId = $row['elementId'];
    $desc = $row['field_swe'.$fieldName];
    if ($conn->query("UPDATE craft_content SET field_en".$fieldName."='".$desc."', field_swe".$fieldName."=NULL WHERE elementId='".$elementId."' AND locale = 'sv';") === TRUE) {
        echo "Updated ".$row['title']." - swe";
    } else {
        echo "Failed ".$row['title']." - swe";
        $failed++;
    }
}
if ($failed == 0) {
    if ($conn->query("UPDATE craft_content SET field_de".$fieldName."=NULL, field_swe".$fieldName."=NULL;") === TRUE) {
        echo "Cleanup Complete";
    } else {
        echo "Cleanup Failed";
    }
}
?>

Once this had completed, the values from deDesc and svDesc should have copied into enDesc on both the German and Swedish locale entries. The values for deDesc and svDesc will also be removed if there were no errors. All you need to do now is rename your field from enDesc to desc and delete the deDesc and svDesc fields from craft. Rinse and repeat this process for each field and cleanup the fields afterwards and that should sort out your multilingual import for you.

Just make sure to take database backups frequently in case there are any errors though.

  • Thx a lot for sharing your solution. This come very handy when importing multilingual content. I my case we have to import all entries in two different languages several time a month when any data has been updated by the client. – jgrote Dec 13 '16 at 8:39
  • You're welcome. I'm glad it helped you reach a solution for your setup. We are in a similar situation with new content still being added to the old site until launch. This could be handled by setting up a CRON to run our import which could target an RSS feed on the old site, but we are just manually importing currently as our content structure has now changed a little. The joys of migrating content! – Matt Shearing Dec 14 '16 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.