chamber-orchestra/translation-bundle
Installation:
composer require chamber-orchestra/translation-bundle
Add the bundle to config/bundles.php:
return [
// ...
ChamberOrchestra\TranslationBundle\TranslationBundle::class => ['all' => true],
];
Configuration: Publish the default config:
php bin/console chamber-orchestra:translation:install
Edit config/packages/chamber_orchestra_translation.yaml to define your translation sources (e.g., google, deepL).
First Use Case: Translate text via a service:
use ChamberOrchestra\TranslationBundle\Service\TranslationService;
$translator = $this->container->get(TranslationService::class);
$result = $translator->translate('Hello', 'en', 'es');
DependencyInjection/: For config and extension logic.Service/: Core translation services (e.g., TranslationService, Provider/GoogleProvider).Exception/: Handle errors like TranslationException or ProviderException.Provider Integration:
Register providers in config/packages/chamber_orchestra_translation.yaml:
chamber_orchestra_translation:
providers:
google:
enabled: true
api_key: '%env(GOOGLE_TRANSLATE_API_KEY)%'
Extend AbstractProvider to add custom providers (e.g., AzureProvider).
Service Injection: Use dependency injection for translation services:
public function __construct(
private TranslationService $translator,
private CacheManagerInterface $cache
) {}
Caching: Enable caching in config:
chamber_orchestra_translation:
cache:
enabled: true
provider: 'app.cache.app'
Cache keys follow the pattern: translation:{source}:{target}:{text}.
Batch Processing:
Use BatchTranslationService for bulk translations:
$batch = $batchTranslator->translateBatch([
['text' => 'Hello', 'target' => 'es'],
['text' => 'World', 'target' => 'fr'],
]);
Symfony Forms:
Add a TranslationType for form fields:
use ChamberOrchestra\TranslationBundle\Form\Type\TranslationType;
$builder->add('translatedText', TranslationType::class, [
'source' => 'en',
'target' => 'es',
]);
Twig Integration: Extend the bundle’s Twig extension to add filters:
{{ 'Hello'|translate('en', 'es') }}
Event Listeners:
Subscribe to TranslationEvent for pre/post-processing:
use ChamberOrchestra\TranslationBundle\Event\TranslationEvent;
public function onTranslation(TranslationEvent $event) {
$event->setText(strtoupper($event->getText())); // Example modifier
}
API Rate Limits:
BatchTranslationService.ProviderException for rate-limit errors.Cache Invalidation:
php bin/console cache:clear
Provider-Specific Quirks:
GOOGLE_TRANSLATE_API_KEY; supports 100+ languages.DEEPL_AUTH_KEY; better for high-quality translations but pricier.Text Length Limits:
Enable Logging:
Add to config/packages/monolog.yaml:
handlers:
translation:
type: stream
path: '%kernel.logs_dir%/translation.log'
level: debug
Logs appear in var/log/translation.log.
Check Provider Responses: Inspect raw API responses for errors:
$translator->translate('Hello', 'en', 'es', true); // Enable debug mode
Test Locally:
Use TranslationService::setTestMode(true) to bypass API calls and return mock responses.
Custom Providers:
Extend AbstractProvider and register in services.yaml:
services:
App\Translation\Provider\MyProvider:
tags: ['chamber_orchestra_translation.provider']
Post-Processing:
Implement PostProcessorInterface to modify translations:
use ChamberOrchestra\TranslationBundle\Service\PostProcessorInterface;
class UppercasePostProcessor implements PostProcessorInterface {
public function process(string $text, string $source, string $target): string {
return strtoupper($text);
}
}
Register in config:
chamber_orchestra_translation:
post_processors:
- App\Translation\UppercasePostProcessor
Event Subscribers:
Listen for TranslationEvent to intercept translations:
use ChamberOrchestra\TranslationBundle\Event\TranslationEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class TranslationSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents(): array {
return [
TranslationEvent::NAME => 'onTranslation',
];
}
public function onTranslation(TranslationEvent $event) {
// Modify $event->getText() or $event->setTargetLanguage()
}
}
Fallback Languages: Define fallbacks in config to handle unsupported language pairs:
chamber_orchestra_translation:
fallback_languages:
'zh-TW': 'zh-CN' # Taiwanese → Simplified Chinese
Environment Variables:
Use %env() for sensitive keys (e.g., API keys) in config/packages/chamber_orchestra_translation.yaml:
providers:
google:
api_key: '%env(GOOGLE_TRANSLATE_API_KEY)%'
How can I help you explore Laravel packages today?