Installation
composer require alicorn/lokalise-bundle
Ensure ext-zip and ext-curl are enabled in your PHP environment.
Enable the Bundle
Register in config/bundles.php (Symfony 4+):
Alicorn\LokaliseBundle\AlicornLokaliseBundle::class => ['all' => true],
Configure Routing
Add to config/routes.yaml:
alicorn_lokalise:
resource: "@AlicornLokaliseBundle/Controller/"
type: annotation
prefix: /lokalise/webhook
Basic Configuration
Set minimal config/packages/alicorn_lokalise.yaml:
alicorn_lokalise:
api:
api_token: "%env(LOKALISE_API_TOKEN)%"
project_id: "%env(LOKALISE_PROJECT_ID)%"
web_path: "public/locales"
symfony_path: "translations"
First Use Case
/lokalise/webhook endpoint.public/locales and translations/.Webhook-Driven Updates
web_path (e.g., public/locales) and symfony_path (e.g., translations/ for Symfony’s translation system).json, xliff, etc. (configure via type in api).CLI Sync Manually sync translations via:
php bin/console lokalise:import
Useful for testing or one-off updates without triggering a build.
Bundle Structure
bundle_structure: Customize file naming with placeholders like %%LANG_ISO%% (e.g., en.json, fr.xliff).directory_prefix: Group files by language (e.g., en/translations.json).Integration with Symfony Translation System
symfony_path (e.g., translations/messages.en.yaml) to auto-load via Symfony’s translator.config/packages/translation.yaml:
framework:
translator:
paths:
- '%kernel.project_dir%/translations'
Environment-Specific Configs
Override paths/tokens per environment (e.g., config/packages/dev/alicorn_lokalise.yaml):
alicorn_lokalise:
web_path: "%kernel.project_dir%/public/locales/dev"
POST /lokalise/webhook/validate endpoint to verify webhook signatures (Lokalise sends a X-Signature header).monolog:
handlers:
main:
level: debug
php bin/console cache:clear
Webhook Signature Verification
X-Signature:
// src/EventListener/LokaliseWebhookListener.php
use Symfony\Component\HttpKernel\Event\RequestEvent;
public function onKernelRequest(RequestEvent $event) {
$request = $event->getRequest();
if ($request->getPathInfo() === '/lokalise/webhook') {
$signature = $request->headers->get('X-Signature');
// Implement validation logic (e.g., HMAC comparison)
}
}
File Overwrites
web_path and symfony_path. Backup critical files or use a pre-update hook.Path Permissions
web_path and symfony_path:
chmod -R 775 public/locales translations/
Deprecated Symfony Features
AppKernel). For Symfony 4/5, use config/bundles.php and adjust routing to YAML/PHP format.API Rate Limits
lokalise:import).var/log/dev.log) for Alicorn\LokaliseBundle errors.curl:
curl -X POST -H "X-Signature: YOUR_SIGNATURE" -F "file=@/tmp/langs.zip" http://localhost/lokalise/webhook
-v for verbose output:
php bin/console lokalise:import -v
extract_file (/tmp/langs.zip) is writable.Custom File Processing
Override the FileProcessor service to transform files post-download:
services:
alicorn_lokalise.file_processor:
class: App\Service\CustomFileProcessor
tags: ['alicorn_lokalise.file_processor']
Implement processFile() in your class.
Post-Update Hooks
Subscribe to the lokalise.post_update event:
// src/EventListener/TranslationUpdateListener.php
use Alicorn\LokaliseBundle\Event\PostUpdateEvent;
public function onPostUpdate(PostUpdateEvent $event) {
// Clear cache, notify Slack, etc.
}
Register in services.yaml:
services:
App\EventListener\TranslationUpdateListener:
tags:
- { name: kernel.event_listener, event: lokalise.post_update, method: onPostUpdate }
Custom API Endpoints
Extend the LokaliseController to add endpoints (e.g., for manual file uploads):
// src/Controller/LokaliseController.php
use Alicorn\LokaliseBundle\Controller\LokaliseController as BaseController;
class LokaliseController extends BaseController {
public function customUploadAction() {
// ...
}
}
How can I help you explore Laravel packages today?