PHP code example of oss-tools / laravel-libretranslate

1. Go to this page and download the library: Download oss-tools/laravel-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/ */

    

oss-tools / laravel-libretranslate example snippets

 php
use OSSTools\LibreTranslate\Client;
use OSSTools\LibreTranslate\Translation\LanguageCodes;

class ExampleController extends Controller
{
    public function translate()
    {
        $client = new Client();
        
        // Returns an instance of \OSSTools\LibreTranslate\Translation\TranslationCollection
        $result = $client->translate('This is some text', LanguageCodes::SPANISH);
        
        // Returns an array of \OSSTools\LibreTranslate\Translation\TranslationItem
        $result = $client->translate('This is some text', LanguageCodes::SPANISH)->getAll();
        
        // Returns a single instance of \OSSTools\LibreTranslate\Translation\TranslationItem
        $result = $client->translate(['This is some text', 'A test'], LanguageCodes::SPANISH)->first();
        
        // Returns a single instance of \OSSTools\LibreTranslate\Translation\TranslationItem
        $result = $client->translate(['This is some text', 'A test'], LanguageCodes::SPANISH)->last();
        
        // Returns a single instance of \OSSTools\LibreTranslate\Translation\TranslationItem
        $result = $client->translate(['This is some text', 'A test'], LanguageCodes::SPANISH)->get('A test');
        
        // Returns "Una prueba"
        $result = $client->translate(['This is some text', 'A test'], LanguageCodes::SPANISH)->last()->getText();
    }
}
 php
use OSSTools\LibreTranslate\Client;

class ExampleController extends Controller
{
    public function translate()
    {
        $client = new Client();
        
        // Returns an instance of \OSSTools\LibreTranslate\Translation\TranslationDetectionCollection
        $result = $client->detect('This is some text');
        
        // Returns an array of \OSSTools\LibreTranslate\Translation\TranslationDetectionItem
        $result = $client->detect('This is some text')->getAll();
        
        // Returns a single instance of \OSSTools\LibreTranslate\Translation\TranslationDetectionItem
        $result = $client->translate('This is some text')->first();

        // Returns "en"
        $result = $client->detect('Some text')->first()->getLanguage();
        
        // Returns "es"
        $result = $client->detect('Una prueba')->first()->getLanguage();
    }
}