How to translate dynamic texts with php?

Asked

Viewed 2,846 times

1

Good Guys I’m wanting to translate a text with php but I can’t find a functional api. I tried to make it work this way

    <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 picked up randomly on another site.

1 answer

1


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

Install via commiserate in your project folder:

composer require stichoza/google-translate-php

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

The use 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 may be interesting, do not speak of text translation by API, but rather of the creation of multilingual websites:

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

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

  • I will use a host, it will be possible to install it on the host?

  • @Carlosdasilva downloads the project from the host to your machine, installs the Composer on your machine, installs it with the command I sent, adds the autoload.php adjusts the codes as in the example and sends it back to your HOST via FTP.

  • Ah ta got to understand Poser is a tool that assists in installing the package from github

  • @Carlosdasilva is yes, as I indicated on https://getcomposer.org/, I recommend that before you change anything in your project I study Composer very well, today it is rare who does not use Composer in more serious projects, only some do not use Composer, who does not know what it is when the project is very simple. Just for the record, Poser is not something you learn overnight, unless you’ve had experience with PIP, NPM or similar tools, so if you don’t understand Poser still recommend reading very calmly and carefully and back up your project.

  • I’ve used NPM, I’m coming back here just to inform you that I got what I wanted. I thank you the same

Browser other questions tagged

You are not signed in. Login or sign up in order to post.