0

I use below PHP code in Laravel to translate one sentence or paragraph from one input field:

    $apiKey = env('GOOGLE_TRANSLATOR_API');
    $url = env('GOOGLE_TRANSLATOR_LINK') . $apiKey . '&q=' . rawurlencode($name) . '&target='.$language;
    $handle = curl_init($url);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($handle);
    $responseDecoded = json_decode($response, true);
    
    return $responseDecoded['data']['translations'][0]['translatedText'];

and it works perfectly fine.

But now I have two input fields (name and description) and I want to translate both of those fields using one API call and show the results individually.

EDIT

I have tried to use array in the API request as below:

    $apiKey = env('GOOGLE_TRANSLATOR_API');
    $url = env('GOOGLE_TRANSLATOR_LINK') . $apiKey . '&q=' . array('sentence one', 'sentence two) . '&target='.$language;
    $handle = curl_init($url);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($handle);
    $responseDecoded = json_decode($response, true);
    
    return $responseDecoded['data']['translations'][0]['translatedText'];

But I get Array to string conversion error.

Sham
  • 23
  • 9
  • @M.Eriksson but in this link it says that google can translate using json, but it is not in php https://cloud.google.com/translate/docs/basic/translating-text#translate_translate_text-drest – Sham May 03 '22 at 11:48
  • 1
    PHP can send anything you want it to in the curl request. So if you want to send JSON, then send JSON. – ADyson May 03 '22 at 11:54
  • And since they give a cURL example in that documentation, you can use https://incarnate.github.io/curl-to-php/ to quickly get a PHP version of that cURL request, to get you started. It's not really clear why you're stuck, – ADyson May 03 '22 at 11:54
  • @ADyson can you write it as answer. – Sham May 03 '22 at 11:55
  • Well it's not really a solution is it? More just a statement of what's possible. I just commented to point out to you that all the information you need to solve this yourself is already available to you. – ADyson May 03 '22 at 11:55
  • @ADyson I tried but failed to solve it. – Sham May 03 '22 at 12:13
  • in that case you need to show us what you tried and explain what specifically went wrong compared to what you expected to happen. "failed" isn't an error message or a meaningful problem description. You can [edit] the question with any updates :-) – ADyson May 03 '22 at 12:26
  • Thanks for the update. Making an array in PHP doesn't, by itself, generate JSON (but it's likely to generate a PHP warning about converting arrays to strings). Also you're not putting the data in the request body. You seem to have guessed, rather than following any of the instructions in the documentation you found, or taking my advice and converting their cURL example into PHP. Or you can use the PHP Google library, as per the example below. – ADyson May 03 '22 at 12:55
  • @ADyson anyways I didn't get your point. – Sham May 03 '22 at 13:12
  • What specifically didn't you understand? – ADyson May 03 '22 at 13:16
  • @ADyson your comments. – Sham May 04 '22 at 08:23
  • As I said, what _specifically_ from what I wrote were you uncertain about? We're not going to get anywhere unless you can clarify that. – ADyson May 04 '22 at 08:43
  • @ADyson this is not clear "PHP can send anything you want it to in the curl request. So if you want to send JSON, then send JSON. – ". If possible write your idea in code. – Sham May 04 '22 at 10:18
  • Well I didn't think it was necessary to write it all out because there are lots of examples of that process online already, e.g. https://stackoverflow.com/questions/11079135/how-to-post-json-data-with-php-curl. Lots more here: https://www.google.com/search?q=php+curl+send+json – ADyson May 04 '22 at 10:27

1 Answers1

1

1Installing the Basic Client Libraries Client library PHP

composer require google/cloud-translate

and in your code

    use Google\Cloud\Translate\V3\TranslationServiceClient;

$translationServiceClient = new TranslationServiceClient();

/** Uncomment and populate these variables in your code */
// $text = 'Hello, world!';
// $textTwo = 'My name is John':
// $targetLanguage = 'fr'; // your lang
// $projectId = '[Google Cloud Project ID]';
$contents = [$text, $textTwo];
$formattedParent = $translationServiceClient->locationName($projectId, 'global');

try {
    $response = $translationServiceClient->translateText(
        $contents,
        $targetLanguage,
        $formattedParent
    );
    // Display the translation for each input text provided
    foreach ($response->getTranslations() as $translation) {
        printf('Translated text: %s' . PHP_EOL, $translation->getTranslatedText());
    }
} finally {
    $translationServiceClient->close();
}

View Code in Github

JEJ
  • 814
  • 5
  • 21