Easy Google translate API V2 client for PHP
This project abstract the google translate api version 2.0 in PHP. This is a easy way to implement and use the Google API Translations into PHP and his spirit is to keep it simple and easy to use.
# Clone this repository
$ git clone https://github.com/hstanleycrow/EasyPHPGoogleTranslate
# install libraries
$ composer update
or
# Install using composer
$ composer require hstanleycrow/easyphptranslate
This examples are using DotEnv to configure the APIKey, but you can setup it as you wwant.
Use this code to translate from one language to another.
$myPrivateApiKey = $_ENV['GOOGLE_API_KEY'];
$textToTranslate = "Hola Mundo";
$sourceLanguage = Null;
$targetLanguage = "fr";
$translateObj = new EasyPHPTranslate($myPrivateApiKey, (new CurlRequest(EasyPHPTranslate::ENDPOINT)));
$translatedText = $translateObj->translate($textToTranslate, $targetLanguage);
$originalTextWordCount = $translateObj->wordCount($textToTranslate);
$translatedTextWordCount = $translateObj->wordCount($translatedText);
echo "<pre>";
echo "Text to translate: $textToTranslate" . PHP_EOL;
echo "Word count: $originalTextWordCount" . PHP_EOL;
echo "Translated Text: $translatedText" . PHP_EOL;
echo "Translated Word count: $translatedTextWordCount" . PHP_EOL;
echo "</pre>";
With this code, you can translate a text to a final language, passing before for other languagees. This can change a little bit the translations, simulating a re-phrase, it is very simple but useful.
$textToTranslate = "Hola Mundo, espero que estes bien";
$sourceLanguage = Null;
$targetLanguages = ["fr", "pt", "nl", "en"];
$translateObj = new EasyPHPTranslate($myPrivateApiKey, new CurlRequest(EasyPHPTranslate::ENDPOINT));
$translatedText = $translateObj->multipleTranslate($textToTranslate, $targetLanguages);
echo "<pre>";
echo "Text to translate: $textToTranslate" . PHP_EOL;
echo "Final Translated Text: $translatedText" . PHP_EOL;
echo "</pre>";
This class try to be simple, so, the detection for a valid language is out of their scope, but maybe later. List of languages supported for Google Translation API V2.
I have tested this class only in this PHP versions. So, if you have an older version and do not work, let me know.
PHP Version |
---|
PHP 8.0 |
PHP 8.1 |
PHP 8.2 |
Este proyecto abstraee la API de traducción de Google V2.0 en PHP. Es una forma muy sencilla de usare la API de Google para traducir textos con PHP y su espiritu es mantenerlo simple y fácil de usar, sin configuraciones complejas ni nada por el estilo.
# Clonar repositorio
$ git clone https://github.com/hstanleycrow/EasyPHPGoogleTranslate
or
# Instalar usando composer
$ composer require hstanleycrow/easyphptranslate
Estos ejemplos usan DotEnv para configurar la API Key, pero tu puedes configurarla como te plazca.
Usa este ejemplo para traducir un texto. Solo debes definir el lenguaje al que quieres traducir, el idioma origen lo detecta la API.
$myPrivateApiKey = $_ENV['GOOGLE_API_KEY'];
$textToTranslate = "Hola Mundo";
$sourceLanguage = Null;
$targetLanguage = "fr";
$translateObj = new EasyPHPTranslate($myPrivateApiKey, new CurlRequest(EasyPHPTranslate::ENDPOINT));
$translatedText = $translateObj->translate($textToTranslate, $targetLanguage);
$originalTextWordCount = $translateObj->wordCount($textToTranslate);
$translatedTextWordCount = $translateObj->wordCount($translatedText);
echo "<pre>";
echo "Text to translate: $textToTranslate" . PHP_EOL;
echo "Word count: $originalTextWordCount" . PHP_EOL;
echo "Translated Text: $translatedText" . PHP_EOL;
echo "Translated Word count: $translatedTextWordCount" . PHP_EOL;
echo "</pre>";
Con este código puedes traducir un texto a un lenguaje final pero pasando por otros lenguajes primero. De esta forma se puede cambiar un poco el resultadod de la traducción, especialmente en textos largos, simulando asi un parafraseado, simple claro pero usable.
$textToTranslate = "Hola Mundo, espero que estes bien";
$sourceLanguage = Null;
$targetLanguages = ["fr", "pt", "nl", "en"];
$translateObj = new EasyPHPTranslate($myPrivateApiKey, new CurlRequest(EasyPHPTranslate::ENDPOINT));
$translatedText = $translateObj->multipleTranslate($textToTranslate, $targetLanguages);
echo "<pre>";
echo "Text to translate: $textToTranslate" . PHP_EOL;
echo "Final Translated Text: $translatedText" . PHP_EOL;
echo "</pre>";
Esta clase intenta ser simple, por eso la validación del lenguaje esta fuera de su alcance, pero tal vez luego lo incorpore junto a otras funcionalidades. Lista de lenguajes soportados por la API de traducción de Google V2.
He probado esta clase en estas versiones de PHP, asi qaue si tienes una version anterior y no funciona, dejame saberlo para mejorar el código.
PHP Version |
---|
PHP 8.0 |
PHP 8.1 |
PHP 8.2 |
MIT
www.hablemosdeseo.net · GitHub @hstanleycrow · Twitter @harold_crow