PHP code example of malvik-lab / libre-translate-api-client
1. Go to this page and download the library: Download malvik-lab/libre-translate-api-client 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/ */
malvik-lab / libre-translate-api-client example snippets
uzzleHttp\Client as HttpClient;
use MalvikLab\LibreTranslateClient\Client;
// LibreTranslate server URL
$baseUrl = 'http://localhost:5000';
// If the server ey, if the server does not e null)
// Example 3: Configuration with a custom Guzzle instance (for advanced customization)
$httpClient = new HttpClient(); // Custom Guzzle client (useful for adding middlewares, timeouts, etc.)
$client = new Client($baseUrl, $apiKey, $httpClient); // With API Key
// or
$client = new Client($baseUrl, null, $httpClient); // Without API Key (if server is free)
// ...
use MalvikLab\LibreTranslateClient\DTO\DetectRequestDTO;
$request = new DetectRequestDTO('Il diavolo fa le pentole ma non i coperchi.');
$response = $client->detect($request);
// ...
use MalvikLab\LibreTranslateClient\DTO\TranslateRequestDTO;
use MalvikLab\LibreTranslateClient\Enum\FormatEnum;
$request = new TranslateRequestDTO(
'Il lupo perde il pelo ma non il vizio',
'it',
'en',
FormatEnum::TEXT,
3
);
$response = $client->translate($request);
MalvikLab\LibreTranslateClient\DTO\TranslateResponseDTO Object
(
[translatedText] => The wolf loses the fur but not the vice
[alternatives] => Array
(
[0] => The wolf loses his hair but not his vice
[1] => The wolf loses his fur but not his vice
[2] => The wolf loses his fur but not the vice
)
)
// ...
use MalvikLab\LibreTranslateClient\DTO\TranslateFileRequestDTO;
$request = new TranslateFileRequestDTO(
'path/to/file.txt',
'it',
'en'
);
$response = $client->translateFile($request);