chiarillomax/yandex-translator-bundle
Installation:
composer require chiarillomax/yandex-translator-bundle
Register the bundle in app/AppKernel.php:
new \Yandex\TranslatorBundle\YandexTranslatorBundle(),
Configuration:
Add Yandex API key to config.yml (or parameters.yml):
parameters:
yandex_translator.key: 'your_yandex_api_key_here'
First Use Case: Translate text in a controller:
use Yandex\TranslatorBundle\YandexTranslator;
use Yandex\TranslatorBundle\Model\Key;
$translator = $this->get('yandex.translator');
$key = new Key();
$key->setValue($this->container->getParameter('yandex_translator.key'));
$translation = $translator->createRequest()
->setKey($key->getValue())
->setText('Hello World')
->setFrom('en')
->setTo('es')
->send();
return $translation->getText(); // "Hola Mundo"
Request Creation:
Always use createRequest() to build translation requests:
$request = $this->get('yandex.translator')->createRequest();
Chaining Methods:
Chain setKey(), setText(), setFrom(), and setTo() for clarity:
$request->setKey($apiKey)
->setText($text)
->setFrom('en')
->setTo('fr');
Service Integration: Inject the translator service into controllers/services:
class TranslationService {
protected $translator;
public function __construct(YandexTranslator $translator) {
$this->translator = $translator;
}
}
Batch Translations: Loop through texts and translate in bulk (if API supports it):
foreach ($texts as $text) {
$translation = $this->translator->createRequest()
->setKey($apiKey)
->setText($text)
->setFrom('en')
->setTo('de')
->send();
}
Error Handling: Wrap requests in try-catch blocks:
try {
$translation = $request->send();
} catch (\Exception $e) {
$this->logger->error('Translation failed: ' . $e->getMessage());
}
Deprecated Symfony2:
API Key Management:
parameters.yml or environment variables:
# config/packages/dev/yandex_translator.yaml
yandex_translator:
key: '%env(YANDEX_TRANSLATOR_KEY)%'
Rate Limits:
$cache = $this->get('cache.app');
$cacheKey = md5($text . $from . $to);
if (!$cache->has($cacheKey)) {
$translation = $request->send();
$cache->set($cacheKey, $translation->getText(), 3600); // Cache for 1 hour
} else {
$translation = $cache->get($cacheKey);
}
Language Codes:
en-US for US English, not just en).Service Not Found:
$this->get('yandex.translator') fails, ensure:
AppKernel.php.composer dump-autoload).Enable API Debugging: Add a debug flag to log requests/responses:
$request->setDebug(true); // If supported by the bundle
Check HTTP Status: Inspect the raw response for errors:
$response = $request->send();
if ($response->getStatusCode() !== 200) {
throw new \RuntimeException('API Error: ' . $response->getContent());
}
Update Dependencies:
If issues arise, check for outdated dependencies (e.g., guzzlehttp/guzzle):
composer update guzzlehttp/guzzle
Custom Response Handling:
Extend the YandexTranslator service to add logic:
class CustomYandexTranslator extends YandexTranslator {
public function translateWithFallback($text, $from, $to) {
try {
return parent::createRequest()
->setText($text)
->setFrom($from)
->setTo($to)
->send()->getText();
} catch (\Exception $e) {
return $text; // Fallback to original text
}
}
}
Event Listeners: Dispatch events before/after translation (e.g., for logging):
// In services.yml
services:
app.translation_listener:
class: AppBundle\EventListener\TranslationListener
tags:
- { name: kernel.event_listener, event: yandex.translation, method: onTranslation }
Configuration Overrides:
Override default settings via config.yml:
yandex_translator:
default_from: 'en'
default_to: 'es'
timeout: 30
How can I help you explore Laravel packages today?