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

This package is auto-updated.

Last update: 2024-04-03 21:59:09 UTC


README

Run Tests Packagist Downloads (custom server)

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!'
# ]
68747470733a2f2f696d672e6275796d6561636f666665652e636f6d2f627574746f6e2d6170692f3f746578743d4275792532306d6525323061253230636f6666656526656d6f6a693d26736c75673d6d67636f6465757226627574746f6e5f636f6c6f75723d46463546354626666f6e745f636f6c6f75723d66666666666626666f6e745f66616d696c793d506f7070696e73266f75746c696e655f636f6c6f75723d30303030303026636f666665655f636f6c6f75723d464644443030

Contributors ✨

Thanks go to these wonderful people ✨: