<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once '../database.php';
require '../../vendor/autoload.php';

use Google\Cloud\Translate\V2\TranslateClient;

$translate = new TranslateClient([
    'key' => 'AIzaSyA-mSJEulio_sJbVs0qk-_K1jmvo0TIwBw'
]);

class Translate extends database
{
    public function getSubCategories()
    {
        $sql = 'SELECT * FROM tb_product_subcategory ';
        return $this->callQuery($sql);
    }
    
    public function updateSubCategory($text, $id)
    {
        return $this->update('tb_product_subcategory', ['cl_name_finnish' => $text], ['cl_id' => $id]);
    }
}
     

$obj = new Translate();
$subCategories = $obj->getSubCategories();

                                                
$count = 0;

foreach($subCategories AS $subCategory)
{
    //if(trim($subCategory['cl_name_estonian']) == '')
    //{
        // Translate text from english to french.
        $result = $translate->translate($subCategory['cl_name'], [
            'source' => 'lv',
            'target' => 'fi'
        ]);

        
        $obj->updateSubCategory($result['text'], $subCategory['cl_id']);
    //}
}
