How to translate dynamic texts with php?

1

Well I'm trying to translate a text with php but I can not find a functional api. I tried to make it 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 because the text is randomly picked up on another site.

    
asked by anonymous 19.09.2017 / 18:49

1 answer

1

You can use link , which uses Google Translate:

Install via composer in your project folder:

composer require stichoza/google-translate-php

The 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 link

To install run the command in the folder of your project:

composer require statickidz/php-google-translate-free

Use should look 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 a few questions on the site that may be interesting, do not talk about text translation by API, but about creating multilingual websites:

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

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

    
19.09.2017 / 19:31