A simple PHP class for Google translate API. This class is designed to be used to pre-selected languages (not auto language detection).
- Create an API key on Google Cloud Translate.
- Put google-translate.class.php in a folder of your choosing and require the class in your PHP file.
- Make sure to add your API key before executing the code.
- Assign source language and target language variables. SerpAPI has a good list of all the language codes in you need them.
- Execute the translation.
// 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.
Follow me on Twitter/X at @tehseowner
Check out my other plugins on OCScripts.com