1. Go to this page and download the library: Download jefs42/libretranslate library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jefs42 / libretranslate example snippets
use Jefs42\LibreTranslate;
$translator = new LibreTranslate();
use Jefs42\LibreTranslate;
$translator = new LibreTranslate();
// use locally installed LibreTranslate server on port 5000 with default language settings
$translator = new LibreTranslate();
// specify server with alternate port
$translator = new LibreTranslate("https://libretranslate", 5042);
// use localhost, default port, but override default languages used in translations
$translator = new LibreTranslate(null, null, 'de', 'it');
$translator->setApiKey('xxxxx-xxxxx-xxxxx');
// Set to translate from English to Swedish
$translator->setLanguages('en', 'sv');
// change only one - source or target
$translator->setSource('es');
$translator->setTarget('ru');
$lang = $translator->detect("mi nombre es jefs42");
// expected result: $lang = 'es'
// translate text using current default source/target languages
$translatedText = $translator->translate("My name is jefs42");
// specifally request languages to use in translation.
// eg. from English to German
$translatedText = $translator->translate("My name is jefs42", "en", "de");
// translate multiple texts in one call
// returns array of translated texts
$translatedText = $translator->translate(["My name is jefs42", "Where is the bathroom?"]);
// translate file using current default source/target languages
$translatedText = $translator->translateFile("/full/path/to/file.txt");
// translate file with specific source/target languages
// eg. from English to Italian
$translatedText = $translator->translateFile("/full/path/to/file.txt", "en", "it");
// Submit suggestion using current source/target language
$translator->suggest('My name is jefs42', 'Mi nombre es jefs42');
// Specify languages for suggestion
$translator->suggest('My name is jefs42', 'Mi chiamo jefs42', 'en', 'it');
$languages = $translator->Languages();
/*
Returns array of language codes/names:
[en] => 'English',
[it] => 'Italian',
...
*/
$settings = $translator->Settings();
/*
Returns array of settings and their current values:
[api_keys] => 1,
[keyRequired] => ,
[char_limit] => -1,
...
*/
$keys = $translator->listKeys();
/*
Returns array of current keys and limits:
[
'key1' => '500',
'key2' => '50',
...
]
*/
// create a new key limited to server defaults
$key = $translator->addKey();
// create a new key with a specific request limit (higher or lower than server default)
$key = $translator->addKey(400); // limit to 400 requests per minute
/*
Returns string of new key:
"xxxxx-xxxx-xxxx"
*/