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?  :(

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.