SuperTranslator is a PHP package that allows you to translate text from one language to another using the Google Translate API.
Install the package via composer:
composer require mgcodeur/super-translator
require_once 'vendor/autoload.php'; // if you don't use autoloading
use Mgcodeur\SuperTranslator\GoogleTranslate;
$from = 'en';
$to = 'fr';
$text = 'Hello World!';
$translatedText = GoogleTranslate::translate($from, $to, $text);
echo $translatedText;
// Output: Bonjour le monde!
If you want to automatically detect the language of the text to translate, you can use the translateAuto
method of the GoogleTranslate
class like this:
require_once 'vendor/autoload.php'; // if you don't use autoloading
use Mgcodeur\SuperTranslator\GoogleTranslate;
$to = 'fr';
$text = 'Hello World!';
$translatedText = GoogleTranslate::translateAuto($to, $text);
echo $translatedText;
// Output: Bonjour le monde!
You can alse use 'auto' as the value of the $from
parameter:
require_once 'vendor/autoload.php'; // if you don't use autoloading
use Mgcodeur\SuperTranslator\GoogleTranslate;
$from = 'auto';
$to = 'fr';
$text = 'Hello World!';
$translatedText = GoogleTranslate::translate($from, $to, $text);
echo $translatedText;
// Output: Bonjour le monde!
If you want to translate a text into multiple languages, you just have to pass an array of languages to the translate
or translateAuto
method of the GoogleTranslate
class like this:
require_once 'vendor/autoload.php'; // if you don't use autoloading
use Mgcodeur\SuperTranslator\GoogleTranslate;
$from = 'en';
$to = ['fr', 'es', 'de'];
$text = 'Hello World!';
$translatedText = GoogleTranslate::translate($from, $to, $text);
//Nb: the $translatedText variable is an array
# Output: [
# 'fr' => 'Bonjour le monde!',
# 'es' => '¡Hola Mundo!',
# 'de' => 'Hallo Welt!'
# ]
Thanks go to these wonderful people ✨: