Installation (despite deprecation, assume legacy project usage):
composer require happyr/translation-bundle
Register the bundle in config/bundles.php:
Happyr\TranslationBundle\HappyrTranslationBundle::class => ['all' => true],
Configuration:
Add to config/packages/happyr_translation.yaml:
happyr_translation:
api_key: '%env(LOCO_API_KEY)%'
project_id: 'your_project_id'
default_locale: 'en'
First Use Case: Fetch translations from Loco for a given locale:
use Happyr\TranslationBundle\Service\TranslationService;
$translations = $this->get('happyr_translation.translation_service')->getTranslations('fr');
Translation Sync:
$service->syncTranslations('es'); // Syncs all missing/updated translations
$service->pushTranslation('es', 'key', 'value');
Translation Loading:
# config/services.yaml
services:
App\Translation\LocoTranslator:
decorates: translator
arguments:
$decorated: '@translator'
Domain-Specific Translations:
$service->getTranslations('de', 'admin'); // Fetches admin-specific keys
cache/ to avoid repeated API calls:
happyr_translation:
cache_enabled: true
cache_lifetime: 3600 # 1 hour
$translator->get('key', [], 'fr', 'messages'); // Falls back to `translations/messages.fr.yml`
Deprecation Warning:
php-translation/symfony-bundle. Migrate if possible.API Rate Limits:
try {
$service->syncTranslations('ja');
} catch (\GuzzleHttp\Exception\RequestException $e) {
if ($e->getCode() === 429) {
sleep(5); // Retry after delay
}
}
Locale Mismatches:
default_locale in config matches your app’s default (e.g., en). Loco may return unexpected keys if misconfigured.Enable Logging:
happyr_translation:
debug: true
Logs API responses to var/log/dev.log.
Validate API Key: Test connectivity with:
$service->ping(); // Returns true if API key is valid
Custom Translation Sources:
Extend Happyr\TranslationBundle\Service\TranslationService to support additional providers (e.g., Crowdin):
class CustomTranslationService extends TranslationService {
public function getTranslations($locale, $domain = null) {
// Override to fetch from Crowdin
}
}
Event Listeners: Hook into translation sync events:
// src/EventListener/TranslationSyncListener.php
public function onSync(TranslationSyncEvent $event) {
$event->addTranslation('new_key', 'value');
}
Register in services.yaml:
services:
App\EventListener\TranslationSyncListener:
tags:
- { name: 'happyr_translation.sync_listener' }
Webhook Integration: Use Loco’s webhooks to auto-trigger syncs on translation updates:
// src/Controller/LocoWebhookController.php
public function handleWebhook(Request $request) {
$this->get('happyr_translation.translation_service')->syncAllLocales();
}
How can I help you explore Laravel packages today?