Skip to main content
Version: Legacy

Translator

Alpha unknown

View on GitHub

warning

The Translator project is in alpha testing.

Use in production is discouraged until it is released into a stable version.

Installation​

note

The compatibility table can be viewed here.

To install, run the console command:

composer require laravel-lang/translator:^1.0-alpha6

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

Usage​

tip

If necessary, specify the providers' authorization data in the config/localization.php settings file. Without this, paid services will not work properly.

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.

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​

note

When directly translating text using Google Translate, the enabled settings parameter is ignored.

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

Via DeepL Translate​

note

When directly translating text using DeepL Translate, the enabled settings parameter is ignored.

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

Via Yandex Translate​

note

When directly translating text using Yandex.Translate, the enabled settings parameter is ignored.

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

Compatibility​

LaravelPHPTranslators APIPackageStatus
10, 11, 12, 138.2, 8.3, 8.4, 8.5Google Translate, DeepL Translate, Yandex.Translate^1.0supported