Translator | Laravel Lang

Laravel Lang Help

Translator

Installation

To install, run the console command:

composer require laravel-lang/translator

You can also change the default settings by making changes to the config/localization.php file.

Usage

Cyclic translation

Not all API services cover the required number of localizations for translation, and therefore it is possible to find one that can translate the text with one call.

For example, we have settings:

use LaravelLang\Translator\Integrations\Deepl; use LaravelLang\Translator\Integrations\Google; use LaravelLang\Translator\Integrations\Yandex; return [ 'translators' => [ 'channels' => [ 'google' => [ 'translator' => Google::class, 'enabled' => true, ], 'deepl' => [ 'translator' => Deepl::class, 'enabled' => true, 'credentials' => [ 'key' => 'deepl-secret-key', ], ], 'yandex' => [ 'translator' => Yandex::class, 'enabled' => false, 'credentials' => [ 'key' => 'yandex-secret-key', 'folder' => 'yandex-folder-id', ], ], ], ], ];

In the settings, we specified Google Translate and DeepL Translate as active translators, and also deactivated Yandex.Translate. The enabled parameter applies only to cyclic translation.

Let's say Google Translate can only translate into French, and DeepL Translate can only translate into German. Thus, we get three translation cases:

  1. We check whether Google Translate can translate into this language. Yes, he can translate.

We return text translation from Google Translate.

  1. We check whether Google Translate can translate into this language. No, he cannot. Let's move on to the next translator.

  2. We check whether DeepL Translate can translate into this language. Yes, he can translate.

We return the text translation from DeepL Translate.

  1. We check whether Google Translate can translate into this language. No, he cannot. Let's move on to the next translator.

  2. We check whether DeepL Translate can translate into this language. No, he cannot. Let's move on to the next translator.

We return the original text without translation.

Via Facade

use LaravelLang\LocaleList\Locale; use LaravelLang\Translator\Facades\Translate; $translated1 = Translate::text( text: 'some text', to : Locale::French ); $translated2 = Translate::text([ 'some text 1', 'some text 2', ], Locale::French); $translated3 = Translate::text([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ], Locale::French);

Via Dependency Injection

use Illuminate\Support\Collection; use LaravelLang\LocaleList\Locale; use LaravelLang\Translator\Services\Translate as TranslateService; class Example { public function __construct( protected TranslateService $translate ) { } public function get1(): string { return $this->translate->text('some text', Locale::French); } public function get2(): array { return $this->translate->text([ 'some text 1', 'some text 2', ], Locale::French); } public function get3(): array { return $this->translate->text([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ], Locale::French); } public function get4(): array { return $this->translate->text(new Collection([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ]), Locale::French); } }

Direct translate

Via Google Translate

To call direct translation, you can use either the LaravelLang\Translator\Facades\Translate facade or the LaravelLang\Translator\Services\Translate service class, connecting it via DI.

use Illuminate\Support\Collection; use LaravelLang\LocaleList\Locale; use LaravelLang\Translator\Facades\Translate; $translate1 = Translate::viaGoogle('some text', Locale::French); // returns string $translate2 = Translate::viaGoogle([ 'some text 1', 'some text 2', ], Locale::French); // returns array $translate3 = Translate::viaGoogle([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ], Locale::French); // returns array $translate3 = Translate::viaGoogle(new Collection([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ]), Locale::French); // returns array
use Illuminate\Support\Collection; use LaravelLang\LocaleList\Locale; use LaravelLang\Translator\Services\Translate; class Example { public function __construct( protected Translate $translator ) { } public function fromString(): string { return $this->translator->viaGoogle('some text', Locale::French); } public function fromArray1(): array { return $this->translator->viaGoogle([ 'some text 1', 'some text 2', ], Locale::French); } public function fromArray2(): array { return $this->translator->viaGoogle([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ], Locale::French); } public function fromCollection(): array { return $this->translator->viaGoogle(new Collection([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ]), Locale::French); } }
use Illuminate\Support\Collection; use LaravelLang\LocaleList\Locale; use LaravelLang\Translator\Integrations\Google; class Example { public function __construct( protected Google $google ) { } public function fromString(): string { return $this->google->translate('some text', Locale::French); } public function fromArray1(): array { return $this->google->translate([ 'some text 1', 'some text 2', ], Locale::French); } public function fromArray2(): array { return $this->google->translate([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ], Locale::French); } public function fromCollection(): array { return $this->google->translate(new Collection([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ]), Locale::French); } }

