bin/console google-translate:detect "your text here to detect language code used"
bin/console google-translate:translate "da" "your text here to translate"
<?php
use Lsv\GoogleTranslationBundle\Translate\TranslatorInterface;
final readonly class TranslateText
{
public function __construct(
private TranslatorInterface $translator,
)
{
}
public function translate(string $text): string
{
return $this->translator->translate($text, 'en', isHtml: true)->text;
}
}
composer require lsv/google-translate-bundle
You need to get a Google API key from the Google Cloud Console, and change it in your environment file.
The bundle will be automatically enabled and the configuration will be added to your .env
file.
Add the bundle to your config/bundles.php
file
return [
// ...
Lsv\GoogleTranslationBundle\GoogleTranslationBundle::class => ['all' => true],
];
# config/packages/google_translation.yaml
lsv_google_translate:
google_api_key: '%env(GOOGLE_API_KEY)%'
# .env
GOOGLE_API_KEY=your_api_key_here