mgcodeur / super-translator
SuperTranslator is a PHP package that allows you to translate text from one language to another using the Google Translate API.
1.0.5
2023-11-24 06:34 UTC
Requires
- campo/random-user-agent: ^1.3
Requires (Dev)
- phpunit/phpunit: ^10.4
- symfony/var-dumper: ^6.3
README
About SuperTranslator
SuperTranslator is a PHP package that allows you to translate text from one language to another using the Google Translate API.
Installation
Install the package via composer:
composer require mgcodeur/super-translator
Basic Usage
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!
Automatic language detection
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!
Translate multiple languages
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!' # ]
Contributors ✨
Thanks go to these wonderful people ✨: