/simple-google-translate

A simple PHP class for Google translate API

Primary LanguagePHPGNU General Public License v3.0GPL-3.0

Simple Google Translate

A simple PHP class for Google translate API. This class is designed to be used to pre-selected languages (not auto language detection).

How to Use

  1. Create an API key on Google Cloud Translate.
  2. Put google-translate.class.php in a folder of your choosing and require the class in your PHP file.
  3. Make sure to add your API key before executing the code.
  4. Assign source language and target language variables. SerpAPI has a good list of all the language codes in you need them.
  5. Execute the translation.

Example

// Require the class
require_once '/path/to/google.translate.class.php';

// Add API key
$apiKey = 'YOUR_API_KEY';
$client = new SimpleGoogleTranslate($apiKey);

// Assign language variables
$sourceLanguage = 'en';
$targetLanguage = 'de';

// Add a string to translate
$string = "Hello this is a test.";

// Execute the code and get the result
$output = $client->translate($string, $sourceLanguage, $targetLanguage);
echo $output;

Output:
Hallo, das ist ein Test.

Links

Follow me on Twitter/X at @tehseowner

Check out my other plugins on OCScripts.com