How to translate dynamic texts with php?

Good Guys I want to translate a text with php but I can not find a functional api. I tried to make this work as follows

    <html>
    <head>
    <title>Teste</title>
    </head>
    <body>
    <form action="#" >
       <textarea name="texto"></textarea>
       <input type="submit" value="Traduzir"/>
    </form>

    <?php
    $texto       = $_POST['texto'];
    $language    = "pt_BR";
    $language_for= "en";
    if($texto != NULL){

    //aqui a tradução

    }else{
    echo "Sem Texto Para Traduzir";
    }
    ?>
</body>
</html>

How can I make this work? PS: I need this text to be translated with the help of some api due to the text being randomly picked up on another site.

Author: Guilherme Nascimento, 2017-09-19

1 answers

Can use https://github.com/Stichoza/google-translate-php , which uses Google Translator:

Install via compose in your project folder:

composer require stichoza/google-translate-php

Usage is something like:

use Stichoza\GoogleTranslate\TranslateClient;

require_once __DIR__ . '/vendor/autoload.php';

$tr = new TranslateClient('en', 'pt'); //Inglês para português

echo $tr->translate('Hello World!'), PHP_EOL;

echo $tr->translate('Good morning!'), PHP_EOL;

Another project I found https://github.com/statickidz/php-google-translate-free

To install run the command in your project folder:

composer require statickidz/php-google-translate-free

Usage should be something like:

use Statickidz\GoogleTranslate;

require_once __DIR__ . '/vendor/autoload.php';

$tr = new GoogleTranslate();

echo $tr->translate('en', 'pt', 'Hello World!'), PHP_EOL;

echo $tr->translate('en', 'pt', 'Good morning!'), PHP_EOL;

Extras / related

There are some questions on the site that can be interesting, they do not talk about text translation by API, but rather about the creation of multilingual sites:

Another question, for those who want to translate your site quickly, without paging or just ease the translation process is with the Google translator tool

Of course this Google tool is not 100%, but it works well overall.

 1
Author: Guilherme Nascimento, 2017-09-19 17:46:27