PHP code example of topview-digital / laravel-translation-helper

1. Go to this page and download the library: Download topview-digital/laravel-translation-helper 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/ */

    

topview-digital / laravel-translation-helper example snippets


php artisan trans-helper:publish

php artisan trans-helper:install

php artisan queue:work --queue=translation &
php artisan queue:work --queue=cite &



namespace TopviewDigital\TranslationHelper\Service;

use Campo\UserAgent;
use Stichoza\GoogleTranslate\GoogleTranslate;
use TopviewDigital\TranslationHelper\Interfaces\TranslatorInterface;

class GoogleTranslator implements TranslatorInterface
{
    protected $break = 0;
    protected $called = 0;
    protected $word;
    protected $source_locale = null;
    protected $target_locale;

    public function __construct()
    {
        $this->target_locale = config('app.locale');
    }

    public function word(string $word)
    {
        $this->word = $word;
        return $this;
    }

    public function targetLocale(string $target_locale)
    {
        $this->target_locale = $target_locale;
        return $this;
    }

    public function sourceLocale(string $source_locale)
    {
        $this->source_locale = $source_locale;
        return $this;
    }

    public function translate()
    {
        $translated = '';
        $translator = new GoogleTranslate();
        try {
            $translated = is_null($this->source_locale)
                ? $translator
                ->setSource()
                ->setTarget($this->target_locale)
                ->translate($this->word)
                : $translator
                ->setSource($this->source_locale)
                ->setTarget($this->target_locale)
                ->translate($this->word);
        } catch (Exception $e) {
            return null;
        }
        return $translated;
    }
}

php artisan trans-helper:sweep

php artisan trans-helper:trans

php artisan trans-helper:export