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);

MalvikLab\LibreTranslateClient\DTO\DetectResponseDTO Object
(
    [items] => Array
        (
            [0] => MalvikLab\LibreTranslateClient\DTO\DetectDTO Object
                (
                    [confidence] => 100
                    [language] => it
                )

        )

)



// ...

$response = $client->languages();

MalvikLab\LibreTranslateClient\DTO\LanguagesResponseDTO Object
(
    [items] => Array
        (
            [0] => MalvikLab\LibreTranslateClient\DTO\LanguageDTO Object
                (
                    [code] => en
                    [name] => English
                    [targets] => Array
                        (
                            [0] => ar
                            // ...
                            [45] => zt
                        )
                )
                
            // ...
        )

)



// ...

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);

MalvikLab\LibreTranslateClient\DTO\TranslateFileResponseDTO Object
(
    [translatedFileUrl] => http://localhost:5000/download_file/72e720fe-1568-457a-a1a4-017939c9f533.file_en.txt
)



// ...

use MalvikLab\LibreTranslateClient\DTO\SuggestRequestDTO;

$request = new SuggestRequestDTO(
    'Hello world!',
    '¡Hola mundo!',
    'en',
    'es'
);
$response = $client->suggest($request);

MalvikLab\LibreTranslateClient\DTO\SuggestResponseDTO Object
(
    [success] => 1
)



// ...

$response = $client->frontendSettings();

MalvikLab\LibreTranslateClient\DTO\FrontendSettingsResponseDTO Object
(
    [apiKeys] => 
    [charLimit] => -1
    [filesTranslation] => 1
    [frontendTimeout] => 500
    [keyRequired] => 
    [language] => MalvikLab\LibreTranslateClient\DTO\SettingsLanguageDTO Object
        (
            [source] => MalvikLab\LibreTranslateClient\DTO\SourceDTO Object
                (
                    [code] => auto
                    [name] => Auto Detect
                )

            [target] => MalvikLab\LibreTranslateClient\DTO\TargetDTO Object
                (
                    [code] => sq
                    [name] => Albanian
                )

        )

    [suggestions] => 1
    [supportedFilesFormat] => Array
        (
            [0] => .txt
            [1] => .odt
            [2] => .odp
            [3] => .docx
            [4] => .pptx
            [5] => .epub
            [6] => .html
        )

)