Translator
El proyecto Translator está en fase de pruebas alfa.
Se desaconseja usarlo en producción hasta que se publique una versión estable.
Instalación
Consulta la tabla de compatibilidad.
Para instalarlo, ejecuta el siguiente comando de consola.
composer require laravel-lang/translator:^1.0-alpha6
Puedes cambiar la configuración predeterminada editando config/localization.php.
Uso
Si es necesario, indica las credenciales de los proveedores en config/localization.php. Los servicios de pago las necesitan para funcionar correctamente.
Traducción en cadena
Algunas API no cubren todas las configuraciones regionales necesarias. Por eso es posible que debas buscar un servicio capaz de traducir el texto en una sola llamada.
Considera esta configuración de ejemplo.
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',
],
],
],
],
];
En la configuración se activaron Google Translate y DeepL Translate y se desactivó Yandex.Translate. El parámetro enabled solo se aplica a la traducción en cadena.
Supongamos que Google Translate solo puede traducir al francés y que DeepL Translate solo puede traducir al alemán. Se dan tres casos.
- Al francés
- Al alemán
- A un idioma no compatible
- Comprueba si Google Translate puede traducir a este idioma. Sí puede.
Devuelve la traducción de Google Translate.
- Comprueba si Google Translate puede traducir a este idioma. Como no puede, pasa al siguiente traductor.
- Comprueba si DeepL Translate puede traducir a este idioma. Sí puede.
Devuelve la traducción de DeepL Translate.
- Comprueba si Google Translate puede traducir a este idioma. Como no puede, pasa al siguiente traductor.
- Comprueba si DeepL Translate puede traducir a este idioma. Como no puede, pasa al siguiente traductor.
Devuelve el texto original sin traducir.
Mediante la fachada
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);
Mediante inyección de dependencias
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);
}
}
Traducción directa
Mediante Google Translate
Al traducir texto directamente con Google Translate, se ignora el parámetro enabled.
Para traducir directamente, usa la fachada LaravelLang\Translator\Facades\Translate o inyecta la clase de servicio LaravelLang\Translator\Services\Translate como dependencia.
- Mediante la fachada
- Inyectar Translator
- Inyectar Google
use Illuminate\Support\Collection;
use LaravelLang\LocaleList\Locale;
use LaravelLang\Translator\Facades\Translate;
$translate1 = Translate::viaGoogle('some text', Locale::French);
// devuelve una cadena
$translate2 = Translate::viaGoogle([
'some text 1',
'some text 2',
], Locale::French);
// devuelve un arreglo
$translate3 = Translate::viaGoogle([
'foo1' => 'some text 1',
'foo2' => 'some text 2',
], Locale::French);
// devuelve un arreglo
$translate3 = Translate::viaGoogle(new Collection([
'foo1' => 'some text 1',
'foo2' => 'some text 2',
]), Locale::French);
// devuelve un arreglo
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);
}
}
Mediante DeepL Translate
Al traducir texto directamente con DeepL Translate, se ignora el parámetro enabled.
Para traducir directamente, usa la fachada LaravelLang\Translator\Facades\Translate o inyecta la clase de servicio LaravelLang\Translator\Services\Translate como dependencia.
- Mediante la fachada
- Inyectar Translator
- Inyectar DeepL
use Illuminate\Support\Collection;
use LaravelLang\LocaleList\Locale;
use LaravelLang\Translator\Facades\Translate;
$translate1 = Translate::viaDeepl('some text', Locale::French);
// devuelve una cadena
$translate2 = Translate::viaDeepl([
'some text 1',
'some text 2',
], Locale::French);
// devuelve un arreglo
$translate3 = Translate::viaDeepl([
'foo1' => 'some text 1',
'foo2' => 'some text 2',
], Locale::French);
// devuelve un arreglo
$translate3 = Translate::viaDeepl(new Collection([
'foo1' => 'some text 1',
'foo2' => 'some text 2',
]), Locale::French);
// devuelve un arreglo
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);
}
}
Mediante Yandex Translate
Al traducir texto directamente con Yandex.Translate, se ignora el parámetro enabled.
Para traducir directamente, usa la fachada LaravelLang\Translator\Facades\Translate o inyecta la clase de servicio LaravelLang\Translator\Services\Translate como dependencia.
- Mediante la fachada
- Inyectar Translator
- Inyectar Yandex
use Illuminate\Support\Collection;
use LaravelLang\LocaleList\Locale;
use LaravelLang\Translator\Facades\Translate;
$translate1 = Translate::viaYandex('some text', Locale::French);
// devuelve una cadena
$translate2 = Translate::viaYandex([
'some text 1',
'some text 2',
], Locale::French);
// devuelve un arreglo
$translate3 = Translate::viaYandex([
'foo1' => 'some text 1',
'foo2' => 'some text 2',
], Locale::French);
// devuelve un arreglo
$translate3 = Translate::viaYandex(new Collection([
'foo1' => 'some text 1',
'foo2' => 'some text 2',
]), Locale::French);
// devuelve un arreglo
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);
}
}
Compatibilidad
| Laravel | PHP | API de traducción | Paquete | Estado |
|---|---|---|---|---|
| 10, 11, 12, 13 | 8.2, 8.3, 8.4, 8.5 | Google Translate, DeepL Translate, Yandex.Translate | ^1.0 | compatible |