Jump to content

Import some columns from .CSV file, split them into text pieces and translate


transparencia

Recommended Posts

This script is supposed to run in the command line.

 

Like the title says, I need to:

 

[*]Import some columns (input the names with an argument) from a .CSV file

[*]Split the column into blocks of 500 lines

[*]Translate this pieces

[*]Put them back into the original file

 

 

I already have some code but it is unfinished and not working:

<?php

//name of the .CSV file
$filename = $argv[1];

//name of the column to be translated
$header_name = $argv[2];

//open file
$file_handle = fopen($filename, "r");

$i=0;
$j=0;
$counter = 0;
$translated = '';
$string = '';

while (!feof($file_handle) ) {

$counter++;

$line_of_text = fgetcsv($file_handle, 1024);

//find the column id that contains the header name.
// headers are always on the the first line
if ( $counter == 1 ){
     while ($line_of_text[$i] != $header_name) $i++;
      $header_id = $i;
                              }

//create the blocks
$string .= $line_of_text[$i];

if ($counter % 500 == 0)
   {
  $gt = new GoogleTranslateWrapper();
$translated .= $gt->translate($string, "en");
    }


}

//Write the translate column in the same .CSV file

fclose($file_handle);


?>

 

Any help in rewriting this to work?  :(

 

This is not something I would've written.  Instead there is a nice CSV parser class that I've used for years:

 

http://www.phpclasses.org/package/849-PHP-Simple-CSV-parser.html

 

Leverage other peoples code whenever possible.  Let them do the debugging for you.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.