Via DeepL Translate

To call direct translation, you can use either the LaravelLang\Translator\Facades\Translate facade or the LaravelLang\Translator\Services\Translate service class, connecting it via DI.

use Illuminate\Support\Collection; use LaravelLang\LocaleList\Locale; use LaravelLang\Translator\Facades\Translate; $translate1 = Translate::viaDeepl('some text', Locale::French); // returns string $translate2 = Translate::viaDeepl([ 'some text 1', 'some text 2', ], Locale::French); // returns array $translate3 = Translate::viaDeepl([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ], Locale::French); // returns array $translate3 = Translate::viaDeepl(new Collection([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ]), Locale::French); // returns array
use Illuminate\Support\Collection; use LaravelLang\LocaleList\Locale; use LaravelLang\Translator\Services\Translate; class Example { public function __construct( protected Translate $translator ) { } public function fromString(): string { return $this->translator->viaDeepl('some text', Locale::French); } public function fromArray1(): array { return $this->translator->viaDeepl([ 'some text 1', 'some text 2', ], Locale::French); } public function fromArray2(): array { return $this->translator->viaDeepl([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ], Locale::French); } public function fromCollection(): array { return $this->translator->viaDeepl(new Collection([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ]), Locale::French); } }
use Illuminate\Support\Collection; use LaravelLang\LocaleList\Locale; use LaravelLang\Translator\Integrations\Deepl; class Example { public function __construct( protected Deepl $deepl ) { } public function fromString(): string { return $this->deepl->translate('some text', Locale::French); } public function fromArray1(): array { return $this->deepl->translate([ 'some text 1', 'some text 2', ], Locale::French); } public function fromArray2(): array { return $this->deepl->translate([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ], Locale::French); } public function fromCollection(): array { return $this->deepl->translate(new Collection([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ]), Locale::French); } }

Via Yandex Translate

To call direct translation, you can use either the LaravelLang\Translator\Facades\Translate facade or the LaravelLang\Translator\Services\Translate service class, connecting it via DI.

use Illuminate\Support\Collection; use LaravelLang\LocaleList\Locale; use LaravelLang\Translator\Facades\Translate; $translate1 = Translate::viaYandex('some text', Locale::French); // returns string $translate2 = Translate::viaYandex([ 'some text 1', 'some text 2', ], Locale::French); // returns array $translate3 = Translate::viaYandex([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ], Locale::French); // returns array $translate3 = Translate::viaYandex(new Collection([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ]), Locale::French); // returns array
use Illuminate\Support\Collection; use LaravelLang\LocaleList\Locale; use LaravelLang\Translator\Services\Translate; class Example { public function __construct( protected Translate $translator ) { } public function fromString(): string { return $this->translator->viaYandex('some text', Locale::French); } public function fromArray1(): array { return $this->translator->viaYandex([ 'some text 1', 'some text 2', ], Locale::French); } public function fromArray2(): array { return $this->translator->viaYandex([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ], Locale::French); } public function fromCollection(): array { return $this->translator->viaYandex(new Collection([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ]), Locale::French); } }
use Illuminate\Support\Collection; use LaravelLang\LocaleList\Locale; use LaravelLang\Translator\Integrations\Yandex; class Example { public function __construct( protected Yandex $yandex ) { } public function fromString(): string { return $this->yandex->translate('some text', Locale::French); } public function fromArray1(): array { return $this->yandex->translate([ 'some text 1', 'some text 2', ], Locale::French); } public function fromArray2(): array { return $this->yandex->translate([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ], Locale::French); } public function fromCollection(): array { return $this->yandex->translate(new Collection([ 'foo1' => 'some text 1', 'foo2' => 'some text 2', ]), Locale::French); } }

Compatibility

Laravel

PHP

Translators API

Package

Status

10, 11

8.2, 8.3

Google Translate, DeepL Translate, Yandex.Translate

^1.0

supported

Last modified: 16 July 2024